@Test public void readIntoByteArrayOffsetAndCount() throws IOException { sink.writeUtf8("abcd"); byte[] sink = new byte[7]; int read = source.read(sink, 2, 3); if (factory == ONE_BYTE_AT_A_TIME_FACTORY) { assertEquals(1, read); byte[] expected = {0, 0, 'a', 0, 0, 0, 0}; assertByteArraysEquals(expected, sink); } else { assertEquals(3, read); byte[] expected = {0, 0, 'a', 'b', 'c', 0, 0}; assertByteArraysEquals(expected, sink); } }
@Test public void readIntoByteArrayNotEnough() throws IOException { sink.writeUtf8("abcd"); byte[] sink = new byte[5]; int read = source.read(sink); if (factory == ONE_BYTE_AT_A_TIME_FACTORY) { assertEquals(1, read); byte[] expected = {'a', 0, 0, 0, 0}; assertByteArraysEquals(expected, sink); } else { assertEquals(4, read); byte[] expected = {'a', 'b', 'c', 'd', 0}; assertByteArraysEquals(expected, sink); } }
@Test public void readFullyByteArray() throws IOException { Buffer data = new Buffer(); data.writeUtf8("Hello").writeUtf8(repeat('e', Segment.SIZE)); byte[] expected = data.clone().readByteArray(); sink.write(data, data.size()); byte[] sink = new byte[Segment.SIZE + 5]; source.readFully(sink); assertByteArraysEquals(expected, sink); }
@Test public void readFullyByteArrayTooShortThrows() throws IOException { sink.writeUtf8("Hello"); byte[] sink = new byte[6]; try { source.readFully(sink); fail(); } catch (EOFException ignored) { } // Verify we read all that we could from the source. assertByteArraysEquals(new byte[] {'H', 'e', 'l', 'l', 'o', 0}, sink); }
@Test public void readByteArray() throws IOException { String string = "abcd" + repeat('e', Segment.SIZE); sink.writeUtf8(string); assertByteArraysEquals(string.getBytes(UTF_8), source.readByteArray()); }