Author: lehmi Date: Sun Oct 22 11:35:18 2023 New Revision: 1913198 URL: http://svn.apache.org/viewvc?rev=1913198&view=rev Log: PDFBOX-5660: DRY refactoring
Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1913198&r1=1913197&r2=1913198&view=diff ============================================================================== --- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java (original) +++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java Sun Oct 22 11:35:18 2023 @@ -34,6 +34,7 @@ import org.apache.commons.logging.LogFac import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSNull; import org.apache.pdfbox.cos.COSNumber; @@ -44,6 +45,7 @@ import org.apache.pdfbox.cos.ICOSParser; import org.apache.pdfbox.io.IOUtils; import org.apache.pdfbox.io.RandomAccessRead; import org.apache.pdfbox.io.RandomAccessReadView; +import org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction; import org.apache.pdfbox.pdfparser.XrefTrailerResolver.XRefType; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial; @@ -158,8 +160,7 @@ public class COSParser extends BaseParse */ public COSParser(RandomAccessRead source) throws IOException { - super(source); - fileLen = source.length(); + this(source, null, null, null); } /** @@ -175,11 +176,47 @@ public class COSParser extends BaseParse public COSParser(RandomAccessRead source, String password, InputStream keyStore, String keyAlias) throws IOException { + this(source, password, keyStore, keyAlias, null); + } + + /** + * Constructor for encrypted pdfs. + * + * @param source input representing the pdf. + * @param password password to be used for decryption. + * @param keyStore key store to be used for decryption when using public key security + * @param keyAlias alias to be used for decryption when using public key security + * @param streamCacheCreateFunction a function to create an instance of the stream cache + * + * @throws IOException if the source data could not be read + */ + public COSParser(RandomAccessRead source, String password, InputStream keyStore, + String keyAlias, StreamCacheCreateFunction streamCacheCreateFunction) throws IOException + { super(source); this.password = password; this.keyAlias = keyAlias; fileLen = source.length(); keyStoreInputStream = keyStore; + init(streamCacheCreateFunction); + } + + private void init(StreamCacheCreateFunction streamCacheCreateFunction) + { + String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE); + if (eofLookupRangeStr != null) + { + try + { + setEOFLookupRange(Integer.parseInt(eofLookupRangeStr)); + } + catch (NumberFormatException nfe) + { + LOG.warn("System property " + SYSPROP_EOFLOOKUPRANGE + + " does not contain an integer value, but: '" + eofLookupRangeStr + "'"); + } + } + document = new COSDocument(streamCacheCreateFunction, this); } /** Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java?rev=1913198&r1=1913197&r2=1913198&view=diff ============================================================================== --- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java (original) +++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/FDFParser.java Sun Oct 22 11:35:18 2023 @@ -18,10 +18,7 @@ package org.apache.pdfbox.pdfparser; import java.io.IOException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.cos.COSDictionary; -import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.io.IOUtils; import org.apache.pdfbox.io.RandomAccessRead; @@ -29,8 +26,6 @@ import org.apache.pdfbox.pdmodel.fdf.FDF public class FDFParser extends COSParser { - private static final Log LOG = LogFactory.getLog(FDFParser.class); - /** * Constructs parser for given file using memory buffer. * @@ -41,25 +36,6 @@ public class FDFParser extends COSParser public FDFParser(RandomAccessRead source) throws IOException { super(source); - init(); - } - - private void init() - { - String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE); - if (eofLookupRangeStr != null) - { - try - { - setEOFLookupRange(Integer.parseInt(eofLookupRangeStr)); - } - catch (NumberFormatException nfe) - { - LOG.warn("System property " + SYSPROP_EOFLOOKUPRANGE - + " does not contain an integer value, but: '" + eofLookupRangeStr + "'"); - } - } - document = new COSDocument(this); } /** Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1913198&r1=1913197&r2=1913198&view=diff ============================================================================== --- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java (original) +++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Sun Oct 22 11:35:18 2023 @@ -24,7 +24,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.Loader; import org.apache.pdfbox.cos.COSDictionary; -import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.io.IOUtils; import org.apache.pdfbox.io.RandomAccessRead; @@ -93,27 +92,8 @@ public class PDFParser extends COSParser String alias, StreamCacheCreateFunction streamCacheCreateFunction) throws IOException { super(source, decryptionPassword, keyStore, alias); - init(streamCacheCreateFunction); } - private void init(StreamCacheCreateFunction streamCacheCreateFunction) - { - String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE); - if (eofLookupRangeStr != null) - { - try - { - setEOFLookupRange(Integer.parseInt(eofLookupRangeStr)); - } - catch (NumberFormatException nfe) - { - LOG.warn("System property " + SYSPROP_EOFLOOKUPRANGE - + " does not contain an integer value, but: '" + eofLookupRangeStr + "'"); - } - } - document = new COSDocument(streamCacheCreateFunction, this); - } - /** * The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset) * to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref