Class COSDocument
- java.lang.Object
-
- com.pd4ml.pdf.cos.COSDocument
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class COSDocument extends java.lang.Object implements java.io.CloseableThe in-memory representation of a whole PDF file's COS layer: the table of indirect objects keyed byCOSObjectKey, and the trailer dictionary that names the document's root ("Catalog") and other special objects.A document can either be built up programmatically (see
setObject(com.pd4ml.pdf.cos.COSObjectKey, com.pd4ml.pdf.cos.COSBase)) or produced byCOSParserfrom the bytes of an actual PDF file. Either way, this is also the natural starting point forCOSPathEvaluatorqueries.Thread-safety: the object table is safe for concurrent
setObject(com.pd4ml.pdf.cos.COSObjectKey, com.pd4ml.pdf.cos.COSBase)calls from multiple threads (e.g.COSObjectImportercloning several distinct source objects into one shared target document in parallel) -- it is a synchronized map, not a plainLinkedHashMap, specifically to avoid silently losing entries under concurrentput(). Insertion order is still preserved for callers that iterategetObjects()/getObjectKeys()without their own explicit sort. As with any synchronized-collection wrapper, a caller that iterates one of those views while another thread is still writing needs its own external synchronization on the returned document instance; the normal "build fully, then read" usage pattern (all writes happen-before the read, e.g. via thread join/executor completion) needs none. Other mutable fields here (trailer,version, etc.) are plain, unsynchronized fields -- this class is meant to be built up by one thread (or several threads only throughsetObject) and then safely published/read, not mutated concurrently in general.
-
-
Constructor Summary
Constructors Constructor Description COSDocument()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()COSObjectcreateNewIndirectObject(COSBase value)COSDictionarygetCatalog()The document catalog, i.e.COSSecurityExceptiongetEncryptionError()Non-null when this document is encrypted but the password given toCOSParsercould not be authenticated as either the user or the owner password -- in that case, strings and streams throughout the document are still in their encrypted form.longgetHighestObjectNumber()Allocates the next free object number, for building new indirect objects programmatically.COSBasegetObject(COSObjectKey key)Looks up an already-registered object.java.util.Collection<COSObjectKey>getObjectKeys()java.util.Collection<COSBase>getObjects()COSDictionarygetTrailer()floatgetVersion()booleanhasObject(COSObjectKey key)booleanisEncrypted()True if this document's trailer names an/Encryptdictionary (regardless of whether the password supplied to the parser actually worked -- seegetEncryptionError()to tell those apart).voidremoveObject(COSObjectKey key)Deregisters the object stored underkey, if any.voidsetEncrypted(boolean encrypted)voidsetEncryptionError(COSSecurityException encryptionError)voidsetObject(COSObjectKey key, COSBase value)Registers/overwrites the object stored underkey.voidsetSourceToClose(java.io.Closeable source)Wires up a resource (e.g.voidsetTrailer(COSDictionary trailer)voidsetVersion(float version)intsize()
-
-
-
Method Detail
-
getVersion
public float getVersion()
-
setVersion
public void setVersion(float version)
-
getTrailer
public COSDictionary getTrailer()
-
setTrailer
public void setTrailer(COSDictionary trailer)
-
isEncrypted
public boolean isEncrypted()
True if this document's trailer names an/Encryptdictionary (regardless of whether the password supplied to the parser actually worked -- seegetEncryptionError()to tell those apart). Strings and streams are decrypted in place as the document is parsed, so once this returns true with no encryption error, every other accessor in this API already returns plaintext and there is nothing further to do.
-
setEncrypted
public void setEncrypted(boolean encrypted)
-
getEncryptionError
public COSSecurityException getEncryptionError()
Non-null when this document is encrypted but the password given toCOSParsercould not be authenticated as either the user or the owner password -- in that case, strings and streams throughout the document are still in their encrypted form.
-
setEncryptionError
public void setEncryptionError(COSSecurityException encryptionError)
-
getCatalog
public COSDictionary getCatalog()
The document catalog, i.e.trailer/Root, ornullif the trailer has no (resolvable) Root entry.
-
setObject
public void setObject(COSObjectKey key, COSBase value)
Registers/overwrites the object stored underkey. Used both by the parser (as it reads "n g obj ... endobj" bodies) and by in-memory document construction.
-
getObject
public COSBase getObject(COSObjectKey key)
Looks up an already-registered object. Parsers that support lazy / on-demand parsing should override or wrap this class so a cache miss triggers parsing the object at its recorded file offset; this base implementation assumes eager loading (which is whatCOSParser.parse()does).
-
hasObject
public boolean hasObject(COSObjectKey key)
-
removeObject
public void removeObject(COSObjectKey key)
Deregisters the object stored underkey, if any. Used by callers that build up a document incrementally and then discard objects that turned out unreachable from the trailer's/Root(e.g.com.pd4ml.pdf.merge's post-clone reachability sweep, which drops structure-tree clones dragged in transitively via a/Pgreference to a page that wasn't actually selected).
-
getObjectKeys
public java.util.Collection<COSObjectKey> getObjectKeys()
-
getObjects
public java.util.Collection<COSBase> getObjects()
-
size
public int size()
-
getHighestObjectNumber
public long getHighestObjectNumber()
Allocates the next free object number, for building new indirect objects programmatically.
-
setSourceToClose
public void setSourceToClose(java.io.Closeable source)
Wires up a resource (e.g. the open file/stream the parser is reading from) that should be released when this document is closed.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
-