private BufferedImage decode() throws IOException {

            ImageReadParam readParam = null;
            if (getDecode() != null) {
                // we have to allocate our own buffered image so that we can
                // install our colour model which will do the desired decode
                readParam = new ImageReadParam();
                SampleModel sm =
                        cm.createCompatibleSampleModel (getWidth (), getHeight ());
                final WritableRaster raster =
                        Raster.createWritableRaster(sm, new Point(0, 0));
                readParam.setDestination(new BufferedImage(cm, raster, true, null));
            }

            final Iterator<ImageReader> jpegReaderIt =
                        ImageIO.getImageReadersByFormatName("jpeg");
            IIOException lastIioEx = null;
            while (jpegReaderIt.hasNext()) {
                try {
                    final ImageReader jpegReader = jpegReaderIt.next();
                    jpegReader.setInput(ImageIO.createImageInputStream(
                            new ByteBufferInputStream(jpegData)), true, false);
                    return readImage(jpegReader, readParam);
                } catch (IIOException e) {
                    // its most likely complaining about an unsupported image
                    // type; hopefully the next image reader will be able to
                    // understand it
                    jpegData.reset();
                    lastIioEx = e;
                }
            }

            throw lastIioEx;

        }