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)); }
/** * Reads a byte from the stream, saving it in the store if it is being * read from the original stream. Implements the abstract * InputStream.read(). * * @return the read byte, or -1 on end of stream. * @throws IOException */ public int read() throws IOException { int inputByte = inputStream.read(); if (firstPass) { saveByte(inputByte); } return inputByte; }
@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(); } } }
@Override public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { //Try to parse TSD file try (RereadableInputStream ris = new RereadableInputStream(stream, 2048, true, true)) { Metadata TSDAndEmbeddedMetadata = new Metadata(); List<TSDMetas> tsdMetasList = this.extractMetas(ris); this.buildMetas(tsdMetasList, metadata != null && metadata.size() > 0 ? TSDAndEmbeddedMetadata : metadata); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); ris.rewind(); //Try to parse embedded file in TSD file this.parseTSDContent(ris, handler, TSDAndEmbeddedMetadata, context); xhtml.endDocument(); } }
private void doACloseBehaviorTest(boolean wantToClose) throws IOException { TestInputStream tis = createTestInputStream(); RereadableInputStream ris = new RereadableInputStream(tis, 5, true, wantToClose); ris.close(); assertEquals(wantToClose, tis.isClosed()); if (! tis.isClosed()) { tis.close(); } }
/** * Resets the given {@link TikaInputStream} (checked by * {@link #ensureStreamReReadable(InputStream, TemporaryResources)}) * so that it can be re-read again. */ public static InputStream streamResetForReRead(InputStream stream, TemporaryResources tmp) throws IOException { // If re-readable, rewind to start if (stream instanceof RereadableInputStream) { ((RereadableInputStream)stream).rewind(); return stream; } // File or Factory based? TikaInputStream tstream = (TikaInputStream)stream; if (tstream.getInputStreamFactory() != null) { // Just get a fresh one each time from the factory return TikaInputStream.get(tstream.getInputStreamFactory(), tmp); } // File based, reset stream to beginning of File tstream.reset(); tstream.mark(-1); return tstream; } }
/** * Closes the input stream and removes the temporary file if one was * created. * * @throws IOException */ public void close() throws IOException { closeStream(); super.close(); if (storeFile != null) { storeFile.delete(); } }
@Override public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { //Try to parse TSD file try (RereadableInputStream ris = new RereadableInputStream(stream, 2048, true, true)) { Metadata TSDAndEmbeddedMetadata = new Metadata(); List<TSDMetas> tsdMetasList = this.extractMetas(ris); this.buildMetas(tsdMetasList, metadata != null && metadata.size() > 0 ? TSDAndEmbeddedMetadata : metadata); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); ris.rewind(); //Try to parse embedded file in TSD file this.parseTSDContent(ris, handler, TSDAndEmbeddedMetadata, context); xhtml.endDocument(); } }
/** * Closes the input stream and removes the temporary file if one was * created. * * @throws IOException */ public void close() throws IOException { closeStream(); super.close(); if (storeFile != null) { storeFile.delete(); } }
/** * "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)); }
/** * Closes the input stream and removes the temporary file if one was * created. * * @throws IOException */ public void close() throws IOException { closeStream(); super.close(); if (storeFile != null) { storeFile.delete(); } }
/** * Reads a byte from the stream, saving it in the store if it is being * read from the original stream. Implements the abstract * InputStream.read(). * * @return the read byte, or -1 on end of stream. * @throws IOException */ public int read() throws IOException { int inputByte = inputStream.read(); if (firstPass) { saveByte(inputByte); } return inputByte; }
/** * "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)); }
/** * Reads a byte from the stream, saving it in the store if it is being * read from the original stream. Implements the abstract * InputStream.read(). * * @return the read byte, or -1 on end of stream. * @throws IOException */ public int read() throws IOException { int inputByte = inputStream.read(); if (firstPass) { saveByte(inputByte); } return inputByte; }