Class TikaInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.tika.io.ProxyInputStream
-
- org.apache.tika.io.TaggedInputStream
-
- org.apache.tika.io.TikaInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class TikaInputStream extends TaggedInputStream
Input stream with extended capabilities. The purpose of this class is to allow files and other resources and information to be associated with theInputStream
instance passed through theParser
interface and other similar APIs.TikaInputStream instances can be created using the various static
get()
factory methods. Most of these methods take an optionalMetadata
argument that is then filled with the available input metadata from the given resource. The created TikaInputStream instance keeps track of the original resource used to create it, while behaving otherwise just like a normal, bufferedInputStream
. A TikaInputStream instance is also guaranteed to support themark(int)
feature.Code that wants to access the underlying file or other resources associated with a TikaInputStream should first use the
get(InputStream)
factory method to cast or wrap a givenInputStream
into a TikaInputStream instance.TikaInputStream includes a few safety features to protect against parsers that may fail to check for an EOF or may incorrectly rely on the unreliable value returned from
FileInputStream.skip(long)
. These parser failures can lead to infinite loops. We strongly encourage the use of TikaInputStream.- Since:
- Apache Tika 0.8
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static TikaInputStream
cast(java.io.InputStream stream)
Returns the given stream casts to a TikaInputStream, ornull
if the stream is not a TikaInputStream.void
close()
Invokes the delegate'sclose()
method.static TikaInputStream
get(byte[] data)
Creates a TikaInputStream from the given array of bytes.static TikaInputStream
get(byte[] data, Metadata metadata)
Creates a TikaInputStream from the given array of bytes.static TikaInputStream
get(java.io.File file)
Deprecated.useget(Path)
.static TikaInputStream
get(java.io.File file, Metadata metadata)
Deprecated.useget(Path, Metadata)
.static TikaInputStream
get(java.io.InputStream stream)
Casts or wraps the given stream to a TikaInputStream instance.static TikaInputStream
get(java.io.InputStream stream, TemporaryResources tmp)
Casts or wraps the given stream to a TikaInputStream instance.static TikaInputStream
get(java.net.URI uri)
Creates a TikaInputStream from the resource at the given URI.static TikaInputStream
get(java.net.URI uri, Metadata metadata)
Creates a TikaInputStream from the resource at the given URI.static TikaInputStream
get(java.net.URL url)
Creates a TikaInputStream from the resource at the given URL.static TikaInputStream
get(java.net.URL url, Metadata metadata)
Creates a TikaInputStream from the resource at the given URL.static TikaInputStream
get(java.nio.file.Path path)
Creates a TikaInputStream from the file at the given path.static TikaInputStream
get(java.nio.file.Path path, Metadata metadata)
Creates a TikaInputStream from the file at the given path.static TikaInputStream
get(java.sql.Blob blob)
Creates a TikaInputStream from the given database BLOB.static TikaInputStream
get(java.sql.Blob blob, Metadata metadata)
Creates a TikaInputStream from the given database BLOB.java.io.File
getFile()
java.nio.channels.FileChannel
getFileChannel()
long
getLength()
Returns the length (in bytes) of this stream.java.lang.Object
getOpenContainer()
Returns the open container object, such as a POIFS FileSystem in the event of an OLE2 document being detected and processed by the OLE2 detector.java.nio.file.Path
getPath()
If the user created this TikaInputStream with a file, the original file will be returned.java.nio.file.Path
getPath(int maxBytes)
long
getPosition()
Returns the current position within the stream.boolean
hasFile()
boolean
hasLength()
static boolean
isTikaInputStream(java.io.InputStream stream)
Checks whether the given stream is a TikaInputStream instance.void
mark(int readlimit)
Invokes the delegate'smark(int)
method.boolean
markSupported()
Invokes the delegate'smarkSupported()
method.int
peek(byte[] buffer)
Fills the given buffer with upcoming bytes from this stream without advancing the current stream position.void
reset()
Invokes the delegate'sreset()
method.void
setOpenContainer(java.lang.Object container)
Stores the open container object against the stream, eg after a Zip contents detector has loaded the file to decide what it contains.long
skip(long ln)
This relies onIOUtils.skip(InputStream, long)
to ensure that the alleged bytes skipped were actually skipped.java.lang.String
toString()
-
Methods inherited from class org.apache.tika.io.TaggedInputStream
isCauseOf, throwIfCauseOf
-
Methods inherited from class org.apache.tika.io.ProxyInputStream
available, read, read, read
-
-
-
-
Method Detail
-
isTikaInputStream
public static boolean isTikaInputStream(java.io.InputStream stream)
Checks whether the given stream is a TikaInputStream instance. The given stream can benull
, in which case the return value isfalse
.- Parameters:
stream
- input stream, possiblynull
- Returns:
true
if the stream is a TikaInputStream instance,false
otherwise
-
get
public static TikaInputStream get(java.io.InputStream stream, TemporaryResources tmp)
Casts or wraps the given stream to a TikaInputStream instance. This method can be used to access the functionality of this class even when given just a normal input stream instance.The given temporary file provider is used for any temporary files, and should be disposed when the returned stream is no longer used.
Use this method instead of the
get(InputStream)
alternative when you don't explicitly close the returned stream. The recommended access pattern is:try (TemporaryResources tmp = new TemporaryResources()) { TikaInputStream stream = TikaInputStream.get(..., tmp); // process stream but don't close it }
The given stream instance will not be closed when the
TemporaryResources.close()
method is called by the try-with-resources statement. The caller is expected to explicitly close the original stream when it's no longer used.- Parameters:
stream
- normal input stream- Returns:
- a TikaInputStream instance
- Since:
- Apache Tika 0.10
-
get
public static TikaInputStream get(java.io.InputStream stream)
Casts or wraps the given stream to a TikaInputStream instance. This method can be used to access the functionality of this class even when given just a normal input stream instance.Use this method instead of the
get(InputStream, TemporaryResources)
alternative when you do explicitly close the returned stream. The recommended access pattern is:try (TikaInputStream stream = TikaInputStream.get(...)) { // process stream }
The given stream instance will be closed along with any other resources associated with the returned TikaInputStream instance when the
close()
method is called by the try-with-resources statement.- Parameters:
stream
- normal input stream- Returns:
- a TikaInputStream instance
-
cast
public static TikaInputStream cast(java.io.InputStream stream)
Returns the given stream casts to a TikaInputStream, ornull
if the stream is not a TikaInputStream.- Parameters:
stream
- normal input stream- Returns:
- a TikaInputStream instance
- Since:
- Apache Tika 0.10
-
get
public static TikaInputStream get(byte[] data)
Creates a TikaInputStream from the given array of bytes.Note that you must always explicitly close the returned stream as in some cases it may end up writing the given data to a temporary file.
- Parameters:
data
- input data- Returns:
- a TikaInputStream instance
-
get
public static TikaInputStream get(byte[] data, Metadata metadata)
Creates a TikaInputStream from the given array of bytes. The length of the array is stored as input metadata in the given metadata instance.Note that you must always explicitly close the returned stream as in some cases it may end up writing the given data to a temporary file.
- Parameters:
data
- input datametadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
-
get
public static TikaInputStream get(java.nio.file.Path path) throws java.io.IOException
Creates a TikaInputStream from the file at the given path.Note that you must always explicitly close the returned stream to prevent leaking open file handles.
- Parameters:
path
- input file- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if an I/O error occurs
-
get
public static TikaInputStream get(java.nio.file.Path path, Metadata metadata) throws java.io.IOException
Creates a TikaInputStream from the file at the given path. The file name and length are stored as input metadata in the given metadata instance.Note that you must always explicitly close the returned stream to prevent leaking open file handles.
- Parameters:
path
- input filemetadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if an I/O error occurs
-
get
@Deprecated public static TikaInputStream get(java.io.File file) throws java.io.FileNotFoundException
Deprecated.useget(Path)
. In Tika 2.0, this will be removed or modified to throw an IOException.Creates a TikaInputStream from the given file.Note that you must always explicitly close the returned stream to prevent leaking open file handles.
- Parameters:
file
- input file- Returns:
- a TikaInputStream instance
- Throws:
java.io.FileNotFoundException
- if the file does not exist
-
get
@Deprecated public static TikaInputStream get(java.io.File file, Metadata metadata) throws java.io.FileNotFoundException
Deprecated.useget(Path, Metadata)
. In Tika 2.0, this will be removed or modified to throw an IOException.Creates a TikaInputStream from the given file. The file name and length are stored as input metadata in the given metadata instance.Note that you must always explicitly close the returned stream to prevent leaking open file handles.
- Parameters:
file
- input filemetadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.io.FileNotFoundException
- if the file does not exist or cannot be opened for reading
-
get
public static TikaInputStream get(java.sql.Blob blob) throws java.sql.SQLException
Creates a TikaInputStream from the given database BLOB.Note that the result set containing the BLOB may need to be kept open until the returned TikaInputStream has been processed and closed. You must also always explicitly close the returned stream as in some cases it may end up writing the blob data to a temporary file.
- Parameters:
blob
- database BLOB- Returns:
- a TikaInputStream instance
- Throws:
java.sql.SQLException
- if BLOB data can not be accessed
-
get
public static TikaInputStream get(java.sql.Blob blob, Metadata metadata) throws java.sql.SQLException
Creates a TikaInputStream from the given database BLOB. The BLOB length (if available) is stored as input metadata in the given metadata instance.Note that the result set containing the BLOB may need to be kept open until the returned TikaInputStream has been processed and closed. You must also always explicitly close the returned stream as in some cases it may end up writing the blob data to a temporary file.
- Parameters:
blob
- database BLOBmetadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.sql.SQLException
- if BLOB data can not be accessed
-
get
public static TikaInputStream get(java.net.URI uri) throws java.io.IOException
Creates a TikaInputStream from the resource at the given URI.Note that you must always explicitly close the returned stream as in some cases it may end up writing the resource to a temporary file.
- Parameters:
uri
- resource URI- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if the resource can not be accessed
-
get
public static TikaInputStream get(java.net.URI uri, Metadata metadata) throws java.io.IOException
Creates a TikaInputStream from the resource at the given URI. The available input metadata is stored in the given metadata instance.Note that you must always explicitly close the returned stream as in some cases it may end up writing the resource to a temporary file.
- Parameters:
uri
- resource URImetadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if the resource can not be accessed
-
get
public static TikaInputStream get(java.net.URL url) throws java.io.IOException
Creates a TikaInputStream from the resource at the given URL.Note that you must always explicitly close the returned stream as in some cases it may end up writing the resource to a temporary file.
- Parameters:
url
- resource URL- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if the resource can not be accessed
-
get
public static TikaInputStream get(java.net.URL url, Metadata metadata) throws java.io.IOException
Creates a TikaInputStream from the resource at the given URL. The available input metadata is stored in the given metadata instance.Note that you must always explicitly close the returned stream as in some cases it may end up writing the resource to a temporary file.
- Parameters:
url
- resource URLmetadata
- metadata instance- Returns:
- a TikaInputStream instance
- Throws:
java.io.IOException
- if the resource can not be accessed
-
peek
public int peek(byte[] buffer) throws java.io.IOException
Fills the given buffer with upcoming bytes from this stream without advancing the current stream position. The buffer is filled up unless the end of stream is encountered before that. This method will block if not enough bytes are immediately available.- Parameters:
buffer
- byte buffer- Returns:
- number of bytes written to the buffer
- Throws:
java.io.IOException
- if the stream can not be read
-
getOpenContainer
public java.lang.Object getOpenContainer()
Returns the open container object, such as a POIFS FileSystem in the event of an OLE2 document being detected and processed by the OLE2 detector.
-
setOpenContainer
public void setOpenContainer(java.lang.Object container)
Stores the open container object against the stream, eg after a Zip contents detector has loaded the file to decide what it contains.
-
hasFile
public boolean hasFile()
-
getPath
public java.nio.file.Path getPath() throws java.io.IOException
If the user created this TikaInputStream with a file, the original file will be returned. If not, the entire stream will be spooled to a temporary file which will be deleted upon the close of this TikaInputStream- Returns:
- Throws:
java.io.IOException
-
getPath
public java.nio.file.Path getPath(int maxBytes) throws java.io.IOException
- Parameters:
maxBytes
- if this is less than 0 and if an underlying file doesn't already exist, the full file will be spooled to disk- Returns:
- the original path used in the initialization of this TikaInputStream,
a temporary file if the stream was shorter than
maxBytes
, ornull
if the underlying stream was longer than maxBytes. - Throws:
java.io.IOException
-
getFile
public java.io.File getFile() throws java.io.IOException
- Throws:
java.io.IOException
- See Also:
getPath()
-
getFileChannel
public java.nio.channels.FileChannel getFileChannel() throws java.io.IOException
- Throws:
java.io.IOException
-
hasLength
public boolean hasLength()
-
getLength
public long getLength() throws java.io.IOException
Returns the length (in bytes) of this stream. Note that if the length was not available when this stream was instantiated, then this method will use thegetPath()
method to buffer the entire stream to a temporary file in order to calculate the stream length. This case will only work if the stream has not yet been consumed.- Returns:
- stream length
- Throws:
java.io.IOException
- if the length can not be determined
-
getPosition
public long getPosition()
Returns the current position within the stream.- Returns:
- stream position
-
skip
public long skip(long ln) throws java.io.IOException
This relies onIOUtils.skip(InputStream, long)
to ensure that the alleged bytes skipped were actually skipped.- Overrides:
skip
in classProxyInputStream
- Parameters:
ln
- the number of bytes to skip- Returns:
- the number of bytes skipped
- Throws:
java.io.IOException
- if the number of bytes requested to be skipped does not match the number of bytes skipped or if there's an IOException during the read.
-
mark
public void mark(int readlimit)
Description copied from class:ProxyInputStream
Invokes the delegate'smark(int)
method.- Overrides:
mark
in classProxyInputStream
- Parameters:
readlimit
- read ahead limit
-
markSupported
public boolean markSupported()
Description copied from class:ProxyInputStream
Invokes the delegate'smarkSupported()
method.- Overrides:
markSupported
in classProxyInputStream
- Returns:
- true if mark is supported, otherwise false
-
reset
public void reset() throws java.io.IOException
Description copied from class:ProxyInputStream
Invokes the delegate'sreset()
method.- Overrides:
reset
in classProxyInputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-
close
public void close() throws java.io.IOException
Description copied from class:ProxyInputStream
Invokes the delegate'sclose()
method.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classProxyInputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-
toString
public java.lang.String toString()
- Overrides:
toString
in classTaggedInputStream
-
-