DataSourceInputStream extends InputStream { @Override public int read() throws IOException { int length = read(singleByteArray); return length == -1 ? -1 : (singleByteArray[0] & 0xFF); } DataSourceInputStream(DataSource dataSource, DataSpec dataSpec); long bytesRead(); void open(); @Override int read(); @Override int read(@NonNull byte[] buffer); @Override int read(@NonNull byte[] buffer, int offset, int length); @Override void close();  }
@Test public void testRead() throws IOException { DataSourceInputStream inputStream = buildTestInputStream(); byte[] readBytes = new byte[TEST_DATA.length]; int totalBytesRead = 0; while (totalBytesRead < TEST_DATA.length) { int bytesRead = inputStream.read(readBytes, totalBytesRead, TEST_DATA.length - totalBytesRead); assertThat(bytesRead).isGreaterThan(0); totalBytesRead += bytesRead; assertThat(inputStream.bytesRead()).isEqualTo(totalBytesRead); } assertThat(readBytes).isEqualTo(TEST_DATA); assertThat(inputStream.bytesRead()).isEqualTo(TEST_DATA.length); assertThat(totalBytesRead).isEqualTo(TEST_DATA.length); assertThat(inputStream.read()).isEqualTo(-1); inputStream.close(); }