Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/main/java/org/verapdf/cos/COSDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public COSDocument(final InputStream fileStream, final PDDocument document) thro
initCOSDocument(document);
}

private void initCOSDocument(final PDDocument document) {
private void initCOSDocument(final PDDocument document) throws IOException {
this.doc = document;
this.body = new COSBody();
this.reader.checkDocCanBeDecrypted();

this.header = this.reader.getHeader();
this.xref = new COSXRefTable();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/verapdf/io/IReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public interface IReader extends Closeable {
List<COSObject> getObjectStreamsList();

int getGreatestKeyNumberFromXref();

void checkDocCanBeDecrypted() throws IOException;
}
34 changes: 23 additions & 11 deletions src/main/java/org/verapdf/io/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ public COSObject getLinearizationDictionary() {
return this.parser.getLinearizationDictionary();
}

@Override
public void checkDocCanBeDecrypted() throws IOException {
if (this.parser.isEncrypted() && !docCanBeDecrypted()) {
InvalidPasswordException failure = new InvalidPasswordException(StringExceptions.ENCRYPTED_PDF);
try {
this.getPDFSource().close();
} catch (IOException e) {
failure.addSuppressed(e);
}
if (this.parser.getDocument() != null) {
FileResourceHandler handler =
this.parser.getDocument().getResourceHandler();
if (handler != null) {
try {
handler.close();
} catch (IOException e) {
failure.addSuppressed(e);
}
}
}
throw failure;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// PRIVATE METHODS
private void init() throws IOException {
Expand All @@ -152,17 +175,6 @@ private void init() throws IOException {
this.parser.getXRefInfo(infos);
setXRefInfo(infos);

if (this.parser.isEncrypted() && !docCanBeDecrypted()) {
this.getPDFSource().close();
if (this.parser.getDocument() != null) {
FileResourceHandler handler =
this.parser.getDocument().getResourceHandler();
if (handler != null) {
handler.close();
}
}
throw new InvalidPasswordException(StringExceptions.ENCRYPTED_PDF);
}
} catch (IOException e) { // If exception is thrown in init() someone
// should close document stream
this.parser.closeInputStream();
Expand Down
Loading