Skip to content

Commit 1953b3f

Browse files
committed
Move tokenizer's position to the end of the stream after reading object. Minor refactoring
DEVSIX-6548
1 parent 206783f commit 1953b3f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,15 @@ protected PdfObject readObject(boolean readAsDirect, boolean objStm) throws IOEx
892892
do {
893893
ch = tokens.read();
894894
} while (ch == 32 || ch == 9 || ch == 0 || ch == 12);
895-
if (ch != '\n')
895+
if (ch != '\n') {
896896
ch = tokens.read();
897-
if (ch != '\n')
897+
}
898+
if (ch != '\n') {
898899
tokens.backOnePosition(ch);
899-
return new PdfStream(tokens.getPosition(), dict);
900+
}
901+
PdfStream pdfStream = new PdfStream(tokens.getPosition(), dict);
902+
tokens.seek(pdfStream.getOffset() + pdfStream.getLength());
903+
return pdfStream;
900904
} else {
901905
tokens.seek(pos);
902906
return dict;
@@ -953,11 +957,13 @@ protected PdfDictionary readDictionary(boolean objStm) throws IOException {
953957
PdfDictionary dic = new PdfDictionary();
954958
while (true) {
955959
tokens.nextValidToken();
956-
if (tokens.getTokenType() == PdfTokenizer.TokenType.EndDic)
960+
if (tokens.getTokenType() == PdfTokenizer.TokenType.EndDic) {
957961
break;
958-
if (tokens.getTokenType() != PdfTokenizer.TokenType.Name)
962+
}
963+
if (tokens.getTokenType() != PdfTokenizer.TokenType.Name) {
959964
tokens.throwError(
960965
KernelExceptionMessageConstant.THIS_DICTIONARY_KEY_IS_NOT_A_NAME, tokens.getStringValue());
966+
}
961967
PdfName name = readPdfName(true);
962968
PdfObject obj = readObject(true, objStm);
963969
if (obj == null) {
@@ -1302,7 +1308,8 @@ protected void rebuildXref() throws IOException {
13021308
tokens.seek(0);
13031309
trailer = null;
13041310
ByteBuffer buffer = new ByteBuffer(24);
1305-
PdfTokenizer lineTokeniser = new PdfTokenizer(new RandomAccessFileOrArray(new ReusableRandomAccessSource(buffer)));
1311+
PdfTokenizer lineTokenizer =
1312+
new PdfTokenizer(new RandomAccessFileOrArray(new ReusableRandomAccessSource(buffer)));
13061313
for (; ; ) {
13071314
long pos = tokens.getPosition();
13081315
buffer.reset();
@@ -1326,7 +1333,7 @@ protected void rebuildXref() throws IOException {
13261333
tokens.seek(pos);
13271334
}
13281335
} else if (buffer.get(0) >= '0' && buffer.get(0) <= '9') {
1329-
int[] obj = PdfTokenizer.checkObjectStart(lineTokeniser);
1336+
int[] obj = PdfTokenizer.checkObjectStart(lineTokenizer);
13301337
if (obj == null)
13311338
continue;
13321339
int num = obj[0];

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This file is part of the iText (R) project.
5050
import com.itextpdf.io.source.ByteArrayOutputStream;
5151
import com.itextpdf.io.source.ByteUtils;
5252
import com.itextpdf.io.source.IRandomAccessSource;
53+
import com.itextpdf.io.source.PdfTokenizer;
5354
import com.itextpdf.io.source.RASInputStream;
5455
import com.itextpdf.io.source.RandomAccessSourceFactory;
5556
import com.itextpdf.kernel.exceptions.InvalidXRefPrevException;
@@ -2607,6 +2608,26 @@ public void streamWithoutEndstreamKeywordConservativeModeTest() throws IOExcepti
26072608
}
26082609
}
26092610

2611+
@Test
2612+
public void tokensPositionIsNotUpdatedWhileReadingLengthTest() throws IOException {
2613+
String filename = SOURCE_FOLDER + "simpleDocWithIndirectLength.pdf";
2614+
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename))) {
2615+
PdfTokenizer tokenizer = pdfDoc.getReader().tokens;
2616+
2617+
// we will try to get the content stream object
2618+
// since it's not been gotten yet, iText will read this object,
2619+
// which will change the tokenizer's position
2620+
PdfStream pageContentStream = (PdfStream) pdfDoc.getPdfObject(5);
2621+
2622+
// tokenizer's position after reading object should point to the end of the object's stream
2623+
Assert.assertEquals(pageContentStream.getOffset() + pageContentStream.getLength(), tokenizer.getPosition());
2624+
2625+
// let's read next valid token and check that it means ending stream
2626+
tokenizer.nextValidToken();
2627+
tokenizer.tokenValueEqualsTo(ByteUtils.getIsoBytes("endstream"));
2628+
}
2629+
}
2630+
26102631
/**
26112632
* Returns the current memory use.
26122633
*

0 commit comments

Comments
 (0)