/** * Returns a new {@link ByteArrayDataInput} instance to read from the {@code bytes} array from the * beginning. */ public static ByteArrayDataInput newDataInput(byte[] bytes) { return newDataInput(new ByteArrayInputStream(bytes)); }
/** * Returns a new {@link ByteArrayDataInput} instance to read from the {@code bytes} array, * starting at the given position. * * @throws IndexOutOfBoundsException if {@code start} is negative or greater than the length of * the array */ public static ByteArrayDataInput newDataInput(byte[] bytes, int start) { checkPositionIndex(start, bytes.length); return newDataInput(new ByteArrayInputStream(bytes, start, bytes.length - start)); }
public void testNewDataInput_readUTF() { byte[] data = new byte[17]; data[1] = 15; System.arraycopy("Kilroy was here".getBytes(Charsets.UTF_8), 0, data, 2, 15); ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals("Kilroy was here", in.readUTF()); }
public void testNewDataInput_readDouble() { byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10}; ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals(Double.longBitsToDouble(0x1234567876543210L), in.readDouble(), 0.0); }
public void testNewDataInput_BAIS() { ByteArrayInputStream bais = new ByteArrayInputStream(new byte[] {0x12, 0x34, 0x56, 0x78}); ByteArrayDataInput in = ByteStreams.newDataInput(bais); assertEquals(0x12345678, in.readInt()); }
public void testNewDataInput_empty() { byte[] b = new byte[0]; ByteArrayDataInput in = ByteStreams.newDataInput(b); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
public void testNewDataInput_readFully() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); byte[] actual = new byte[bytes.length]; in.readFully(actual); assertEquals(bytes, actual); }
public void testNewDataInput_readLong() { byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10}; ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals(0x1234567876543210L, in.readLong()); }
public void testNewDataInput_readBoolean() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); assertTrue(in.readBoolean()); }
public void testNewDataInput_readFloat() { byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10}; ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals(Float.intBitsToFloat(0x12345678), in.readFloat(), 0.0); assertEquals(Float.intBitsToFloat(0x76543210), in.readFloat(), 0.0); }
public void testNewDataInput_readFullyAndThenSome() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); byte[] actual = new byte[bytes.length * 2]; try { in.readFully(actual); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
public void testNewDataInput_skip() { ByteArrayDataInput in = ByteStreams.newDataInput(new byte[2]); assertEquals(2, in.skipBytes(2)); assertEquals(0, in.skipBytes(1)); }
public void testNewDataInput_offset() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes, 2); assertEquals(0x56787654, in.readInt()); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
public void testNewDataInput_readChar() { byte[] data = "qed".getBytes(Charsets.UTF_16BE); ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals('q', in.readChar()); assertEquals('e', in.readChar()); assertEquals('d', in.readChar()); }
public void testNewDataInput_readByte() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); for (int i = 0; i < bytes.length; i++) { assertEquals(bytes[i], in.readByte()); } try { in.readByte(); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
public void testNewDataInput_readUnsignedByte() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); for (int i = 0; i < bytes.length; i++) { assertEquals(bytes[i], in.readUnsignedByte()); } try { in.readUnsignedByte(); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
public void testNewDataInput_readFullyWithOffset() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); byte[] actual = new byte[4]; in.readFully(actual, 2, 2); assertEquals(0, actual[0]); assertEquals(0, actual[1]); assertEquals(bytes[0], actual[2]); assertEquals(bytes[1], actual[3]); }
public void testNewDataInput_readLine() { ByteArrayDataInput in = ByteStreams.newDataInput( "This is a line\r\nThis too\rand this\nand also this".getBytes(Charsets.UTF_8)); assertEquals("This is a line", in.readLine()); assertEquals("This too", in.readLine()); assertEquals("and this", in.readLine()); assertEquals("and also this", in.readLine()); }
public void testNewDataInput_normal() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); assertEquals(0x12345678, in.readInt()); assertEquals(0x76543210, in.readInt()); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
public void testNewDataInput_readUnsignedShort() { byte[] data = {0, 0, 0, 1, (byte) 0xFF, (byte) 0xFF, 0x12, 0x34}; ByteArrayDataInput in = ByteStreams.newDataInput(data); assertEquals(0, in.readUnsignedShort()); assertEquals(1, in.readUnsignedShort()); assertEquals(65535, in.readUnsignedShort()); assertEquals(0x1234, in.readUnsignedShort()); }