Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-79 nio object parser #251

Merged
merged 59 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
e71c161
GH-79 a sort of, kind of, working prototype
Mar 8, 2022
f363451
GH-79 a sort of, kind of, working a slightly better
Mar 10, 2022
cbfe61c
GH-79 small wins, still lots of work to do.
Jun 3, 2022
e663f91
Merge branch 'master' into GH-79.nio.object.parser
Jun 29, 2022
478f230
GH-79 fix unrelated annotation parsing error.
Nov 10, 2022
f9639a2
GH-79 fix parser offset issue.
Nov 10, 2022
37e83dd
GH-79 update logger
Nov 10, 2022
4120230
GH-79 fix decimal parsing
Nov 10, 2022
e019773
GH-79 some debug helpers
Nov 10, 2022
c7d0a1d
GH-79 fix encryption reference passthrough when parsing arrays
Nov 11, 2022
300513b
GH-79 fix nasty CrossReferenceTable error where subgroups were not pa…
Nov 12, 2022
4a8aeaa
Merge branch 'master' into GH-79.nio.object.parser
Nov 14, 2022
f239c64
GH-79 more encryption reference issues.
Nov 14, 2022
bf7002e
GH-79 fix DictionaryEntries type changes in the lexer.
Nov 17, 2022
0dd8f54
GH-79 touch up white space handling for streams
Nov 19, 2022
406fd56
GH-79 fix incorrect starting object number when parsing xref table
Nov 19, 2022
9a365c5
GH-79 little code clean up.
Nov 23, 2022
efe4d77
GH-79 address parser issue where know types were not returned as obje…
Dec 2, 2022
117a802
Merge branch 'master' into GH-79.nio.object.parser
Jan 13, 2023
7c4de19
Merge branch 'master' into GH-79.nio.object.parser
Jan 13, 2023
cc9e916
GH-79 fix issue trying to load a font data stream as a font object.
Jan 17, 2023
f6cdbb3
GH-79 fix issue where zeroed stream wasn't taking into account the en…
Jan 17, 2023
85312d0
GH-79 make sure we don't clear a valid font bbox.
Jan 17, 2023
814e55c
GH-79 further refinement of the approach.
Jan 22, 2023
e3dcf68
GH-79 cleaned up stream and table cross-reference common code as well…
Jan 23, 2023
70f8c93
GH-79 further clean up and brought back incremental updates for table…
Jan 24, 2023
d2bcdc7
GH-79 attempted cleanup/removal of throwable from code base.
Jan 25, 2023
a2b39a5
GH-80 fix some missing 2 byte hex values from not being rendering cor…
Jan 25, 2023
3c23ad7
GH-79 remove redundant new BufferedOutputStream wrapper.
Jan 25, 2023
34c8282
GH-79 clean up extra new line creation.
Jan 25, 2023
fad1274
GH-79 todo cleanup
Jan 25, 2023
fb53c96
GH-79 refactor a recursive call that was getting away and not really …
Jan 27, 2023
e42ff2b
GH-79 standardize buffer sizes for and minimize parser creation
Jan 29, 2023
8257457
GH-79 fix missing generation when adding a used entry.
Jan 30, 2023
46ed19e
GH-79 fix layer visibility state issue by pulling from catalog when p…
Feb 1, 2023
d572719
GH-79 more exception handling and logging touchups.
Feb 2, 2023
9829529
GH-79 more logging touch ups.
Feb 3, 2023
6b64e7d
GH-79 revert PdfSecurityException change
Feb 3, 2023
4abab2d
GH-79 touch up digital signatures to use the new nio file access.
Feb 8, 2023
00bdb64
GH-79 fix nasty offset issue when reindexing and looking for xref pos…
Feb 14, 2023
1a283d3
GH-79 move annotation to soft references so we less likely to lose re…
Feb 15, 2023
a61d70c
Merge branch 'master' into GH-79.nio.object.parser
Feb 15, 2023
233a2e2
GH-79 revert back to 4 threads.
Feb 16, 2023
a643c74
GH-79 attempt to fix attachments, not there yet
Feb 16, 2023
2ff1c0b
GH-79 migrated to slice over buffer get operations.
Feb 17, 2023
d317f31
GH-79 reworked Parser.getPObject to handle attachments that can span …
Feb 18, 2023
6f6c0da
GH-79 touch up artifacts on icon.
Feb 18, 2023
8850669
GH-79 touch up artifacts on icon.
Feb 18, 2023
81aced7
GH-79 fix issue when loading a catalogue followed by a regular docume…
Feb 18, 2023
e136ee9
GH-79 code clean up
Feb 22, 2023
727afdf
GH-79 code clean up
Feb 22, 2023
7178314
GH-79 more code clean up
Feb 23, 2023
1952e59
GH-79 code clean up
Feb 24, 2023
6186daa
GH-79 code clean up
Feb 24, 2023
10f8261
GH-79 code clean up
Feb 24, 2023
e19723a
GH-79 code clean up
Feb 27, 2023
09fe6a2
GH-79 misc. log combing fixes.
Mar 9, 2023
43625d8
GH-79 address concurrent access/modification during page init when a …
Mar 9, 2023
6450ca1
Merge branch 'master' into GH-79.nio.object.parser
Mar 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
GH-79 attempted cleanup/removal of throwable from code base.
  • Loading branch information
