/** * "Rewinds" the stream to the beginning for rereading. * @throws IOException */ public void rewind() throws IOException { if (firstPass && readToEndOfStreamOnFirstRewind) { // Force read to end of stream to fill store with any // remaining bytes from original stream. while(read() != -1) { // empty loop } } closeStream(); if (storeOutputStream != null) { storeOutputStream.close(); storeOutputStream = null; } firstPass = false; boolean newStreamIsInMemory = (size < maxBytesInMemory); inputStream = newStreamIsInMemory ? new ByteArrayInputStream(byteBuffer) : new BufferedInputStream(new FileInputStream(storeFile)); }
@Test public void test() throws IOException { InputStream is = createTestInputStream(); try (RereadableInputStream ris = new RereadableInputStream(is, MEMORY_THRESHOLD, true, true)) { for (int pass = 0; pass < NUM_PASSES; pass++) { for (int byteNum = 0; byteNum < TEST_SIZE; byteNum++) { int byteRead = ris.read(); assertEquals("Pass = " + pass + ", byte num should be " + byteNum + " but is " + byteRead + ".", byteNum, byteRead); } ris.rewind(); } } }
private void doTestRewind(boolean readToEndOnRewind) throws IOException { RereadableInputStream ris = null; try { InputStream s1 = createTestInputStream(); ris = new RereadableInputStream(s1, 5, readToEndOnRewind, true); ris.read(); assertEquals(1, ris.getSize()); ris.rewind(); boolean moreBytesWereRead = (ris.getSize() > 1); assertEquals(readToEndOnRewind, moreBytesWereRead); } finally { if (ris != null) { ris.close(); } } }
/** * "Rewinds" the stream to the beginning for rereading. * @throws IOException */ public void rewind() throws IOException { if (firstPass && readToEndOfStreamOnFirstRewind) { // Force read to end of stream to fill store with any // remaining bytes from original stream. while(read() != -1) { // empty loop } } closeStream(); if (storeOutputStream != null) { storeOutputStream.close(); storeOutputStream = null; } firstPass = false; boolean newStreamIsInMemory = (size < maxBytesInMemory); inputStream = newStreamIsInMemory ? new ByteArrayInputStream(byteBuffer) : new BufferedInputStream(new FileInputStream(storeFile)); }
/** * "Rewinds" the stream to the beginning for rereading. * @throws IOException */ public void rewind() throws IOException { if (firstPass && readToEndOfStreamOnFirstRewind) { // Force read to end of stream to fill store with any // remaining bytes from original stream. while(read() != -1) { // empty loop } } closeStream(); if (storeOutputStream != null) { storeOutputStream.close(); storeOutputStream = null; } firstPass = false; boolean newStreamIsInMemory = (size < maxBytesInMemory); inputStream = newStreamIsInMemory ? new ByteArrayInputStream(byteBuffer) : new BufferedInputStream(new FileInputStream(storeFile)); }