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