@@ -45,6 +45,8 @@ This file is part of the iText (R) project.
45
45
import com .itextpdf .io .LogMessageConstant ;
46
46
import com .itextpdf .io .source .ByteArrayOutputStream ;
47
47
import com .itextpdf .io .source .ByteUtils ;
48
+ import com .itextpdf .io .source .IRandomAccessSource ;
49
+ import com .itextpdf .io .source .RandomAccessSourceFactory ;
48
50
import com .itextpdf .io .util .FileUtil ;
49
51
import com .itextpdf .io .util .MessageFormatUtil ;
50
52
import com .itextpdf .kernel .PdfException ;
@@ -53,6 +55,11 @@ This file is part of the iText (R) project.
53
55
import com .itextpdf .test .annotations .LogMessage ;
54
56
import com .itextpdf .test .annotations .LogMessages ;
55
57
import com .itextpdf .test .annotations .type .IntegrationTest ;
58
+
59
+ import java .io .FileInputStream ;
60
+ import java .io .InputStream ;
61
+ import java .nio .file .Files ;
62
+ import java .nio .file .Paths ;
56
63
import org .junit .Assert ;
57
64
import org .junit .BeforeClass ;
58
65
import org .junit .Ignore ;
@@ -2012,6 +2019,38 @@ public void pdf11VersionValidTest() throws IOException {
2012
2019
new PdfDocument (new PdfReader (fileName ));
2013
2020
}
2014
2021
2022
+ @ Test
2023
+ public void closeStreamCreatedByITextTest () throws IOException {
2024
+ String fileName = sourceFolder + "emptyPdf.pdf" ;
2025
+ String copiedFileName = destinationFolder + "emptyPdf.pdf" ;
2026
+ //Later in the test we will need to delete a file. Since we do not want to delete it from sources, we will
2027
+ // copy it to destination folder.
2028
+ File copiedFile = copyFileForTest (fileName , copiedFileName );
2029
+ Exception e = Assert .assertThrows (com .itextpdf .io .IOException .class , () -> new PdfReader (fileName ));
2030
+ Assert .assertEquals (com .itextpdf .io .IOException .PdfHeaderNotFound , e .getMessage ());
2031
+ //This check is meaningfull only on Windows, since on other OS the fact of a stream being open doesn't
2032
+ // prevent the stream from being deleted.
2033
+ Assert .assertTrue (FileUtil .deleteFile (copiedFile ));
2034
+ }
2035
+
2036
+ @ Test
2037
+ public void notCloseUserStreamTest () throws IOException {
2038
+ String fileName = sourceFolder + "emptyPdf.pdf" ;
2039
+ try (InputStream pdfStream = new FileInputStream (fileName )) {
2040
+ IRandomAccessSource randomAccessSource = new RandomAccessSourceFactory ()
2041
+ .createSource (pdfStream );
2042
+ Exception e = Assert .assertThrows (com .itextpdf .io .IOException .class ,
2043
+ () -> new PdfReader (randomAccessSource , new ReaderProperties ()));
2044
+ //An exception would be thrown, if stream is closed.
2045
+ Assert .assertEquals (-1 , pdfStream .read ());
2046
+ }
2047
+ }
2048
+ private File copyFileForTest (String fileName , String copiedFileName ) throws IOException {
2049
+ File copiedFile = new File (copiedFileName );
2050
+ Files .copy (Paths .get (fileName ), Paths .get (copiedFileName ));
2051
+ return copiedFile ;
2052
+ }
2053
+
2015
2054
private PdfReader pdfDocumentNotReadTestInit () throws IOException {
2016
2055
String filename = sourceFolder + "XrefWithNullOffsets.pdf" ;
2017
2056
0 commit comments