### Question:
UsbFileInputStream extends InputStream { @Override public int read() throws IOException { if(currentByteOffset >= file.getLength()) { return -1; } ByteBuffer buffer = ByteBuffer.allocate(512); buffer.limit(1); file.read(currentByteOffset, buffer); currentByteOffset++; buffer.flip(); return buffer.get(); } UsbFileInputStream(UsbFile file); @Override int available(); @Override int read(); @Override void close(); @Override int read(byte[] buffer); @Override int read(byte[] buffer, int byteOffset, int byteCount); @Override long skip(long byteCount);  }### Answer:
@Test public void readMoreThenLength() throws Exception { InputStream is = new UsbFileInputStream(file); int read = is.read(new byte[200]); assertEquals(123, read); read = is.read(new byte[200]); assertEquals(-1, read); }
@Test public void readInt() throws Exception { InputStream is = new UsbFileInputStream(file); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ByteBuffer buffer = invocation.getArgument(1); buffer.put((byte) 1); return null; } }).when(file).read(any(Long.class), any(ByteBuffer.class)); for (int i = 0; i < 20; i++) { int b = is.read(); assertEquals(1, b); verify(file, times(i + 1)).read(longCaptor.capture(), byteBufferCaptor.capture()); assertEquals(i, longCaptor.getValue().longValue()); assertNotNull(byteBufferCaptor.getValue()); } }
@Test public void read() throws Exception { InputStream is = new UsbFileInputStream(file); is.read(new byte[50]); verify(file).read(longCaptor.capture(), byteBufferCaptor.capture()); assertEquals(0, longCaptor.getValue().longValue()); assertEquals(50, byteBufferCaptor.getValue().remaining()); is.read(new byte[50]); verify(file, times(2)).read(longCaptor.capture(), byteBufferCaptor.capture()); assertEquals(50, longCaptor.getValue().longValue()); assertEquals(50, byteBufferCaptor.getValue().remaining()); }
@Test public void readOffset() throws Exception { InputStream is = new UsbFileInputStream(file); is.read(new byte[50], 0, 50); verify(file).read(longCaptor.capture(), byteBufferCaptor.capture()); assertEquals(0, longCaptor.getValue().longValue()); assertEquals(50, byteBufferCaptor.getValue().remaining()); is.read(new byte[50], 10, 30); verify(file, times(2)).read(longCaptor.capture(), byteBufferCaptor.capture()); assertEquals(50, longCaptor.getValue().longValue()); assertEquals(10, byteBufferCaptor.getValue().position()); assertEquals(30, byteBufferCaptor.getValue().remaining()); }