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; 021 022import java.io.ByteArrayOutputStream; 023import java.io.DataInputStream; 024import java.io.DataOutputStream; 025import java.nio.ByteBuffer; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.nio.MultiByteBuff; 028import org.apache.hadoop.hbase.testclassification.IOTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 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 TestMultiByteBuffInputStream { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestMultiByteBuffInputStream.class); 041 042 @Test 043 public void testReads() throws Exception { 044 ByteArrayOutputStream bos = new ByteArrayOutputStream(100); 045 DataOutputStream dos = new DataOutputStream(bos); 046 String s = "test"; 047 int i = 128; 048 dos.write(1); 049 dos.writeInt(i); 050 dos.writeBytes(s); 051 dos.writeLong(12345L); 052 dos.writeShort(2); 053 dos.flush(); 054 ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray()); 055 056 // bbis contains 19 bytes 057 // 1 byte, 4 bytes int, 4 bytes string, 8 bytes long and 2 bytes short 058 ByteBuffInputStream bbis = new ByteBuffInputStream(new MultiByteBuff(bb)); 059 assertEquals(15 + s.length(), bbis.available()); 060 assertEquals(1, bbis.read()); 061 byte[] ib = new byte[4]; 062 bbis.read(ib); 063 assertEquals(i, Bytes.toInt(ib)); 064 byte[] sb = new byte[s.length()]; 065 bbis.read(sb); 066 assertEquals(s, Bytes.toString(sb)); 067 byte[] lb = new byte[8]; 068 bbis.read(lb); 069 assertEquals(12345, Bytes.toLong(lb)); 070 assertEquals(2, bbis.available()); 071 ib = new byte[4]; 072 int read = bbis.read(ib, 0, ib.length); 073 // We dont have 4 bytes remainig but only 2. So onlt those should be returned back 074 assertEquals(2, read); 075 assertEquals(2, Bytes.toShort(ib)); 076 assertEquals(0, bbis.available()); 077 // At end. The read() should return -1 078 assertEquals(-1, bbis.read()); 079 bbis.close(); 080 081 bb = ByteBuffer.wrap(bos.toByteArray()); 082 bbis = new ByteBuffInputStream(new MultiByteBuff(bb)); 083 DataInputStream dis = new DataInputStream(bbis); 084 dis.read(); 085 assertEquals(i, dis.readInt()); 086 dis.close(); 087 } 088}