From 4954bbe0f1a0709b1c9c4d3c692e70a87c263489 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 13 Nov 2006 11:13:55 +0000 Subject: [PATCH] Avoid an exception when getting the default bitmap image size on some JVMs git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@474253 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/apache/poi/hslf/model/Picture.java | 17 ++++++++++++----- .../apache/poi/hslf/usermodel/TestPictures.java | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java index f1b9b3a4df..3be79efbd1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java @@ -122,12 +122,19 @@ public class Picture extends SimpleShape { public void setDefaultSize(){ PictureData pict = getPictureData(); if (pict instanceof Bitmap){ + BufferedImage img = null; try { - BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData())); - if(img != null) setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight())); - else setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); - } catch (IOException e){ - ; + img = ImageIO.read(new ByteArrayInputStream(pict.getData())); + } + catch (IOException e){} + catch (NegativeArraySizeException ne) {} + + if(img != null) { + // Valid image, set anchor from it + setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight())); + } else { + // Invalid image, go with the default metafile size + setAnchor(new java.awt.Rectangle(0, 0, 200, 200)); } } else { //default size of a metafile picture is 200x200 diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index 6767a299b0..91f4f1f509 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -257,6 +257,11 @@ public class TestPictures extends TestCase{ Slide slide = ppt.createSlide(); File img = new File(cwd, "sci_cec.dib"); + + // Check we can read the test DIB image + assertTrue(img.exists()); + + // Add the image int idx = ppt.addPicture(img, Picture.DIB); Picture pict = new Picture(idx); assertEquals(idx, pict.getPictureIndex()); -- 2.39.5