Patrick Corless committed Jan 25, 2023
commit d2bcdc79d78746caa450346c79061c65fa3bf3f8
39 changes: 17 additions & 22 deletions core/core-awt/src/main/java/org/icepdf/core/pobjects/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.icepdf.core.SecurityCallback;
import org.icepdf.core.application.ProductInfo;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PdfSecurityException;
import org.icepdf.core.pobjects.acroform.FieldDictionary;
import org.icepdf.core.pobjects.acroform.InteractiveForm;
Expand Down Expand Up @@ -183,13 +182,12 @@ private String getDocumentCachedFilePath() {
* Load a PDF file from the given path and initiates the document's Catalog.
*
* @param filepath path of PDF document.
* @throws PDFException if an invalid file encoding.
* @throws PdfSecurityException if a security provider cannot be found
* or there is an error decrypting the file.
* @throws IOException if a problem setting up, or parsing the file.
*/
public void setFile(String filepath)
throws PDFException, PdfSecurityException, IOException {
throws PdfSecurityException, IOException {
setDocumentOrigin(filepath);

File file = new File(filepath);
Expand All @@ -198,7 +196,7 @@ public void setFile(String filepath)
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
setInputStream(mappedFileByteBuffer);
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to set document file path", e);
}
}
Expand All @@ -210,13 +208,12 @@ public void setFile(String filepath)
* be stored in memory.
*
* @param url location of file.
* @throws PDFException an invalid file encoding.
* @throws PdfSecurityException if a security provider can not be found
* or there is an error decrypting the file.
* @throws IOException if a problem downloading, setting up, or parsing the file.
*/
public void setUrl(URL url)
throws PDFException, PdfSecurityException, IOException {
throws PdfSecurityException, IOException {
InputStream in = null;
try {
// make a connection
Expand All @@ -243,13 +240,12 @@ public void setUrl(URL url)
*
* @param inputStream input stream containing PDF data
* @param pathOrURL value assigned to document origin
* @throws PDFException an invalid stream or file encoding
* @throws PdfSecurityException if a security provider can not be found
* or there is an error decrypting the file.
* @throws IOException if a problem setting up, or parsing the SeekableInput.
*/
public void setInputStream(InputStream inputStream, String pathOrURL)
throws PDFException, PdfSecurityException, IOException {
throws PdfSecurityException, IOException {
setDocumentOrigin(pathOrURL);

if (!isCachingEnabled) {
Expand All @@ -259,7 +255,6 @@ public void setInputStream(InputStream inputStream, String pathOrURL)
}
// if caching is allowed cache the url to file
else {
//System.out.println("Started downloading PDF to disk : " + (new java.util.Date()));
// create tmp file and write bytes to it.
File tempFile = File.createTempFile(
"ICEpdfTempFile" + getClass().hashCode(),
Expand All @@ -276,8 +271,9 @@ public void setInputStream(InputStream inputStream, String pathOrURL)
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
setInputStream(mappedFileByteBuffer);
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to set document input stream", e);
throw e;
}
}
}
Expand All @@ -294,13 +290,12 @@ public void setInputStream(InputStream inputStream, String pathOrURL)
* @param offset the index into the byte array where the PDF data begins
* @param length the number of bytes in the byte array belonging to the PDF data
* @param pathOrURL value assigned to document origin
* @throws PDFException an invalid stream or file encoding
* @throws PdfSecurityException if a security provider can not be found
* or there is an error decrypting the file.
* @throws IOException if a problem setting up, or parsing the SeekableInput.
*/
public void setByteArray(byte[] data, int offset, int length, String pathOrURL)
throws PDFException, PdfSecurityException, IOException {
throws PdfSecurityException, IOException {
setDocumentOrigin(pathOrURL);

if (!isCachingEnabled) {
Expand All @@ -318,9 +313,9 @@ public void setByteArray(byte[] data, int offset, int length, String pathOrURL)
// Write the data to the temp file.
try {
Files.copy(new ByteArrayInputStream(data), tempFile.toPath());
} catch (Throwable e) {
} catch (IOException e) {
logger.log(Level.FINE, "Error writing PDF output stream.", e);
throw new IOException(e.getMessage());
throw e;
}

setDocumentCachedFilePath(tempFile.getAbsolutePath());
Expand All @@ -330,8 +325,9 @@ public void setByteArray(byte[] data, int offset, int length, String pathOrURL)
ByteBuffer mappedFileByteBuffer = documentFileChannel.map(
FileChannel.MapMode.READ_ONLY, 0, documentFileChannel.size());
setInputStream(mappedFileByteBuffer);
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to set document input stream", e);
throw e;
}
}
}
Expand All @@ -340,12 +336,11 @@ public void setByteArray(byte[] data, int offset, int length, String pathOrURL)
* Sets the input stream of the PDF file to be rendered.
*
* @param input ByteBuffer containing PDF data stream
* @throws PDFException if error occurs
* @throws PdfSecurityException security error
* @throws IOException io error during stream handling
*/
private void setInputStream(ByteBuffer input)
throws PDFException, PdfSecurityException, IOException {
throws PdfSecurityException, IOException {
try {
// load the head
header = new Header();
Expand All @@ -360,7 +355,7 @@ private void setInputStream(ByteBuffer input)
Trailer trailer = new Trailer();
try {
trailer.parseXrefOffset(input);
} catch (Throwable e) {
} catch (Exception e) {
trailer.setLazyInitializationFailed(true);
logger.log(Level.WARNING, "Trailer loading failed, reindexing file.", e);
}
Expand All @@ -370,7 +365,7 @@ private void setInputStream(ByteBuffer input)
crossReferenceRoot.setTrailer(trailer);
crossReferenceRoot.initialize(input);
library.setCrossReferenceRoot(crossReferenceRoot);
} catch (Throwable e) {
} catch (Exception e) {
crossReferenceRoot.setLazyInitializationFailed(true);
logger.log(Level.WARNING, "Cross reference loading failed, reindexing file.");
}
Expand Down Expand Up @@ -401,7 +396,7 @@ private void setInputStream(ByteBuffer input)
library.setStateManager(stateManager);
} catch (PdfSecurityException | IOException e) {
dispose();
logger.log(Level.SEVERE, "Error loading PDF Document.", e);
logger.log(Level.SEVERE, "Failed to load PDF Document.", e);
throw e;
} catch (Exception e) {
dispose();
Expand Down Expand Up @@ -577,9 +572,9 @@ public long writeToOutputStream(OutputStream out) throws IOException {
this,
new BufferedOutputStream(out),
documentLength);
} catch (Throwable e) {
} catch (IOException e) {
logger.log(Level.FINE, "Error writing PDF output stream.", e);
throw new IOException(e.getMessage());
throw e;
}
return documentLength + appendedLength;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ public synchronized void init() throws InterruptedException {
shapes = new Shapes();
logger.log(Level.FINE, "Parsing form interrupted parsing Form content stream.", e);
throw new InterruptedException(e.getMessage());
} catch (Throwable e) {
// some parsing or other wise unrecoverable problem, we'll try to render the page none the less.
} catch (Exception e) {
// some parsing or otherwise unrecoverable problem, we'll try to render the page nonetheless.
// but not this form object.
shapes = new Shapes();
logger.log(Level.FINE, "Error parsing Form content stream.", e);
logger.log(Level.WARNING, "Error parsing Form content stream.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public int getUnsignedInt(int start, int offset) {
stringData.substring(start, start + offset), 16);
} catch (NumberFormatException e) {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Number Format Exception " + unsignedInt);
logger.finer("Number Format Exception " + unsignedInt + " " + stringData.substring(start, start + offset));
}
}
return unsignedInt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ private String convertHexChars(StringBuilder name) {
}
}
}
} catch (Throwable e) {
} catch (Exception e) {
logger.finer("Error parsing hexadecimal characters.");
// we are going to bail on any exception and just return the original
// string.
// we are going to bail on any exception and just return the original string
return name.toString();
}
return name.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public SignatureValidator validateSignature(SignatureFieldDictionary signatureFi
return signatureValidator;
} catch (SignatureIntegrityException e) {
logger.log(Level.WARNING, "Signature certificate could not be initialized.", e);
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.WARNING, "Signature validation was unsuccessful.", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public VariableTextFieldDictionary(Library library, DictionaryEntries entries) {
fontName = gs.getTextState().fontName;
}
}
} catch (Throwable e) {
} catch (Exception e) {
logger.warning("Could not validate default appearance, defaulting.");
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ public String generateDefaultAppearance(String content, Resources resources) {
if (gs.getTextState().fontName != null) fontName = gs.getTextState().fontName;
}
}
} catch (Throwable e) {
} catch (Exception e) {
logger.warning("Could not generate default appearance stream.");
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Error parsing text feld content stream", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected void parseSignerData(ASN1Sequence signedData, byte[] cmsData) throws S
MessageDigest tmp = AlgorithmIdentifier.getDigestInstance(objectId, null);
logger.finest("DigestAlgorithmIdentifiers: " + digestAlgorithmName + " " + objectId);
logger.finest(tmp.toString());
} catch (Throwable ex) {
} catch (Exception ex) {
logger.log(Level.WARNING, "Error finding iod: " + objectId, ex);
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ protected void parseSignerData(ASN1Sequence signedData, byte[] cmsData) throws S
try {
new TimeStampToken(contentInfo);
isEmbeddedTimeStamp = true;
} catch (Throwable e1) {
} catch (Exception e1) {
throw new SignatureIntegrityException("Valid TimeStamp could now be created");
}
}
Expand Down Expand Up @@ -651,7 +651,7 @@ protected void validateDocument() throws SignatureIntegrityException {
} catch (CertificateException e) {
logger.log(Level.FINEST, "Certificate exception.", e);
isCertificateChainTrusted = false;
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.FINEST, "Error validation certificate chain.", e);
isCertificateChainTrusted = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.*;
import org.icepdf.core.pobjects.acroform.signature.exceptions.CertificateVerificationException;
import org.icepdf.core.pobjects.acroform.signature.exceptions.RevocationVerificationException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ private static BufferedImage deriveBufferedImageFromTIFFBytes(
img = (BufferedImage) roGetAsBufferedImage.invoke(javax_media_jai_RenderedOp_op);
}
}
} catch (Throwable e) {
} catch (Exception e) {
// catch and return a null image so we can try again using a different compression method.
logger.finer("Decoding TIFF: " + TIFF_COMPRESSION_NAMES[compression] + " failed trying alternative");
} finally {
Expand Down Expand Up @@ -817,9 +817,9 @@ private static BufferedImage applyImageMaskAndDecodeArray(
// From empirically testing 6 of the 9 possible combinations of
// BlackIs1 {true, false, not given} and Decode {[0 1], [1 0], not given}
// this is the rule. Unknown combinations:
// BlackIs1=false, Decode=[0 1]
// BlackIs1=false, Decode=[1 0]
// BlackIs1=true, Decode=[0 1]
// BlackIs1=false, Decode=[0 1]
// BlackIs1=false, Decode=[1 0]
// BlackIs1=true, Decode=[0 1]
boolean flag = ((blackIs1 == null) && (!defaultDecode)) ||
((blackIs1 != null) && blackIs1 && (decode == null));
if (imageMask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public synchronized void init(Name subtype) {
}
// catch everything, we can fall back to font substitution if a failure
// occurs.
catch (Throwable e) {
catch (Exception e) {
logger.log(Level.FINE, "Error Reading Embedded Font ", e);
embeddedFontDamaged = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ else if (FONT_SUBTYPE_CID_FONT_TYPE_0.equals(subtype)) {
font = new TypeCidType2Font(library, entries);
}
if (font == null) {
// create OFont implementation.
// create OFont implementation.
font = new org.icepdf.core.pobjects.fonts.ofont.Font(library, entries);
}
return font;
Expand All @@ -127,7 +127,7 @@ public FontFile createFontFile(Stream fontStream, int fontType, Name fontSubType
} else if (FONT_CID_TYPE_2 == fontType) {
fontFile = new ZFontType2(fontStream);
}
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.WARNING, "Error reading font file type " + FONT_OPEN_TYPE, e);
}
return fontFile;
Expand All @@ -136,7 +136,7 @@ public FontFile createFontFile(Stream fontStream, int fontType, Name fontSubType
public FontFile createFontFile(File file, int fontType, String fontSubType) {
try {
return createFontFile(file.toURI().toURL(), fontType, fontSubType);
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.FINE, "Could not create instance of font file " + fontType, e);
}
return null;
Expand All @@ -150,7 +150,7 @@ public FontFile createFontFile(URL url, int fontType, String fontSubType) {
} else if (FONT_TYPE_1 == fontType) {
fontFile = new ZFontType1(url);
}
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.FINE, "Could not create instance of font file " + fontType, e);
}
return fontFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void setFontProperties(Preferences fontPreferences)
}
}
sortFontListByName();
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.FINE, "Error setting font properties ", e);
throw new IllegalArgumentException(errorString);
}
Expand Down Expand Up @@ -439,8 +439,8 @@ private void loadSystemFont(List<String> fontDirectories) {
}
} catch (SecurityException e) {
logger.log(Level.WARNING, "SecurityException: failed to load fonts from directory: ", e);
} catch (Throwable e) {
logger.log(Level.FINE, "Failed to load fonts from directory: ", e);
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to load fonts from directory: ", e);
}
}

Expand Down Expand Up @@ -855,7 +855,8 @@ private FontFile buildFont(String fontPath) {
}
font = buildFont(file);
}
} catch (Throwable e) {
} catch (Exception e) {
// there are a lot of system font that don't ready correctly, so don't get to noisy
logger.log(Level.FINE, "Error reading font program.", e);
}
return font;
Expand Down Expand Up @@ -905,7 +906,7 @@ else if ((fontPath.endsWith(".otf") || fontPath.endsWith(".OTF")) ||
(fontPath.endsWith(".otc") || fontPath.endsWith(".OTC"))) {
font = fontFactory.createFontFile(fontUri, FontFactory.FONT_OPEN_TYPE, null);
}
} catch (Throwable e) {
} catch (Exception e) {
logger.log(Level.FINE, "Error reading font program.", e);
}
return font;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ protected void parseToUnicode() {
try {
toUnicodeCMap = new CMap(cMapStream);
toUnicodeCMap.init();
} catch (Throwable e) {
logger.log(Level.SEVERE, "Error reading CMap file.", e);
} catch (Exception e) {
logger.log(Level.WARNING, "Error reading CMap file.", e);
}
}
else if (objectUnicode instanceof Name) {
Expand Down
Loading