1515import java .util .regex .PatternSyntaxException ;
1616
1717import org .apache .pdfbox .Loader ;
18+ import org .apache .pdfbox .pdmodel .PDDocument ;
1819import org .apache .pdfbox .pdmodel .PDPage ;
1920import org .apache .pdfbox .pdmodel .common .PDRectangle ;
2021import org .dspace .content .Bitstream ;
@@ -85,23 +86,20 @@ public String getDescription() {
8586
8687 public File inputStreamToTempFile (InputStream source , String prefix , String suffix ) throws IOException {
8788 File f = File .createTempFile (prefix , suffix );
88- f .deleteOnExit ();
89- FileOutputStream fos = new FileOutputStream (f );
90-
91- byte [] buffer = new byte [1024 ];
92- int len = source .read (buffer );
93- while (len != -1 ) {
94- fos .write (buffer , 0 , len );
95- len = source .read (buffer );
89+ try (FileOutputStream fos = new FileOutputStream (f )) {
90+ byte [] buffer = new byte [8192 ];
91+ int len = source .read (buffer );
92+ while (len != -1 ) {
93+ fos .write (buffer , 0 , len );
94+ len = source .read (buffer );
95+ }
9696 }
97- fos .close ();
9897 return f ;
9998 }
10099
101100 public File getThumbnailFile (File f , boolean verbose )
102101 throws IOException , InterruptedException , IM4JavaException {
103102 File f2 = new File (f .getParentFile (), f .getName () + ".jpg" );
104- f2 .deleteOnExit ();
105103 ConvertCmd cmd = new ConvertCmd ();
106104 IMOperation op = new IMOperation ();
107105 op .autoOrient ();
@@ -123,11 +121,7 @@ public File getThumbnailFile(File f, boolean verbose)
123121 */
124122 public File getImageFile (File f , boolean verbose )
125123 throws IOException , InterruptedException , IM4JavaException {
126- // Writing an intermediate file to disk is inefficient, but since we're
127- // doing it anyway, we should use a lossless format. IM's internal MIFF
128- // is lossless like PNG and TIFF, but much faster.
129124 File f2 = new File (f .getParentFile (), f .getName () + ".miff" );
130- f2 .deleteOnExit ();
131125 ConvertCmd cmd = new ConvertCmd ();
132126 IMOperation op = new IMOperation ();
133127
@@ -154,9 +148,15 @@ public File getImageFile(File f, boolean verbose)
154148 // same size as the MediaBox if it doesn't exist. Also note that we
155149 // only need to check the first page, since that's what we use for
156150 // generating the thumbnail (PDPage uses a zero-based index).
157- PDPage pdfPage = Loader .loadPDF (f ).getPage (0 );
158- PDRectangle pdfPageMediaBox = pdfPage .getMediaBox ();
159- PDRectangle pdfPageCropBox = pdfPage .getCropBox ();
151+ PDRectangle pdfPageMediaBox ;
152+ PDRectangle pdfPageCropBox ;
153+ try (
154+ PDDocument pdfDoc = Loader .loadPDF (f )
155+ ) {
156+ PDPage pdfPage = pdfDoc .getPage (0 );
157+ pdfPageMediaBox = pdfPage .getMediaBox ();
158+ pdfPageCropBox = pdfPage .getCropBox ();
159+ }
160160
161161 // This option must come *before* we open the input file.
162162 if (pdfPageCropBox != pdfPageMediaBox ) {
0 commit comments