View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.io;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.DataInputStream;
24  import java.io.DataOutputStream;
25  import java.nio.ByteBuffer;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestByteBufferInputStream {
34  
35    @Test
36    public void testReads() throws Exception {
37      ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
38      DataOutputStream dos = new DataOutputStream(bos);
39      String s = "test";
40      int i = 128;
41      dos.write(1);
42      dos.writeInt(i);
43      dos.writeBytes(s);
44      dos.writeLong(12345L);
45      dos.writeShort(2);
46      dos.flush();
47      ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
48  
49      // bbis contains 19 bytes
50      // 1 byte, 4 bytes int, 4 bytes string, 8 bytes long and 2 bytes short
51      ByteBufferInputStream bbis = new ByteBufferInputStream(bb);
52      assertEquals(15 + s.length(), bbis.available());
53      assertEquals(1, bbis.read());
54      byte[] ib = new byte[4];
55      bbis.read(ib);
56      assertEquals(i, Bytes.toInt(ib));
57      byte[] sb = new byte[s.length()];
58      bbis.read(sb);
59      assertEquals(s, Bytes.toString(sb));
60      byte[] lb = new byte[8];
61      bbis.read(lb);
62      assertEquals(12345, Bytes.toLong(lb));
63      assertEquals(2, bbis.available());
64      ib = new byte[4];
65      int read = bbis.read(ib, 0, ib.length);
66      // We dont have 4 bytes remainig but only 2. So onlt those should be returned back
67      assertEquals(2, read);
68      assertEquals(2, Bytes.toShort(ib));
69      assertEquals(0, bbis.available());
70      // At end. The read() should return -1
71      assertEquals(-1, bbis.read());
72      bbis.close();
73  
74      bb = ByteBuffer.wrap(bos.toByteArray());
75      bbis = new ByteBufferInputStream(bb);
76      DataInputStream dis = new DataInputStream(bbis);
77      dis.read();
78      assertEquals(i, dis.readInt());
79      dis.close();
80    }
81  }