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.nio.ByteBuff;
027import org.apache.hadoop.hbase.testclassification.IOTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.ByteBufferUtils;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({ IOTests.class, SmallTests.class })
036public class TestByteBufferListOutputStream {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestByteBufferListOutputStream.class);
041
042  @Test
043  public void testWrites() throws Exception {
044    ByteBuffAllocator alloc = new ByteBuffAllocator(true, 3, 10, 10 / 6);
045    ByteBufferListOutputStream bbos = new ByteBufferListOutputStream(alloc);
046    bbos.write(2);// Write a byte
047    bbos.writeInt(100);// Write an int
048    byte[] b = Bytes.toBytes("row123");// 6 bytes
049    bbos.write(b);
050    assertEquals(2, bbos.allBufs.size());
051    // Just use the 3rd BB from pool so that pabos, on request, wont get one
052    ByteBuff bb1 = alloc.allocateOneBuffer();
053    ByteBuffer bb = ByteBuffer.wrap(Bytes.toBytes("row123_cf1_q1"));// 13 bytes
054    bbos.write(bb, 0, bb.capacity());
055    bb1.release();
056    bbos.writeInt(123);
057    bbos.writeInt(124);
058    assertEquals(0, alloc.getFreeBufferCount());
059    List<ByteBuffer> allBufs = bbos.getByteBuffers();
060    assertEquals(4, allBufs.size());
061    assertEquals(4, bbos.allBufs.size());
062    ByteBuffer b1 = allBufs.get(0);
063    assertEquals(10, b1.remaining());
064    assertEquals(2, b1.get());
065    assertEquals(100, b1.getInt());
066    byte[] bActual = new byte[b.length];
067    b1.get(bActual, 0, 5);// 5 bytes in 1st BB
068    ByteBuffer b2 = allBufs.get(1);
069    assertEquals(10, b2.remaining());
070    b2.get(bActual, 5, 1);// Remaining 1 byte in 2nd BB
071    assertTrue(Bytes.equals(b, bActual));
072    bActual = new byte[bb.capacity()];
073    b2.get(bActual, 0, 9);
074    ByteBuffer b3 = allBufs.get(2);
075    assertEquals(8, b3.remaining());
076    b3.get(bActual, 9, 4);
077    assertTrue(ByteBufferUtils.equals(bb, 0, bb.capacity(), bActual, 0, bActual.length));
078    assertEquals(123, b3.getInt());
079    ByteBuffer b4 = allBufs.get(3);
080    assertEquals(4, b4.remaining());
081    assertEquals(124, b4.getInt());
082    bbos.releaseResources();
083    assertEquals(3, alloc.getFreeBufferCount());
084  }
085}