[libbluray-devel] DVBBufferedImage: error checking, fix exception type

hpi1 git at videolan.org
Wed Apr 6 10:23:47 CEST 2016


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Mon Apr  4 12:44:31 2016 +0300| [47b1281f807a51b239685263c7cfed40f53931ab] | committer: hpi1

DVBBufferedImage: error checking, fix exception type

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=47b1281f807a51b239685263c7cfed40f53931ab
---

 .../bdj/java/org/dvb/ui/DVBBufferedImage.java      |   94 ++++++++++++++++++--
 1 file changed, 89 insertions(+), 5 deletions(-)

diff --git a/src/libbluray/bdj/java/org/dvb/ui/DVBBufferedImage.java b/src/libbluray/bdj/java/org/dvb/ui/DVBBufferedImage.java
index 4b5728f..0307380 100644
--- a/src/libbluray/bdj/java/org/dvb/ui/DVBBufferedImage.java
+++ b/src/libbluray/bdj/java/org/dvb/ui/DVBBufferedImage.java
@@ -1,6 +1,7 @@
 /*
  * This file is part of libbluray
  * Copyright (C) 2010  William Hahne
+ * Copyright (C) 2016  Petri Hintukainen <phintuka at users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,6 +28,7 @@ import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.awt.image.ImageObserver;
 import java.awt.image.ImageProducer;
+import java.awt.image.RasterFormatException;
 
 public class DVBBufferedImage extends Image {
     public DVBBufferedImage(int width, int height)
@@ -36,19 +38,37 @@ public class DVBBufferedImage extends Image {
 
     public DVBBufferedImage(int width, int height, int type)
     {
+        if (type != TYPE_BASE && type != TYPE_ADVANCED) {
+            throw new IllegalArgumentException(err("Unknown image type " + type));
+        }
+        if (width <= 0 || height <= 0) {
+            throw new IllegalArgumentException(err("Invalid size:" + width + "x" + height));
+        }
         this.type = type;
+
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gd = ge.getDefaultScreenDevice();
         GraphicsConfiguration gc = gd.getDefaultConfiguration();
         bufferedImage = gc.createCompatibleImage(width, height);
+
+        if (bufferedImage == null) {
+            err("Error creating buffered image");
+        }
     }
 
     private DVBBufferedImage(BufferedImage image, int type) {
+        if (image == null) {
+            throw new IllegalArgumentException(err("null image"));
+        }
         this.type = type;
         this.bufferedImage = image;
     }
 
     public DVBGraphics createGraphics() {
+        if (bufferedImage == null) {
+            err("disposed");
+            return null;
+        }
         DVBGraphics gfx = new DVBGraphicsImpl(bufferedImage.createGraphics());
         gfx.type = type;
         return gfx;
@@ -59,18 +79,29 @@ public class DVBBufferedImage extends Image {
     }
 
     public void flush() {
-        bufferedImage.flush();
+        if (bufferedImage != null) {
+            bufferedImage.flush();
+        }
     }
 
     public Graphics getGraphics() {
+        if (bufferedImage == null) {
+            return null;
+        }
         return bufferedImage.getGraphics();
     }
 
     public int getHeight() {
+        if (bufferedImage == null) {
+            return -1;
+        }
         return bufferedImage.getHeight();
     }
 
     public int getHeight(ImageObserver observer) {
+        if (bufferedImage == null) {
+            return -1;
+        }
         return bufferedImage.getHeight(observer);
     }
 
@@ -79,49 +110,102 @@ public class DVBBufferedImage extends Image {
     }
 
     public Object getProperty(String name, ImageObserver observer) {
+        if (bufferedImage == null) {
+            return null;
+        }
         return bufferedImage.getProperty(name, observer);
     }
 
     public int getRGB(int x, int y) {
+        if (bufferedImage == null) {
+             throw new ArrayIndexOutOfBoundsException(err("disposed"));
+        }
+
+        int width = bufferedImage.getWidth();
+        int height = bufferedImage.getHeight();
+        if (x < 0 || y < 0 || x >= width || y >= height) {
+            throw new ArrayIndexOutOfBoundsException(err("getRGB out of bounds"));
+        }
+
         return bufferedImage.getRGB(x, y);
     }
 
     public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
+        if (bufferedImage == null) {
+             throw new ArrayIndexOutOfBoundsException(err("disposed"));
+        }
+
+        int width = bufferedImage.getWidth();
+        int height = bufferedImage.getHeight();
+        if (startX < 0 || startY < 0 || startX + w >= width || startY + height >= height) {
+            throw new ArrayIndexOutOfBoundsException(err("getRGB out of bounds"));
+        }
+
         return bufferedImage.getRGB(startX, startY, w, h, rgbArray, offset, scansize);
     }
 
     public Image getScaledInstance(int width, int height, int hints) {
+        if (bufferedImage == null) {
+            return null;
+        }
         return bufferedImage.getScaledInstance(width, height, hints);
     }
 
     public ImageProducer getSource() {
+        if (bufferedImage == null) {
+            return null;
+        }
         return bufferedImage.getSource();
     }
 
     public DVBBufferedImage getSubimage(int x, int y, int w, int h) throws DVBRasterFormatException {
-        BufferedImage subImage = bufferedImage.getSubimage(x, y, w, h);
-        return new DVBBufferedImage(subImage, this.type);
+        if (bufferedImage == null) {
+            return null;
+        }
+        try {
+            BufferedImage subImage = bufferedImage.getSubimage(x, y, w, h);
+            return new DVBBufferedImage(subImage, this.type);
+        } catch (RasterFormatException e) {
+            throw new DVBRasterFormatException(err(e.getMessage()));
+        }
     }
 
     public int getWidth() {
+        if (bufferedImage == null) {
+            return -1;
+        }
         return bufferedImage.getWidth();
     }
 
     public int getWidth(ImageObserver observer) {
+        if (bufferedImage == null) {
+            return -1;
+        }
         return bufferedImage.getWidth(observer);
     }
 
     public void setRGB(int x, int y, int rgb) {
-        this.bufferedImage.setRGB(x, y, rgb);
+        if (bufferedImage == null) {
+            throw new ArrayIndexOutOfBoundsException(err("disposed"));
+        }
+        bufferedImage.setRGB(x, y, rgb);
     }
 
     public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) {
+        if (bufferedImage == null) {
+            throw new ArrayIndexOutOfBoundsException(err("disposed"));
+        }
         bufferedImage.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
     }
 
     public String toString()
     {
-        return new String("DVBBufferedImage");
+        return getClass().getName() + "[img=" + bufferedImage + "]";
+    }
+
+    private String err(String msg) {
+        System.err.println("DVBBufferedImage: " + msg);
+        return msg;
     }
 
     public static final int TYPE_ADVANCED = 20;



More information about the libbluray-devel mailing list