Package org.apache.pdfbox.cos
Class COSStream
- java.lang.Object
-
- org.apache.pdfbox.cos.COSBase
-
- org.apache.pdfbox.cos.COSDictionary
-
- org.apache.pdfbox.cos.COSStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,COSUpdateInfo
,COSObjectable
public class COSStream extends COSDictionary implements java.io.Closeable
This class represents a stream object in a PDF document.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
isWriting
private static org.apache.commons.logging.Log
LOG
private RandomAccess
randomAccess
private ScratchFile
scratchFile
-
Fields inherited from class org.apache.pdfbox.cos.COSDictionary
items
-
-
Constructor Summary
Constructors Constructor Description COSStream()
Creates a new stream with an empty dictionary.COSStream(ScratchFile scratchFile)
Creates a new stream with an empty dictionary.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
accept(ICOSVisitor visitor)
visitor pattern double dispatch method.private void
checkClosed()
Throws if the random access backing store has been closed.void
close()
java.io.OutputStream
createFilteredStream()
Deprecated.UsecreateRawOutputStream()
instead.COSInputStream
createInputStream()
Returns a new InputStream which reads the decoded stream data.COSInputStream
createInputStream(DecodeOptions options)
java.io.OutputStream
createOutputStream()
Returns a new OutputStream for writing stream data, using the current filters.java.io.OutputStream
createOutputStream(COSBase filters)
Returns a new OutputStream for writing stream data, using and the given filters.java.io.InputStream
createRawInputStream()
Returns a new InputStream which reads the encoded PDF stream data.java.io.OutputStream
createRawOutputStream()
Returns a new OutputStream for writing encoded PDF data.java.io.OutputStream
createUnfilteredStream()
Deprecated.UsecreateOutputStream()
instead.private void
ensureRandomAccessExists(boolean forInputStream)
java.io.InputStream
getFilteredStream()
Deprecated.UsecreateRawInputStream()
instead.private java.util.List<Filter>
getFilterList()
Returns the list of filters.COSBase
getFilters()
This will return the filters to apply to the byte stream.long
getLength()
Returns the length of the encoded stream.java.lang.String
getString()
Deprecated.UsetoTextString()
instead.java.io.InputStream
getUnfilteredStream()
Deprecated.UsecreateInputStream()
instead.void
setFilters(COSBase filters)
Deprecated.UsecreateOutputStream(COSBase)
instead.java.lang.String
toTextString()
Returns the contents of the stream as a PDF "text string".-
Methods inherited from class org.apache.pdfbox.cos.COSDictionary
addAll, asUnmodifiableDictionary, clear, containsKey, containsKey, containsValue, entrySet, getBoolean, getBoolean, getBoolean, getCOSArray, getCOSDictionary, getCOSName, getCOSName, getCOSObject, getCOSStream, getDate, getDate, getDate, getDate, getDictionaryObject, getDictionaryObject, getDictionaryObject, getDictionaryObject, getEmbeddedDate, getEmbeddedDate, getEmbeddedDate, getEmbeddedDate, getEmbeddedInt, getEmbeddedInt, getEmbeddedInt, getEmbeddedInt, getEmbeddedString, getEmbeddedString, getEmbeddedString, getEmbeddedString, getFlag, getFloat, getFloat, getFloat, getFloat, getInt, getInt, getInt, getInt, getInt, getInt, getInt, getItem, getItem, getItem, getKeyForValue, getLong, getLong, getLong, getLong, getLong, getNameAsString, getNameAsString, getNameAsString, getNameAsString, getObjectFromPath, getString, getString, getString, getString, getValues, isNeedToBeUpdated, keySet, mergeInto, removeItem, setBoolean, setBoolean, setDate, setDate, setEmbeddedDate, setEmbeddedDate, setEmbeddedInt, setEmbeddedInt, setEmbeddedString, setEmbeddedString, setFlag, setFloat, setFloat, setInt, setInt, setItem, setItem, setItem, setItem, setLong, setLong, setName, setName, setNeedToBeUpdated, setString, setString, size, toString
-
Methods inherited from class org.apache.pdfbox.cos.COSBase
getCOSObject, isDirect, setDirect
-
-
-
-
Field Detail
-
randomAccess
private RandomAccess randomAccess
-
scratchFile
private final ScratchFile scratchFile
-
isWriting
private boolean isWriting
-
LOG
private static final org.apache.commons.logging.Log LOG
-
-
Constructor Detail
-
COSStream
public COSStream()
Creates a new stream with an empty dictionary.Try to avoid using this constructor because it creates a new scratch file in memory. Instead, use
document.getDocument().createCOSStream()
which will use the existing scratch file (in memory or in temp file) of the document.
-
COSStream
public COSStream(ScratchFile scratchFile)
Creates a new stream with an empty dictionary. Data is stored in the given scratch file.- Parameters:
scratchFile
- Scratch file for writing stream data.
-
-
Method Detail
-
checkClosed
private void checkClosed() throws java.io.IOException
Throws if the random access backing store has been closed. Helpful for catching cases where a user tries to use a COSStream which has outlived its COSDocument.- Throws:
java.io.IOException
-
getFilteredStream
@Deprecated public java.io.InputStream getFilteredStream() throws java.io.IOException
Deprecated.UsecreateRawInputStream()
instead.This will get the stream with all of the filters applied.- Returns:
- the bytes of the physical (encoded) stream
- Throws:
java.io.IOException
- when encoding causes an exception
-
ensureRandomAccessExists
private void ensureRandomAccessExists(boolean forInputStream) throws java.io.IOException
- Parameters:
forInputStream
- iftrue
andrandomAccess
isnull
a debug message is logged - input stream should be retrieved after data being written to stream- Throws:
java.io.IOException
-
createRawInputStream
public java.io.InputStream createRawInputStream() throws java.io.IOException
Returns a new InputStream which reads the encoded PDF stream data. Experts only!- Returns:
- InputStream containing raw, encoded PDF stream data.
- Throws:
java.io.IOException
- If the stream could not be read.
-
getUnfilteredStream
@Deprecated public java.io.InputStream getUnfilteredStream() throws java.io.IOException
Deprecated.UsecreateInputStream()
instead.This will get the logical content stream with none of the filters.- Returns:
- the bytes of the logical (decoded) stream
- Throws:
java.io.IOException
- when decoding causes an exception
-
createInputStream
public COSInputStream createInputStream() throws java.io.IOException
Returns a new InputStream which reads the decoded stream data.- Returns:
- InputStream containing decoded stream data.
- Throws:
java.io.IOException
- If the stream could not be read.
-
createInputStream
public COSInputStream createInputStream(DecodeOptions options) throws java.io.IOException
- Throws:
java.io.IOException
-
createUnfilteredStream
@Deprecated public java.io.OutputStream createUnfilteredStream() throws java.io.IOException
Deprecated.UsecreateOutputStream()
instead.This will create an output stream that can be written to.- Returns:
- An output stream which raw data bytes should be written to.
- Throws:
java.io.IOException
- If there is an error creating the stream.
-
createOutputStream
public java.io.OutputStream createOutputStream() throws java.io.IOException
Returns a new OutputStream for writing stream data, using the current filters.- Returns:
- OutputStream for un-encoded stream data.
- Throws:
java.io.IOException
- If the output stream could not be created.
-
createOutputStream
public java.io.OutputStream createOutputStream(COSBase filters) throws java.io.IOException
Returns a new OutputStream for writing stream data, using and the given filters.- Parameters:
filters
- COSArray or COSName of filters to be used.- Returns:
- OutputStream for un-encoded stream data.
- Throws:
java.io.IOException
- If the output stream could not be created.
-
createFilteredStream
@Deprecated public java.io.OutputStream createFilteredStream() throws java.io.IOException
Deprecated.UsecreateRawOutputStream()
instead.This will create a new stream for which filtered byte should be written to. You probably don't want this but want to use the createUnfilteredStream, which is used to write raw bytes to.- Returns:
- A stream that can be written to.
- Throws:
java.io.IOException
- If there is an error creating the stream.
-
createRawOutputStream
public java.io.OutputStream createRawOutputStream() throws java.io.IOException
Returns a new OutputStream for writing encoded PDF data. Experts only!- Returns:
- OutputStream for raw PDF stream data.
- Throws:
java.io.IOException
- If the output stream could not be created.
-
getFilterList
private java.util.List<Filter> getFilterList() throws java.io.IOException
Returns the list of filters.- Throws:
java.io.IOException
-
getLength
public long getLength()
Returns the length of the encoded stream.- Returns:
- length in bytes
-
getFilters
public COSBase getFilters()
This will return the filters to apply to the byte stream. The method will return- null if no filters are to be applied
- a COSName if one filter is to be applied
- a COSArray containing COSNames if multiple filters are to be applied
- Returns:
- the COSBase object representing the filters
-
setFilters
@Deprecated public void setFilters(COSBase filters) throws java.io.IOException
Deprecated.UsecreateOutputStream(COSBase)
instead.Sets the filters to be applied when encoding or decoding the stream.- Parameters:
filters
- The filters to set on this stream.- Throws:
java.io.IOException
- If there is an error clearing the old filters.
-
getString
@Deprecated public java.lang.String getString()
Deprecated.UsetoTextString()
instead.Returns the contents of the stream as a text string.- Returns:
- the string representation of this string.
-
toTextString
public java.lang.String toTextString()
Returns the contents of the stream as a PDF "text string".- Returns:
- the text string representation of this stream.
-
accept
public java.lang.Object accept(ICOSVisitor visitor) throws java.io.IOException
Description copied from class:COSDictionary
visitor pattern double dispatch method.- Overrides:
accept
in classCOSDictionary
- Parameters:
visitor
- The object to notify when visiting this object.- Returns:
- The object that the visitor returns.
- Throws:
java.io.IOException
- If there is an error visiting this object.
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
-