RandomAccessFileInputStream extends InputStream { @Override public int read() throws IOException { if (available() <= 0) { return -1; } return raf.read(); } RandomAccessFileInputStream(File file);  RandomAccessFileInputStream(File file, long rangeOffset, long rangeLength); void setRange(long rangeOffset, long rangeLength); @Override int available(); long getPosition(); @Override void close(); @Override int read(); @Override int read(byte[] b, int off, int len); @Override int read(byte[] b); @Override long skip(long n); @Override boolean markSupported(); @Override void mark(int readlimit); @Override void reset(); long length();  }
@Test public void testRead_OneByte() throws IOException { for (int x = 0; x < testData.length; x++) { Assert.assertEquals(x, stream.read()); } Assert.assertEquals(-1, stream.read()); }
@Test public void testRead_WithBuffer() throws IOException { int bytesLeft = testData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[10]; while (bytesLeft > 0) { int numRead = stream.read(buffer); if (numRead > 0) { bytesLeft -= numRead; out.write(buffer, 0, numRead); } } Assert.assertEquals(-1, stream.read(buffer, 0, 1)); Assert.assertArrayEquals(testData, out.toByteArray()); }
@Test public void testRead_WithBuffer_NegativeLength() throws IOException { Assert.assertEquals(0, stream.read(new byte[] {}, 0, -1)); }
@Test public void testRead_WithPartialBuffer() throws IOException { int bytesLeft = testData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[10]; while (bytesLeft > 0) { int numRead = stream.read(buffer, 0, 2); if (numRead > 0) { bytesLeft -= numRead; out.write(buffer, 0, numRead); } } Assert.assertEquals(-1, stream.read()); Assert.assertArrayEquals(testData, out.toByteArray()); }
@Test public void testConstructorWithSpecificLength() throws IOException { stream = new RandomAccessFileInputStream(tempFile, 5, 2); Assert.assertEquals(5, stream.read()); Assert.assertEquals(6, stream.read()); Assert.assertEquals(-1, stream.read()); }