@Override public final int read(final byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
@Override public final int read(final byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
@Override public final int read(final byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
@Test(dataProvider = "ReadBytesTestData") public void testReadBytes(final int byteReadSize, final int bufsize) throws Exception { final byte[] bytes = new byte[255]; for ( int i = 0; i < bytes.length; i++ ) bytes[i] = (byte)i; final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); final byte[] readBytes = new byte[byteReadSize]; final PositionalBufferedStream pbs = new PositionalBufferedStream(bais, bufsize); int i = 0; while ( i < 255 ) { final int expectedBytesToRead = Math.min(255 - i, readBytes.length); final int nBytesRead = pbs.read(readBytes); Assert.assertEquals(nBytesRead, expectedBytesToRead, "Didn't read as many bytes as expected from PBS"); for ( int j = 0; j < nBytesRead; j++ ) Assert.assertEquals(readBytes[j], bytes[i+j], "Bytes read not those expected"); i += nBytesRead; } } }
@Test public void testIsDone() throws Exception{ PositionalBufferedStream is = new PositionalBufferedStream(FileIs); while(!is.isDone()){ is.read(); } Assert.assertTrue(is.isDone()); Assert.assertEquals(is.getPosition(), expectedBytes); }
@Test public void testPeek() throws Exception{ int trials = 10; PositionalBufferedStream is = new PositionalBufferedStream(FileIs); int bb = is.peek(); for(int ii=0; ii < trials; ii++){ Assert.assertEquals(is.peek(), bb); Assert.assertEquals(is.getPosition(), 0); } while((bb = is.peek()) >= 0){ Assert.assertEquals(is.read(), bb); } }
@Test public void testSkip() throws IOException { for ( int skipSizeBase : Arrays.asList(0, 10, 100, 1000, 10000, 1000000)) { for ( int skipSizeAdd = 0; skipSizeAdd < 10; skipSizeAdd ++ ) { final int skipSize = skipSizeBase + skipSizeAdd; final byte[] bytes = new byte[skipSize+2]; Arrays.fill(bytes, 0, skipSize, (byte)0); bytes[skipSize] = 1; bytes[skipSize+1] = 2; final InputStream is = new ByteArrayInputStream(bytes); final PositionalBufferedStream pbs = new PositionalBufferedStream(is); pbs.skip(skipSize); // first value is 1 Assert.assertTrue(! pbs.isDone()); Assert.assertEquals(pbs.getPosition(), skipSize); Assert.assertEquals(pbs.peek(), 1); Assert.assertEquals(pbs.read(), 1); Assert.assertTrue(! pbs.isDone()); Assert.assertEquals(pbs.getPosition(), skipSize + 1); Assert.assertEquals(pbs.peek(), 2); Assert.assertEquals(pbs.read(), 2); Assert.assertTrue(pbs.isDone()); } } }
private void testStream(final byte[] bytes) throws IOException { final InputStream is = new ByteArrayInputStream(bytes); final PositionalBufferedStream pbs = new PositionalBufferedStream(is); int bytePos = 0; while ( ! pbs.isDone() ) { Assert.assertTrue(bytePos < bytes.length); // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test peek final byte atPos = bytes[bytePos]; Assert.assertEquals(toByte(pbs.peek()), atPos); // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test read Assert.assertEquals(toByte(pbs.read()), atPos); bytePos++; // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test repeek if ( bytePos < bytes.length ) { Assert.assertEquals(toByte(pbs.peek()), bytes[bytePos]); // test position Assert.assertEquals(pbs.getPosition(), bytePos); } } Assert.assertEquals(bytePos, bytes.length); pbs.close(); }
if ( inputStream.read(headerBytes) != headerSizeInBytes ) error("Couldn't read all of the bytes specified in the header length = " + headerSizeInBytes);
if ( inputStream.read(headerBytes) != headerSizeInBytes ) error("Couldn't read all of the bytes specified in the header length = " + headerSizeInBytes);
if ( inputStream.read(headerBytes) != headerSizeInBytes ) error("Couldn't read all of the bytes specified in the header length = " + headerSizeInBytes);