Skip to content

Commit 8f7d0f3

Browse files
committed
Fixed bug with NotSerializableException when creating pdfReader form string by implementing custom logic for serialization/deserialization.
DEVSIX-572
1 parent 1965698 commit 8f7d0f3

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfReader.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public class PdfReader implements Closeable, Serializable {
8282
//indicate nearest first Indirect reference object which includes current reading the object, using for PdfString decrypt
8383
private PdfIndirectReference currentIndirectReference;
8484

85+
// For internal usage only
86+
private String sourcePath;
87+
8588
protected PdfTokenizer tokens;
8689
protected PdfEncryption decrypt;
8790

@@ -102,7 +105,6 @@ public class PdfReader implements Closeable, Serializable {
102105
protected boolean fixedXref = false;
103106
protected boolean xrefStm = false;
104107

105-
106108
/**
107109
* Constructs a new PdfReader.
108110
*
@@ -153,7 +155,7 @@ public PdfReader(String filename, ReaderProperties properties) throws IOExceptio
153155
.createBestSource(filename),
154156
properties
155157
);
156-
158+
this.sourcePath = filename;
157159
}
158160

159161
/**
@@ -1099,6 +1101,30 @@ private void checkPdfStreamLength(PdfStream pdfStream) throws IOException {
10991101
}
11001102
}
11011103

1104+
/**
1105+
* This method is invoked while deserialization
1106+
*/
1107+
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
1108+
in.defaultReadObject();
1109+
if (sourcePath != null && tokens == null) {
1110+
tokens = getOffsetTokeniser(new RandomAccessSourceFactory().setForceRead(false).createBestSource(sourcePath));
1111+
}
1112+
}
1113+
1114+
/**
1115+
* This method is invoked while serialization
1116+
*/
1117+
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
1118+
if (sourcePath != null) {
1119+
PdfTokenizer tempTokens = tokens;
1120+
tokens = null;
1121+
out.defaultWriteObject();
1122+
tokens = tempTokens;
1123+
} else {
1124+
out.defaultWriteObject();
1125+
}
1126+
}
1127+
11021128
protected static class ReusableRandomAccessSource implements IRandomAccessSource {
11031129
private ByteBuffer buffer;
11041130

0 commit comments

Comments
 (0)