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