Skip to content

Commit 5ef1089

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Throw exception if PdfReader instance is attempted to be reused between PdfDocument instances
ITXT-CR-938
1 parent 0d18957 commit 5ef1089

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

kernel/src/main/java/com/itextpdf/kernel/PdfException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public class PdfException extends RuntimeException {
233233
public static final String PdfFormXobjectHasInvalidBbox = "PdfFormXObject has invalid BBox.";
234234
public static final String PdfObjectStreamReachMaxSize = "PdfObjectStream reach max size.";
235235
public static final String PdfPagesTreeCouldBeGeneratedOnlyOnce = "PdfPages tree could be generated only once.";
236+
public static final String PdfReaderHasBeenAlreadyUtilized = "Given PdfReader instance has already been utilized. The PdfReader cannot be reused, please create a new instance.";
236237
public static final String PdfStartxrefIsNotFollowedByANumber = "PDF startxref is not followed by a number.";
237238
public static final String PdfStartxrefNotFound = "PDF startxref not found.";
238239
public static final String PdfIndirectObjectBelongsToOtherPdfDocument = "Pdf indirect object belongs to other PDF document. Copy object to current pdf document.";

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,9 @@ protected void open(PdfVersion newPdfVersion) {
18111811
try {
18121812
EventCounterHandler.getInstance().onEvent(CoreEvent.PROCESS, properties.metaInfo, getClass());
18131813
if (reader != null) {
1814+
if (reader.pdfDocument != null) {
1815+
throw new PdfException(PdfException.PdfReaderHasBeenAlreadyUtilized);
1816+
}
18141817
reader.pdfDocument = this;
18151818
reader.readPdf();
18161819
for (ICounter counter : getCounters()) {

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfReaderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ This file is part of the iText (R) project.
5454
import org.junit.Assert;
5555
import org.junit.BeforeClass;
5656
import org.junit.Ignore;
57+
import org.junit.Rule;
5758
import org.junit.Test;
5859
import org.junit.experimental.categories.Category;
60+
import org.junit.rules.ExpectedException;
5961

6062
import java.io.IOException;
6163
import com.itextpdf.io.util.MessageFormatUtil;
@@ -74,6 +76,9 @@ public class PdfReaderTest extends ExtendedITextTest {
7476
static final String creator = "iText 6";
7577
static final String title = "Empty iText 6 Document";
7678

79+
@Rule
80+
public ExpectedException junitExpectedException = ExpectedException.none();
81+
7782
@BeforeClass
7883
public static void beforeClass() {
7984
createDestinationFolder(destinationFolder);
@@ -1587,6 +1592,18 @@ public void wrongStructureFlushingTest() throws IOException {
15871592
pdfDoc.close();
15881593
}
15891594

1595+
@Test
1596+
public void readerReuseTest() throws IOException {
1597+
junitExpectedException.expect(PdfException.class);
1598+
junitExpectedException.expectMessage(PdfException.PdfReaderHasBeenAlreadyUtilized);
1599+
1600+
String filename = sourceFolder + "hello.pdf";
1601+
1602+
PdfReader reader = new PdfReader(filename);
1603+
PdfDocument pdfDoc1 = new PdfDocument(reader);
1604+
PdfDocument pdfDoc2 = new PdfDocument(reader);
1605+
}
1606+
15901607
private boolean objectTypeEqualTo(PdfObject object, PdfName type) {
15911608
PdfName objectType = ((PdfDictionary) object).getAsName(PdfName.Type);
15921609
return type.equals(objectType);

0 commit comments

Comments
 (0)