1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io;
19
20 import static org.junit.Assert.*;
21
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24
25 import org.apache.hadoop.hbase.testclassification.SmallTests;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29
30 @Category(SmallTests.class)
31 public class TestByteBufferOutputStream {
32 @Test
33 public void testByteBufferReuse() throws IOException {
34 byte [] someBytes = Bytes.toBytes("some bytes");
35 ByteBuffer bb = ByteBuffer.allocate(someBytes.length);
36 ByteBuffer bbToReuse = write(bb, someBytes);
37 bbToReuse = write(bbToReuse, Bytes.toBytes("less"));
38 assertTrue(bb == bbToReuse);
39 }
40
41 private ByteBuffer write(final ByteBuffer bb, final byte [] bytes) throws IOException {
42 try (ByteBufferOutputStream bbos = new ByteBufferOutputStream(bb)) {
43 bbos.write(bytes);
44 assertTrue(Bytes.compareTo(bytes, bbos.toByteArray(0, bytes.length)) == 0);
45 bbos.flush();
46 return bbos.getByteBuffer();
47 }
48 }
49 }