svn commit: r1370694 - /cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java

[email protected]
Newsgroups gmane.text.xml.cocoon.cvs
Message-ID <[email protected]>
Author: javier
Date: Wed Aug  8 08:19:09 2012
New Revision: 1370694

URL: http://svn.apache.org/viewvc?rev=1370694&view=rev
Log:
COCOON-2327: Remove old unused code and replace old java image processing

Modified:
    cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java

Modified: cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java?rev=1370694&r1=1370693&r2=1370694&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java (original)
+++ cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/reading/ImageReader.java Wed Aug  8 08:19:09 2012
@@ -27,7 +27,13 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Serializable;
+import java.util.Iterator;
 import java.util.Map;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageWriteParam;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.ImageOutputStream;
 import javax.swing.ImageIcon;
 
 import org.apache.avalon.framework.parameters.Parameters;
@@ -36,11 +42,6 @@ import org.apache.cocoon.environment.Sou
 import org.apache.commons.lang.SystemUtils;
 import org.xml.sax.SAXException;
 
-import com.sun.image.codec.jpeg.ImageFormatException;
-import com.sun.image.codec.jpeg.JPEGCodec;
-import com.sun.image.codec.jpeg.JPEGEncodeParam;
-import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
 /**
  * The <code>ImageReader</code> component is used to serve binary image data
  * in a sitemap pipeline. It makes use of HTTP Headers to determine if
@@ -104,9 +105,6 @@ final public class ImageReader extends R
     private static final boolean ENLARGE_DEFAULT = true;
     private static final boolean FIT_DEFAULT = false;
 
-    /* See http://developer.java.sun.com/developer/bugParade/bugs/4502892.html */
-    private static final boolean JVMBugFixed = SystemUtils.isJavaVersionAtLeast(1.4f);
-
     private int width;
     private int height;
     private float[] scaleColor = new float[3];
@@ -270,28 +268,6 @@ final public class ImageReader extends R
                                   + " expires: " + expires);
             }
 
-            /*
-             * NOTE (SM):
-             * Due to Bug Id 4502892 (which is found in *all* JVM implementations from
-             * 1.2.x and 1.3.x on all OS!), we must buffer the JPEG generation to avoid
-             * that connection resetting by the peer (user pressing the stop button,
-             * for example) crashes the entire JVM (yes, dude, the bug is *that* nasty
-             * since it happens in JPEG routines which are native!)
-             * I'm perfectly aware of the huge memory problems that this causes (almost
-             * doubling memory consumption for each image and making the GC work twice
-             * as hard) but it's *far* better than restarting the JVM every 2 minutes
-             * (since this is the average experience for image-intensive web application
-             * such as an image gallery).
-             * Please, go to the <a href="http://developer.java.sun.com/developer/bugParade/bugs/4502892.html">Sun Developers Connection</a>
-             * and vote this BUG as the one you would like fixed sooner rather than
-             * later and all this hack will automagically go away.
-             * Many deep thanks to Michael Hartle <[email protected]> for tracking
-             * this down and suggesting the workaround.
-             *
-             * UPDATE (SM):
-             * This appears to be fixed on JDK 1.4
-             */
-
             try {
                 byte content[] = readFully(inputStream);
                 ImageIcon icon = new ImageIcon(content);
@@ -328,25 +304,17 @@ final public class ImageReader extends R
                     colorFilter.filter(currentImage, currentImage);
                 }
 
-                // JVM Bug handling
-                if (JVMBugFixed) {
-                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
-                    JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
-                    p.setQuality(this.quality[0], true);
-                    encoder.setJPEGEncodeParam(p);
-                    encoder.encode(currentImage);
-                } else {
-                    ByteArrayOutputStream bstream = new ByteArrayOutputStream();
-                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bstream);
-                    JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
-                    p.setQuality(this.quality[0], true);
-                    encoder.setJPEGEncodeParam(p);
-                    encoder.encode(currentImage);
-                    out.write(bstream.toByteArray());
-                }
+                Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
+                ImageOutputStream ios = ImageIO.createImageOutputStream(out);
+                ImageWriter writer = writers.next();
+                writer.setOutput(ios);
+                ImageWriteParam p = writer.getDefaultWriteParam();
+                p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
+                p.setCompressionQuality(this.quality[0]);
+                writer.write(currentImage);
 
-                out.flush();
-            } catch (ImageFormatException e) {
+                ios.flush();
+            } catch (IOException e) {
                 throw new ProcessingException("Error reading the image. " +
                                               "Note that only JPEG images are currently supported.");
             } finally {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.