001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.nio.ByteBuffer;
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.IOTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.ByteBufferUtils;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ IOTests.class, SmallTests.class })
035public class TestByteBufferListOutputStream {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestByteBufferListOutputStream.class);
040
041  @Test
042  public void testWrites() throws Exception {
043    ByteBufferPool pool = new ByteBufferPool(10, 3);
044    ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(pool);
045    bbos.write(2);// Write a byte
046    bbos.writeInt(100);// Write an int
047    byte[] b = Bytes.toBytes("row123");// 6 bytes
048    bbos.write(b);
049    // Just use the 3rd BB from pool so that pabos, on request, wont get one
050    ByteBuffer bb1 = pool.getBuffer();
051    ByteBuffer bb = ByteBuffer.wrap(Bytes.toBytes("row123_cf1_q1"));// 13 bytes
052    bbos.write(bb, 0, bb.capacity());
053    pool.putbackBuffer(bb1);
054    bbos.writeInt(123);
055    bbos.writeInt(124);
056    assertEquals(0, pool.getQueueSize());
057    List<ByteBuffer> allBufs = bbos.getByteBuffers();
058    assertEquals(4, allBufs.size());
059    assertEquals(3, bbos.bufsFromPool.size());
060    ByteBuffer b1 = allBufs.get(0);
061    assertEquals(10, b1.remaining());
062    assertEquals(2, b1.get());
063    assertEquals(100, b1.getInt());
064    byte[] bActual = new byte[b.length];
065    b1.get(bActual, 0, 5);//5 bytes in 1st BB
066    ByteBuffer b2 = allBufs.get(1);
067    assertEquals(10, b2.remaining());
068    b2.get(bActual, 5, 1);// Remaining 1 byte in 2nd BB
069    assertTrue(Bytes.equals(b, bActual));
070    bActual = new byte[bb.capacity()];
071    b2.get(bActual, 0, 9);
072    ByteBuffer b3 = allBufs.get(2);
073    assertEquals(8, b3.remaining());
074    b3.get(bActual, 9, 4);
075    assertTrue(ByteBufferUtils.equals(bb, 0, bb.capacity(), bActual, 0, bActual.length));
076    assertEquals(123, b3.getInt());
077    ByteBuffer b4 = allBufs.get(3);
078    assertEquals(4, b4.remaining());
079    assertEquals(124, b4.getInt());
080    bbos.releaseResources();
081    assertEquals(3, pool.getQueueSize());
082  }
083}