EndOfTransmissionInputStream extends InputStream { @Override public int read() throws IOException { if (readEOT) { readEOT = false; return -1; } int l = in.read(); if (l == EOT) { readEOT = true; } return l; } EndOfTransmissionInputStream(InputStream in); @Override int read();  }
@Test public void readOneByte() throws IOException { int c = -1; while ((c = in.read()) != -1) { buf[bytesRead++] = (byte) c; } Assert.assertArrayEquals(firstBytes, Arrays.copyOf(buf, bytesRead)); buf = new byte[256]; bytesRead = 0; c = -1; while ((c = in.read()) != -1) { buf[bytesRead++] = (byte) c; } Assert.assertArrayEquals(secondBytes, Arrays.copyOf(buf, bytesRead)); }
@Test public void readBytes() throws IOException { bytesRead = in.read(buf); Assert.assertEquals(7, bytesRead); Assert.assertArrayEquals(firstBytes, Arrays.copyOf(buf, bytesRead)); bytesRead = in.read(buf); Assert.assertEquals(5, bytesRead); Assert.assertArrayEquals(secondBytes, Arrays.copyOf(buf, bytesRead)); }
@Test public void readBytesWithOffsetAndLength() throws IOException { bytesRead = in.read(buf, 2, 23); Assert.assertEquals(7, bytesRead); Assert.assertArrayEquals(firstBytes, Arrays.copyOfRange(buf, 2, 2 + bytesRead)); bytesRead = in.read(buf, 13, 45); Assert.assertEquals(5, bytesRead); Assert.assertArrayEquals(secondBytes, Arrays.copyOfRange(buf, 13, 13 + bytesRead)); }