@@ -230,30 +230,37 @@ protected InputStream getThumb(
230230 ) throws Exception {
231231
232232 File tempFile = File .createTempFile ("temp" , ".tmp" );
233- tempFile .deleteOnExit ();
234233
235234 // Write to temp file
236235 try (FileOutputStream fos = new FileOutputStream (tempFile )) {
237- byte [] buffer = new byte [4096 ];
236+ byte [] buffer = new byte [8192 ];
238237 int len ;
239238 while ((len = source .read (buffer )) != -1 ) {
240239 fos .write (buffer , 0 , len );
241240 }
242241 }
243242
244- int rotation = 0 ;
245- try (FileInputStream fis = new FileInputStream (tempFile )) {
246- rotation = getImageRotationUsingImageReader (fis );
247- }
243+ try {
244+ int rotation ;
245+ try (FileInputStream fis = new FileInputStream (tempFile )) {
246+ rotation = getImageRotationUsingImageReader (fis );
247+ }
248248
249- try (FileInputStream fis = new FileInputStream (tempFile )) {
250- // read in bitstream's image
251- BufferedImage buf = ImageIO .read (fis );
249+ try (FileInputStream fis = new FileInputStream (tempFile )) {
250+ BufferedImage buf = ImageIO .read (fis );
251+ if (buf == null ) {
252+ throw new IOException ("No image reader found for bitstream" );
253+ }
252254
253- return getThumbDim (
254- currentItem , buf , verbose , xmax , ymax , blurring , hqscaling , brandHeight , brandFontPoint , rotation ,
255- brandFont
256- );
255+ return getThumbDim (
256+ currentItem , buf , verbose , xmax , ymax , blurring , hqscaling , brandHeight , brandFontPoint , rotation ,
257+ brandFont
258+ );
259+ }
260+ } finally {
261+ if (tempFile != null && tempFile .exists ()) {
262+ tempFile .delete ();
263+ }
257264 }
258265 }
259266
@@ -327,29 +334,30 @@ public InputStream getThumbDim(Item currentItem, BufferedImage buf, boolean verb
327334
328335 // now render the image into the thumbnail buffer
329336 Graphics2D g2d = thumbnail .createGraphics ();
330- g2d .drawImage (correctedImage , 0 , 0 , xsize , ysize , null );
337+ try {
338+ g2d .drawImage (correctedImage , 0 , 0 , xsize , ysize , null );
331339
332- if (brandHeight != 0 ) {
333- ConfigurationService configurationService
334- = DSpaceServicesFactory .getInstance ().getConfigurationService ();
335- Brand brand = new Brand (xsize , brandHeight , new Font (brandFont , Font .PLAIN , brandFontPoint ), 5 );
336- BufferedImage brandImage = brand .create (configurationService .getProperty ("webui.preview.brand" ),
337- configurationService .getProperty ("webui.preview.brand.abbrev" ),
338- currentItem == null ? "" : "hdl:" + currentItem .getHandle ());
340+ if (brandHeight != 0 ) {
341+ ConfigurationService configurationService
342+ = DSpaceServicesFactory .getInstance ().getConfigurationService ();
343+ Brand brand = new Brand (xsize , brandHeight , new Font (brandFont , Font .PLAIN , brandFontPoint ), 5 );
344+ BufferedImage brandImage = brand .create (configurationService .getProperty ("webui.preview.brand" ),
345+ configurationService .getProperty ("webui.preview.brand.abbrev" ),
346+ currentItem == null ? "" : "hdl:" + currentItem .getHandle ());
339347
340- g2d .drawImage (brandImage , 0 , ysize , xsize , 20 , null );
341- }
348+ g2d .drawImage (brandImage , 0 , ysize , xsize , 20 , null );
349+ }
342350
351+ ByteArrayInputStream bais ;
352+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
353+ ImageIO .write (thumbnail , "jpeg" , baos );
354+ bais = new ByteArrayInputStream (baos .toByteArray ());
355+ }
343356
344- ByteArrayInputStream bais ;
345- // now create an input stream for the thumbnail buffer and return it
346- try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
347- ImageIO .write (thumbnail , "jpeg" , baos );
348- // now get the array
349- bais = new ByteArrayInputStream (baos .toByteArray ());
357+ return bais ;
358+ } finally {
359+ g2d .dispose ();
350360 }
351-
352- return bais ; // hope this gets written out before its garbage collected!
353361 }
354362
355363
0 commit comments