@@ -25,6 +25,7 @@ This file is part of the iText (R) project.
25
25
import com .itextpdf .commons .actions .contexts .IMetaInfo ;
26
26
import com .itextpdf .commons .utils .FileUtil ;
27
27
import com .itextpdf .commons .utils .MessageFormatUtil ;
28
+ import com .itextpdf .commons .utils .SystemUtil ;
28
29
import com .itextpdf .io .font .PdfEncodings ;
29
30
import com .itextpdf .io .logs .IoLogMessageConstant ;
30
31
import com .itextpdf .io .util .GhostscriptHelper ;
@@ -73,6 +74,7 @@ This file is part of the iText (R) project.
73
74
import java .nio .charset .StandardCharsets ;
74
75
import java .util .ArrayList ;
75
76
import java .util .Arrays ;
77
+ import java .util .Collections ;
76
78
import java .util .Comparator ;
77
79
import java .util .HashSet ;
78
80
import java .util .LinkedHashMap ;
@@ -119,6 +121,7 @@ public class CompareTool {
119
121
private static final String VERSION_REPLACEMENT = "<version>" ;
120
122
private static final String COPYRIGHT_REGEXP = "\u00a9 \\ d+-\\ d+ (?:iText Group NV|Apryse Group NV)" ;
121
123
private static final String COPYRIGHT_REPLACEMENT = "\u00a9 <copyright years> Apryse Group NV" ;
124
+ private static final boolean MEMORY_FIRST_WRITER_DISABLED ;
122
125
123
126
private static final String NEW_LINES = "\\ r|\\ n" ;
124
127
@@ -147,6 +150,11 @@ public class CompareTool {
147
150
private String gsExec ;
148
151
private String compareExec ;
149
152
153
+ static {
154
+ MEMORY_FIRST_WRITER_DISABLED = "true" .equalsIgnoreCase (
155
+ SystemUtil .getPropertyOrEnvironmentVariable ("DISABLE_MEMORY_FIRST_WRITER" ));
156
+ }
157
+
150
158
/**
151
159
* Create new {@link CompareTool} instance.
152
160
*/
@@ -182,35 +190,39 @@ public static PdfWriter createTestPdfWriter(String filename) throws IOException
182
190
* be created, or cannot be opened for any other reason.
183
191
*/
184
192
public static PdfWriter createTestPdfWriter (String filename , WriterProperties properties ) throws IOException {
185
- return new MemoryFirstPdfWriter (filename , properties ); // Android-Conversion-Replace return new PdfWriter(filename, properties);
193
+ if (MEMORY_FIRST_WRITER_DISABLED ) {
194
+ return new PdfWriter (filename , properties );
195
+ } else {
196
+ return new MemoryFirstPdfWriter (filename , properties ); // Android-Conversion-Replace return new PdfWriter(filename, properties);
197
+ }
186
198
}
187
199
188
200
/**
189
201
* Create {@link PdfReader} out of the data created recently or read from disk.
190
202
*
191
203
* @param filename File to read the data from when necessary.
204
+ * @param properties {@link ReaderProperties} to use.
192
205
* @return {@link PdfReader} to be used in tests.
193
206
* @throws IOException on error
194
207
*/
195
- public static PdfReader createOutputReader (String filename ) throws IOException {
196
- return CompareTool .createOutputReader (filename , new ReaderProperties ());
208
+ public static PdfReader createOutputReader (String filename , ReaderProperties properties ) throws IOException {
209
+ MemoryFirstPdfWriter outWriter = MemoryFirstPdfWriter .get (filename );
210
+ if (outWriter != null ) {
211
+ return new PdfReader (new ByteArrayInputStream (outWriter .getBAOutputStream ().toByteArray ()), properties );
212
+ } else {
213
+ return new PdfReader (filename , properties );
214
+ }
197
215
}
198
216
199
217
/**
200
218
* Create {@link PdfReader} out of the data created recently or read from disk.
201
219
*
202
220
* @param filename File to read the data from when necessary.
203
- * @param properties {@link ReaderProperties} to use.
204
221
* @return {@link PdfReader} to be used in tests.
205
222
* @throws IOException on error
206
223
*/
207
- public static PdfReader createOutputReader (String filename , ReaderProperties properties ) throws IOException {
208
- MemoryFirstPdfWriter outWriter = MemoryFirstPdfWriter .get (filename );
209
- if (outWriter != null ) {
210
- return new PdfReader (new ByteArrayInputStream (outWriter .getBAOutputStream ().toByteArray ()), properties );
211
- } else {
212
- return new PdfReader (filename , properties );
213
- }
224
+ public static PdfReader createOutputReader (String filename ) throws IOException {
225
+ return CompareTool .createOutputReader (filename , new ReaderProperties ());
214
226
}
215
227
216
228
/**
@@ -360,39 +372,39 @@ public CompareTool enableEncryptionCompare(boolean kdfSaltCompareEnabled) {
360
372
}
361
373
362
374
/**
363
- * Gets {@link ReaderProperties} to be passed later to the {@link PdfReader} of the output document.
375
+ * Gets {@link ReaderProperties} to be passed later to the {@link PdfReader} of the cmp document.
364
376
* <p>
365
377
* Documents for comparison are opened in reader mode. This method is intended to alter {@link ReaderProperties}
366
- * which are used to open the output document. This is particularly useful for comparison of encrypted documents.
378
+ * which are used to open the cmp document. This is particularly useful for comparison of encrypted documents.
367
379
* <p>
368
380
* For more explanations about what outDoc and cmpDoc are see last paragraph of the {@link CompareTool}
369
381
* class description.
370
382
*
371
- * @return {@link ReaderProperties} instance to be passed later to the {@link PdfReader} of the output document.
383
+ * @return {@link ReaderProperties} instance to be passed later to the {@link PdfReader} of the cmp document.
372
384
*/
373
- public ReaderProperties getOutReaderProperties () {
374
- if (outProps == null ) {
375
- outProps = new ReaderProperties ();
385
+ public ReaderProperties getCmpReaderProperties () {
386
+ if (cmpProps == null ) {
387
+ cmpProps = new ReaderProperties ();
376
388
}
377
- return outProps ;
389
+ return cmpProps ;
378
390
}
379
391
380
392
/**
381
- * Gets {@link ReaderProperties} to be passed later to the {@link PdfReader} of the cmp document.
393
+ * Gets {@link ReaderProperties} to be passed later to the {@link PdfReader} of the output document.
382
394
* <p>
383
395
* Documents for comparison are opened in reader mode. This method is intended to alter {@link ReaderProperties}
384
- * which are used to open the cmp document. This is particularly useful for comparison of encrypted documents.
396
+ * which are used to open the output document. This is particularly useful for comparison of encrypted documents.
385
397
* <p>
386
398
* For more explanations about what outDoc and cmpDoc are see last paragraph of the {@link CompareTool}
387
399
* class description.
388
400
*
389
- * @return {@link ReaderProperties} instance to be passed later to the {@link PdfReader} of the cmp document.
401
+ * @return {@link ReaderProperties} instance to be passed later to the {@link PdfReader} of the output document.
390
402
*/
391
- public ReaderProperties getCmpReaderProperties () {
392
- if (cmpProps == null ) {
393
- cmpProps = new ReaderProperties ();
403
+ public ReaderProperties getOutReaderProperties () {
404
+ if (outProps == null ) {
405
+ outProps = new ReaderProperties ();
394
406
}
395
- return cmpProps ;
407
+ return outProps ;
396
408
}
397
409
398
410
/**
@@ -1313,6 +1325,10 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
1313
1325
CompareTool .writeOnDiskIfNotExists (cmpPdf );
1314
1326
return compareVisuallyAndCombineReports (compareResult .getReport (), outPath , differenceImagePrefix , ignoredAreas , equalPages );
1315
1327
}
1328
+ } catch (Exception e ) {
1329
+ CompareTool .writeOnDisk (outPdf );
1330
+ CompareTool .writeOnDiskIfNotExists (cmpPdf );
1331
+ throw e ;
1316
1332
}
1317
1333
}
1318
1334
0 commit comments