Package org.apache.tika.utils
Class RereadableInputStream
java.lang.Object
java.io.InputStream
org.apache.tika.utils.RereadableInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
Wraps an input stream, reading it only once, but making it available
for rereading an arbitrary number of times. The stream's bytes are
stored in memory up to a user specified maximum, and then stored in a
temporary file which is deleted when this class's close() method is called.
-
Constructor Summary
ConstructorDescriptionRereadableInputStream
(InputStream inputStream) Creates a rereadable input stream with defaults of 512*1024*1024 bytes (500M) for maxBytesInMemory and both readToEndOfStreamOnFirstRewind and closeOriginalStreamOnClose set to trueRereadableInputStream
(InputStream inputStream, boolean closeOriginalStreamOnClose) Creates a rereadable input stream defaulting to 512*1024*1024 bytes (500M) for maxBytesInMemoryRereadableInputStream
(InputStream inputStream, int maxBytesInMemory) Creates a rereadable input stream with closeOriginalStreamOnClose set to trueRereadableInputStream
(InputStream inputStream, int maxBytesInMemory, boolean closeOriginalStreamOnClose) Creates a rereadable input stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the input stream and removes the temporary file if one was created.int
read()
Reads a byte from the stream, saving it in the store if it is being read from the original stream.void
rewind()
"Rewinds" the stream to the beginning for rereading.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
Constructor Details
-
RereadableInputStream
Creates a rereadable input stream with defaults of 512*1024*1024 bytes (500M) for maxBytesInMemory and both readToEndOfStreamOnFirstRewind and closeOriginalStreamOnClose set to true- Parameters:
inputStream
- stream containing the source of data
-
RereadableInputStream
Creates a rereadable input stream defaulting to 512*1024*1024 bytes (500M) for maxBytesInMemory- Parameters:
inputStream
- stream containing the source of data
-
RereadableInputStream
Creates a rereadable input stream with closeOriginalStreamOnClose set to true- Parameters:
inputStream
- stream containing the source of datamaxBytesInMemory
- maximum number of bytes to use to store the stream's contents in memory before switching to disk; note that the instance will preallocate a byte array whose size is maxBytesInMemory. This byte array will be made available for garbage collection (i.e. its reference set to null) when the content size exceeds the array's size, when close() is called, or when there are no more references to the instance.
-
RereadableInputStream
public RereadableInputStream(InputStream inputStream, int maxBytesInMemory, boolean closeOriginalStreamOnClose) Creates a rereadable input stream.- Parameters:
inputStream
- stream containing the source of datamaxBytesInMemory
- maximum number of bytes to use to store the stream's contents in memory before switching to disk; note that the instance will preallocate a byte array whose size is maxBytesInMemory. This byte array will be made available for garbage collection (i.e. its reference set to null) when the content size exceeds the array's size, when close() is called, or when there are no more references to the instance.
-
-
Method Details
-
read
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().- Specified by:
read
in classInputStream
- Returns:
- the read byte, or -1 on end of stream.
- Throws:
IOException
-
rewind
"Rewinds" the stream to the beginning for rereading.- Throws:
IOException
-
close
Closes the input stream and removes the temporary file if one was created.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-