Class TikaInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
This implementation uses backing strategies to handle different input types:
ByteArraySourcefor byte[] inputs - no caching neededFileSourcefor Path/File inputs - direct file accessCachingSourcefor InputStream inputs - passthrough by default; caches bytes only afterenableRewind()
- Since:
- Apache Tika 0.8
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.commons.io.input.ProxyInputStream
org.apache.commons.io.input.ProxyInputStream.AbstractBuilder<T,B extends org.apache.commons.io.build.AbstractStreamBuilder<T, B>> -
Field Summary
Fields inherited from class java.io.FilterInputStream
in -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCloseableResource(Closeable closeable) protected voidafterRead(int n) voidclose()voidEnables full rewind capability for this stream.voidenableRewind(CacheMemoryBudget budget) LikeenableRewind(), but supplies a sharedCacheMemoryBudgetgoverning how much may be held in memory before spilling to disk (used only by stream-backed sources);nullfalls back to the per-object default.static TikaInputStreamget(byte[] data) static TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamget(InputStream stream) static TikaInputStreamget(InputStream stream, TemporaryResources tmp, Metadata metadata) static TikaInputStreamget(InputStream stream, Metadata metadata) static TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamget(Path path, Metadata metadata, TemporaryResources tmp) static TikaInputStreamstatic TikaInputStreamstatic TikaInputStreamget(org.apache.commons.io.function.IOSupplier<InputStream> opener, TemporaryResources tmp, Metadata metadata) Creates a TikaInputStream from a re-openable stream supplier.getFile()static TikaInputStreamgetFromContainer(Object openContainer, long length, Metadata metadata) longThe stream length.getPath()static TikaInputStreamAn empty stream standing in for content that is never extracted -- a metadata-only entry, a rendering carried as an open container.longReturns a read-only random-accessSeekableByteChannelover this stream's full content.booleanhasFile()Whether the content is already on disk: a real file, or a stream cache that spilled.booleanbooleanTrue whengetLength()would return a measured, ground-truth length (file, byte array, fully-drained cache, explicit override) without forcing a spool.static ByteBufferinMemoryContent(SeekableByteChannel channel) Zero-copy, read-only view of the content behind a channel fromgetSeekableByteChannel(), ornullwhen that content is on disk.booleanvoidmark(int readlimit) booleanintpeek(byte[] buffer) voidvoidreset()voidrewind()Rewind the stream to the beginning.voidvoidsetOpenContainer(Object container) longskip(long n) Skips up tonbytes.toString()booleanAsks the underlying source to hold its full content in memory, so a later rewind and re-read does not go back to the source.Methods inherited from class org.apache.commons.io.input.TaggedInputStream
handleIOException, isCauseOf, throwIfCauseOfMethods inherited from class org.apache.commons.io.input.ProxyInputStream
available, beforeRead, read, read, read, setReference, unwrapMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Method Details
-
get
-
get
public static TikaInputStream get(org.apache.commons.io.function.IOSupplier<InputStream> opener, TemporaryResources tmp, Metadata metadata) Creates a TikaInputStream from a re-openable stream supplier. Unlikeget(InputStream, TemporaryResources, Metadata)-- which caches a one-shot stream to memory/disk so it can be rewound -- the supplier is re-invoked to re-read the content, so rewinding (e.g. during digesting) never spills to disk. A temp file is created only ifgetPath()is later called (a parser/detector needing a File) orgetSeekableByteChannel()is asked for content that does not fit in memory.- Parameters:
opener- supplies a fresh InputStream over the same content on each calltmp- temporary resources for any on-demandgetPath()spillmetadata- metadata used for extension/length hints; may be null
-
get
-
get
-
get
-
get
-
getPlaceholder
An empty stream standing in for content that is never extracted -- a metadata-only entry, a rendering carried as an open container. It reports an unknown length, so nothing mistakes the placeholder's size for the document's. Pair it withMetadataOnlyParseto register an entry without parsing it, unless an open container supplies the content. -
get
- Throws:
IOException
-
get
- Throws:
IOException
-
get
public static TikaInputStream get(Path path, Metadata metadata, TemporaryResources tmp) throws IOException - Throws:
IOException
-
get
- Throws:
IOException
-
get
- Throws:
IOException
-
get
- Throws:
SQLExceptionIOException
-
get
- Throws:
SQLExceptionIOException
-
get
- Throws:
IOException
-
get
- Throws:
IOException
-
get
- Throws:
IOException
-
get
- Throws:
IOException
-
getFromContainer
public static TikaInputStream getFromContainer(Object openContainer, long length, Metadata metadata) -
skip
Skips up tonbytes. Returns the actual number of bytes skipped, which may be less than requested if the end of stream is reached.This method does NOT throw
EOFExceptionif fewer bytes are available. Callers must check the return value to determine how many bytes were actually skipped.- Overrides:
skipin classorg.apache.commons.io.input.ProxyInputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped (may be less than
n) - Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
markin classorg.apache.commons.io.input.ProxyInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classorg.apache.commons.io.input.ProxyInputStream
-
reset
- Overrides:
resetin classorg.apache.commons.io.input.ProxyInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classorg.apache.commons.io.input.ProxyInputStream- Throws:
IOException
-
afterRead
- Overrides:
afterReadin classorg.apache.commons.io.input.ProxyInputStream- Throws:
IOException
-
peek
- Throws:
IOException
-
getOpenContainer
-
setOpenContainer
-
addCloseableResource
-
hasFile
public boolean hasFile()Whether the content is already on disk: a real file, or a stream cache that spilled.getPath()then returns that file without re-copying anything already written to it -- but it is not free, and it is not a getter: for a cache that spilled mid-stream it first drains the rest of the source into the file, switches this stream to reading from that file, and setsContent-Lengthon the Metadata this stream was created with. UsehasLength()if you only need the size. -
getPath
- Throws:
IOException
-
getFile
- Throws:
IOException
-
getFileChannel
- Throws:
IOException
-
hasLength
public boolean hasLength() -
hasReliableLength
public boolean hasReliableLength()True whengetLength()would return a measured, ground-truth length (file, byte array, fully-drained cache, explicit override) without forcing a spool. False when the only length available is a caller-declared hint (Content-Length metadata, HTTP header, archive central directory), which may lie. -
getLength
The stream length. For a stream-backed instance with no declared length this spools the entire remaining stream to a temporary file to measure it.- Throws:
IOException
-
getPosition
public long getPosition() -
setCloseShield
public void setCloseShield() -
removeCloseShield
public void removeCloseShield() -
isCloseShield
public boolean isCloseShield() -
rewind
Rewind the stream to the beginning.For streams created from byte arrays or files, this always works. For streams created from raw InputStreams, this requires
enableRewind()to have been called first.- Throws:
IOException
-
enableRewind
Enables full rewind capability for this stream.For streams backed by byte arrays or files, this is a no-op since they are inherently rewindable. For streams backed by raw InputStreams, this switches from passthrough mode to caching mode, enabling subsequent
rewind(),mark(int)/reset(), and random access.Must be called when position is 0 (before any reading), otherwise throws IOException.
Use this method when you know you'll need to rewind the stream later (e.g., for detection followed by parsing, or digest calculation). For streaming-only operations (e.g., HTML parsing), skip this call to avoid unnecessary caching overhead.
- Throws:
IOException- if bytes have already been read from the stream (position is not 0); rewind support cannot be enabled retroactively
-
enableRewind
LikeenableRewind(), but supplies a sharedCacheMemoryBudgetgoverning how much may be held in memory before spilling to disk (used only by stream-backed sources);nullfalls back to the per-object default.- Parameters:
budget- shared memory budget, ornull- Throws:
IOException- if bytes have already been read (position is not 0)
-
getSeekableByteChannel
Returns a read-only random-accessSeekableByteChannelover this stream's full content. UnlikegetPath()/getFile(), this never forces content that is already in memory onto disk: in-memory content is served from memory, file-backed or spilled content from a file channel, and unread stream content is drained through the cache which decides memory-vs-disk as it goes. Use this when random access is needed (e.g. reading a zip central directory); reservegetFile()for callers that truly need aFile. Closed with the stream if the caller does not close it first. Does not disturb this stream's read position.- Throws:
IOException- if this stream has been partially read without rewind enabled
-
tryRetainInMemory
Asks the underlying source to hold its full content in memory, so a later rewind and re-read does not go back to the source. Worth calling before a pass that will be followed by another (digest then parse) when re-opening is expensive -- a zip entry has to be inflated again, where a file is served by the page cache.Advisory: sources that would have to spill, or cannot tell whether the content fits, return false and behave as before. Does not change the read position.
- Returns:
- true if the full content is now held in memory
- Throws:
IOException- See Also:
-
inMemoryContent
Zero-copy, read-only view of the content behind a channel fromgetSeekableByteChannel(), ornullwhen that content is on disk. Lets a consumer that wants random access (PDFBox, metadata-extractor) read what is already in memory without a second copy; when this returns null the caller should use the file.The view aliases the cache's own array and is valid exactly while
channelis open: the channel pins the array, and the content is fully drained before any channel is handed out. Keep the channel open for as long as the view is in use, then close it -- a view that outlives its channel still reads correctly but is no longer counted against the memory budget.- Throws:
IOException
-
toString
-