From: Keiron Liddle Date: Fri, 8 Nov 2002 10:48:00 +0000 (+0000) Subject: handle image stream better, try to make sure only X-Git-Tag: Alt-Design-integration-base~340 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=706230140ec1ac44971b9d74a71926a396c46360;p=xmlgraphics-fop.git handle image stream better, try to make sure only opened once and properly closed gif image broken until it can be loaded from a stream git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195455 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/fo/FOUserAgent.java b/src/org/apache/fop/fo/FOUserAgent.java index 543b46191..adb2fc8c9 100644 --- a/src/org/apache/fop/fo/FOUserAgent.java +++ b/src/org/apache/fop/fo/FOUserAgent.java @@ -13,9 +13,10 @@ import org.apache.fop.render.RendererContext; import org.apache.avalon.framework.logger.LogEnabled; import org.apache.avalon.framework.logger.Logger; -import org.w3c.dom.*; - import java.util.HashMap; +import java.io.InputStream; + +import org.w3c.dom.Document; /** * The User Agent for fo. @@ -54,6 +55,14 @@ public class FOUserAgent implements LogEnabled { return base; } + /** + * Get an input stream for a reference. + * Temporary solution until API better. + */ + public InputStream getStream(String uri) { + return null; + } + public float getPixelUnitToMillimeter() { return 0.35277777777777777778f; } diff --git a/src/org/apache/fop/image/AbstractFopImage.java b/src/org/apache/fop/image/AbstractFopImage.java index ef9de7992..3e3b3220d 100644 --- a/src/org/apache/fop/image/AbstractFopImage.java +++ b/src/org/apache/fop/image/AbstractFopImage.java @@ -11,6 +11,7 @@ package org.apache.fop.image; import java.net.URL; import java.awt.color.ColorSpace; import java.awt.color.ICC_Profile; +import java.io.InputStream; // FOP import org.apache.fop.pdf.PDFColor; @@ -38,9 +39,9 @@ public abstract class AbstractFopImage implements FopImage { protected int m_height = 0; /** - * Image URL. + * Image input stream. */ - protected URL m_href = null; + protected InputStream inputStream = null; /** * ImageReader object (to obtain image header informations). @@ -85,12 +86,12 @@ public abstract class AbstractFopImage implements FopImage { *
  • image height * * The image data isn't kept in memory. - * @param href image URL + * @param input input stream * imgReader ImageReader object * @return a new FopImage object */ - public AbstractFopImage(URL href, FopImage.ImageInfo info) { - this.m_href = href; + public AbstractFopImage(FopImage.ImageInfo info) { + this.inputStream = info.inputStream; this.imageInfo = info; if(this.imageInfo.width != -1) { m_width = imageInfo.width; @@ -146,14 +147,6 @@ public abstract class AbstractFopImage implements FopImage { return false; } - /** - * Return the image URL. - * @return the image URL (as String) - */ - public String getURL() { - return this.m_href.toString(); - } - /** * Return the image width. * @return the image width @@ -178,6 +171,9 @@ public abstract class AbstractFopImage implements FopImage { return this.m_colorSpace; } + /** + * Get ICC profile for this image. + */ public ICC_Profile getICCProfile() { return null; } diff --git a/src/org/apache/fop/image/BmpImage.java b/src/org/apache/fop/image/BmpImage.java index 583cc756e..c6d17d8ff 100644 --- a/src/org/apache/fop/image/BmpImage.java +++ b/src/org/apache/fop/image/BmpImage.java @@ -25,8 +25,8 @@ import org.apache.fop.image.analyser.ImageReader; import org.apache.fop.fo.FOUserAgent; public class BmpImage extends AbstractFopImage { - public BmpImage(URL href, FopImage.ImageInfo imgReader) { - super(href, imgReader); + public BmpImage(FopImage.ImageInfo imgReader) { + super(imgReader); } protected boolean loadBitmap(FOUserAgent ua) { @@ -34,13 +34,11 @@ public class BmpImage extends AbstractFopImage { int hpos = 22; // offset positioning for w and height in bmp files int[] headermap = new int[54]; int filepos = 0; - InputStream file = null; byte palette[] = null; try { - file = this.m_href.openStream(); boolean eof = false; while ((!eof) && (filepos < 54)) { - int input = file.read(); + int input = inputStream.read(); if (input == -1) eof = true; else @@ -54,7 +52,7 @@ public class BmpImage extends AbstractFopImage { while (!eof && countr < palettesize) { int count2 = 2; while (!eof && count2 >= -1) { - int input = file.read(); + int input = inputStream.read(); if (input == -1) eof = true; else if (count2 >= 0) { @@ -69,7 +67,7 @@ public class BmpImage extends AbstractFopImage { } } catch (IOException e) { ua.getLogger().error("Error while loading image " - + this.m_href.toString() + " : " + + "" + " : " + e.getClass() + " - " + e.getMessage(), e); return false; @@ -94,7 +92,7 @@ public class BmpImage extends AbstractFopImage { else if (this.m_bitsPerPixel == 4 || this.m_bitsPerPixel == 8) bytes = this.m_width / (8 / this.m_bitsPerPixel); else { - ua.getLogger().error("Image (" + this.m_href.toString() + ua.getLogger().error("Image (" + "" + ") has " + this.m_bitsPerPixel + " which is not a supported BMP format."); return false; @@ -112,13 +110,14 @@ public class BmpImage extends AbstractFopImage { try { int input; int count = 0; - file.skip((long)(imagestart - filepos)); - while ((input = file.read()) != -1) + inputStream.skip((long)(imagestart - filepos)); + while ((input = inputStream.read()) != -1) temp[count++] = input; - file.close(); + inputStream.close(); + inputStream = null; } catch (IOException e) { ua.getLogger().error("Error while loading image " - + this.m_href.toString() + " : " + + "" + " : " + e.getClass() + " - " + e.getMessage(), e); return false; diff --git a/src/org/apache/fop/image/EPSImage.java b/src/org/apache/fop/image/EPSImage.java index 88b0582f3..b6d207555 100644 --- a/src/org/apache/fop/image/EPSImage.java +++ b/src/org/apache/fop/image/EPSImage.java @@ -35,14 +35,14 @@ public class EPSImage extends AbstractFopImage { /** * Initialize docName and bounding box */ - private void init(URL href) { + private void init(String name) { bbox = new int[4]; bbox[0] = 0; bbox[1] = 0; bbox[2] = 0; bbox[3] = 0; - docName = href.toString(); + docName = name; } /** @@ -59,9 +59,9 @@ public class EPSImage extends AbstractFopImage { return bbox; } - public EPSImage(URL href, FopImage.ImageInfo imgInfo) { - super(href, imgInfo); - init(href); + public EPSImage(FopImage.ImageInfo imgInfo) { + super(imgInfo); + init(""); if (imgInfo.data instanceof EPSData) { epsData = (EPSData) imgInfo.data; bbox = new int[4]; diff --git a/src/org/apache/fop/image/FopImage.java b/src/org/apache/fop/image/FopImage.java index a09272a2a..229bec44a 100644 --- a/src/org/apache/fop/image/FopImage.java +++ b/src/org/apache/fop/image/FopImage.java @@ -32,9 +32,6 @@ public interface FopImage { */ public boolean load(int type, FOUserAgent ua); - // Ressource location - public String getURL(); - // image size public int getWidth(); public int getHeight(); @@ -65,7 +62,7 @@ public interface FopImage { public int getRessourceBytesSize(); public static class ImageInfo { - public InputStream stream; + public InputStream inputStream; public int width; public int height; public Object data; diff --git a/src/org/apache/fop/image/FopImageConsumer.java b/src/org/apache/fop/image/FopImageConsumer.java index 15b8c5ca3..b4ed55b91 100644 --- a/src/org/apache/fop/image/FopImageConsumer.java +++ b/src/org/apache/fop/image/FopImageConsumer.java @@ -134,3 +134,4 @@ public class FopImageConsumer implements ImageConsumer { } } + diff --git a/src/org/apache/fop/image/GifImage.java b/src/org/apache/fop/image/GifImage.java index 32495c0e0..31c696627 100644 --- a/src/org/apache/fop/image/GifImage.java +++ b/src/org/apache/fop/image/GifImage.java @@ -13,6 +13,7 @@ import java.awt.image.ImageProducer; import java.awt.image.ColorModel; import java.awt.image.IndexColorModel; import java.awt.color.ColorSpace; +import java.io.InputStream; // FOP import org.apache.fop.pdf.PDFColor; @@ -27,14 +28,19 @@ import org.apache.fop.fo.FOUserAgent; * @see FopImage */ public class GifImage extends AbstractFopImage { - public GifImage(URL href, FopImage.ImageInfo imgReader) { - super(href, imgReader); + public GifImage(FopImage.ImageInfo imgReader) { + super(imgReader); } protected boolean loadBitmap(FOUserAgent ua) { int[] tmpMap = null; + try { - ImageProducer ip = (ImageProducer) this.m_href.getContent(); + ImageProducer ip = null; + // todo: how to load gif image from stream + //ip = (ImageProducer) inputStream.getContent(); + inputStream.close(); + inputStream = null; FopImageConsumer consumer = new FopImageConsumer(ip); ip.startProduction(consumer); @@ -66,21 +72,18 @@ public class GifImage extends AbstractFopImage { } else if (transparencyType == java.awt.Transparency.BITMASK) { if (cm instanceof IndexColorModel) { + IndexColorModel indexcm = (IndexColorModel) cm; this.m_isTransparent = false; - byte[] alphas = new byte[ - ((IndexColorModel) cm).getMapSize()]; - byte[] reds = new byte[ - ((IndexColorModel) cm).getMapSize()]; - byte[] greens = new byte[ - ((IndexColorModel) cm).getMapSize()]; - byte[] blues = new byte[ - ((IndexColorModel) cm).getMapSize()]; - ((IndexColorModel) cm).getAlphas(alphas); - ((IndexColorModel) cm).getReds(reds); - ((IndexColorModel) cm).getGreens(greens); - ((IndexColorModel) cm).getBlues(blues); + byte[] alphas = new byte[indexcm.getMapSize()]; + byte[] reds = new byte[indexcm.getMapSize()]; + byte[] greens = new byte[indexcm.getMapSize()]; + byte[] blues = new byte[indexcm.getMapSize()]; + indexcm.getAlphas(alphas); + indexcm.getReds(reds); + indexcm.getGreens(greens); + indexcm.getBlues(blues); for (int i = 0; - i < ((IndexColorModel) cm).getMapSize(); + i < indexcm.getMapSize(); i++) { if ((alphas[i] & 0xFF) == 0) { this.m_isTransparent = true; @@ -114,7 +117,7 @@ public class GifImage extends AbstractFopImage { } } catch (Exception ex) { ua.getLogger().error("Error while loading image " - + this.m_href.toString() + " : " + + "" + " : " + ex.getClass() + " - " + ex.getMessage(), ex); return false; diff --git a/src/org/apache/fop/image/ImageFactory.java b/src/org/apache/fop/image/ImageFactory.java index 8189c0559..a4fc8c92c 100644 --- a/src/org/apache/fop/image/ImageFactory.java +++ b/src/org/apache/fop/image/ImageFactory.java @@ -10,6 +10,7 @@ package org.apache.fop.image; // Java import java.io.IOException; import java.io.InputStream; +import java.io.BufferedInputStream; import java.io.File; import java.net.URL; import java.net.MalformedURLException; @@ -59,7 +60,6 @@ public class ImageFactory { */ // Get the absolute URL URL absoluteURL = null; - InputStream imgIS = null; href = href.trim(); if (href.startsWith("url(") && (href.indexOf(")") != -1)) { href = href.substring(4, href.indexOf(")")).trim(); @@ -102,9 +102,92 @@ public class ImageFactory { protected static FopImage loadImage(String href, String baseURL, FOUserAgent ua) { Logger log = ua.getLogger(); + + InputStream imgIS = openStream(href, baseURL, ua); + + // If not, check image type + FopImage.ImageInfo imgInfo = null; + try { + imgInfo = ImageReaderFactory.make( + href, imgIS, ua); + } catch (Exception e) { + log.error("Error while recovering Image Informations (" + + href + ") : " + e.getMessage(), e); + return null; + } + if (imgInfo == null) { + try { + imgIS.close(); + imgIS = null; + } catch (Exception e) { + } + log.error("No ImageReader for this type of image (" + + href + ")"); + return null; + } + // Associate mime-type to FopImage class + String imgMimeType = imgInfo.mimeType; + String imgClassName = getImageClassName(imgMimeType); + if (imgClassName == null) { + log.error("Unsupported image type (" + + href + ") : " + imgMimeType); + return null; + } + + // load the right image class + // return new + Object imageInstance = null; + Class imageClass = null; + try { + imageClass = Class.forName(imgClassName); + Class[] imageConstructorParameters = new Class[1]; + imageConstructorParameters[0] = org.apache.fop.image.FopImage.ImageInfo.class; + Constructor imageConstructor = + imageClass.getDeclaredConstructor( + imageConstructorParameters); + Object[] initArgs = new Object[1]; + initArgs[0] = imgInfo; + imageInstance = imageConstructor.newInstance(initArgs); + } catch (java.lang.reflect.InvocationTargetException ex) { + Throwable t = ex.getTargetException(); + String msg; + if (t != null) { + msg = t.getMessage(); + } else { + msg = ex.getMessage(); + } + log.error("Error creating FopImage object (" + + href + ") : " + msg, (t == null) ? ex:t); + return null; + } + catch (Exception ex) { + log.error("Error creating FopImage object (" + + href + ") : " + ex.getMessage(), ex); + return null; + } + if (!(imageInstance instanceof org.apache.fop.image.FopImage)) { + log.error("Error creating FopImage object (" + + href + ") : " + "class " + + imageClass.getName() + " doesn't implement org.apache.fop.image.FopImage interface"); + return null; + } + return (FopImage) imageInstance; + } + + /** + * create an FopImage objects. + * @param href image URL as a String + * @return a new FopImage object + */ + protected static InputStream openStream(String href, String baseURL, + FOUserAgent ua) { + Logger log = ua.getLogger(); // Get the absolute URL URL absoluteURL = null; - InputStream imgIS = null; + InputStream imgIS = ua.getStream(href); + if(imgIS != null) { + return imgIS; + } try { // try url as complete first, this can cause // a problem with relative uri's if there is an @@ -138,31 +221,23 @@ public class ImageFactory { } } + BufferedInputStream bis = null; // If not, check image type FopImage.ImageInfo imgInfo = null; try { if (imgIS == null) { imgIS = absoluteURL.openStream(); } - imgInfo = ImageReaderFactory.make( - absoluteURL.toExternalForm(), imgIS, ua); + bis = new BufferedInputStream(imgIS); } catch (Exception e) { - log.error("Error while recovering Image Informations (" + - absoluteURL.toString() + ") : " + e.getMessage(), e); + log.error("Error while opening stream for (" + + href + ") : " + e.getMessage(), e); return null; } - finally { if (imgIS != null) { - try { - imgIS.close(); - } catch (IOException e) {} - } - } if (imgInfo == null) { - log.error("No ImageReader for this type of image (" + - absoluteURL.toString() + ")"); - return null; - } - // Associate mime-type to FopImage class - String imgMimeType = imgInfo.mimeType; + return bis; + } + + private static String getImageClassName(String imgMimeType) { String imgClassName = null; if ("image/gif".equals(imgMimeType)) { imgClassName = "org.apache.fop.image.GifImage"; @@ -189,54 +264,8 @@ public class ImageFactory { } else if ("text/xml".equals(imgMimeType)) { imgClassName = "org.apache.fop.image.XMLImage"; } - if (imgClassName == null) { - log.error("Unsupported image type (" + - absoluteURL.toString() + ") : " + imgMimeType); - return null; - } - - // load the right image class - // return new - Object imageInstance = null; - Class imageClass = null; - try { - imageClass = Class.forName(imgClassName); - Class[] imageConstructorParameters = new Class[2]; - imageConstructorParameters[0] = java.net.URL.class; - imageConstructorParameters[1] = org.apache.fop.image.FopImage.ImageInfo.class; - Constructor imageConstructor = - imageClass.getDeclaredConstructor( - imageConstructorParameters); - Object[] initArgs = new Object[2]; - initArgs[0] = absoluteURL; - initArgs[1] = imgInfo; - imageInstance = imageConstructor.newInstance(initArgs); - } catch (java.lang.reflect.InvocationTargetException ex) { - Throwable t = ex.getTargetException(); - String msg; - if (t != null) { - msg = t.getMessage(); - } else { - msg = ex.getMessage(); - } - log.error("Error creating FopImage object (" + - absoluteURL.toString() + ") : " + msg, (t == null) ? ex:t); - return null; - } - catch (Exception ex) { - log.error("Error creating FopImage object (" + - absoluteURL.toString() + ") : " + ex.getMessage(), ex); - return null; - } - if (!(imageInstance instanceof org.apache.fop.image.FopImage)) { - log.error("Error creating FopImage object (" + - absoluteURL.toString() + ") : " + "class " + - imageClass.getName() + " doesn't implement org.apache.fop.image.FopImage interface"); - return null; - } - return (FopImage) imageInstance; + return imgClassName; } - } class BasicImageCache implements ImageCache { diff --git a/src/org/apache/fop/image/JAIImage.java b/src/org/apache/fop/image/JAIImage.java index 1a1a42e43..c4da068e6 100644 --- a/src/org/apache/fop/image/JAIImage.java +++ b/src/org/apache/fop/image/JAIImage.java @@ -10,7 +10,7 @@ package org.apache.fop.image; // Java import java.net.URL; import java.io.InputStream; -import java.io.BufferedInputStream; +import java.io.InputStream; // AWT import java.awt.image.ColorModel; @@ -36,20 +36,17 @@ import org.apache.fop.image.analyser.ImageReader; */ public class JAIImage extends AbstractFopImage { - public JAIImage(URL href, FopImage.ImageInfo imgReader) { - super(href, imgReader); + public JAIImage(FopImage.ImageInfo imgReader) { + super(imgReader); } protected void loadImage() { try { - InputStream inputStream = this.m_href.openStream(); - /* - * BufferedInputStream inputStream = this.m_imageReader.getInputStream(); - * inputStream.reset(); - */ com.sun.media.jai.codec.FileCacheSeekableStream seekableInput = new FileCacheSeekableStream(inputStream); RenderedOp imageOp = JAI.create("stream", seekableInput); + inputStream.close(); + inputStream = null; this.m_height = imageOp.getHeight(); this.m_width = imageOp.getWidth(); @@ -138,7 +135,7 @@ public class JAIImage extends AbstractFopImage { } catch (Exception ex) { /*throw new FopImageException("Error while loading image " - + this.m_href.toString() + " : " + + "" + " : " + ex.getClass() + " - " + ex.getMessage()); */} diff --git a/src/org/apache/fop/image/JimiImage.java b/src/org/apache/fop/image/JimiImage.java index 4cc780a6f..8369ab66e 100644 --- a/src/org/apache/fop/image/JimiImage.java +++ b/src/org/apache/fop/image/JimiImage.java @@ -13,6 +13,7 @@ import java.awt.image.ImageProducer; import java.awt.image.ColorModel; import java.awt.image.IndexColorModel; import java.awt.color.ColorSpace; +import java.io.InputStream; // Jimi import com.sun.jimi.core.*; @@ -32,8 +33,8 @@ import org.apache.avalon.framework.logger.Logger; * @see FopImage */ public class JimiImage extends AbstractFopImage { - public JimiImage(URL href, FopImage.ImageInfo imgReader) { - super(href, imgReader); + public JimiImage(FopImage.ImageInfo imgReader) { + super(imgReader); try { Class c = Class.forName("com.sun.jimi.core.Jimi"); } catch (ClassNotFoundException e) { @@ -61,7 +62,7 @@ public class JimiImage extends AbstractFopImage { int[] tmpMap = null; try { ImageProducer ip = - Jimi.getImageProducer(this.m_href.openStream(), + Jimi.getImageProducer(inputStream, Jimi.SYNCHRONOUS | Jimi.IN_MEMORY); FopImageConsumer consumer = new FopImageConsumer(ip); ip.startProduction(consumer); @@ -72,6 +73,9 @@ public class JimiImage extends AbstractFopImage { this.m_height = consumer.getHeight(); this.m_width = consumer.getWidth(); + inputStream.close(); + inputStream = null; + try { tmpMap = consumer.getImage(); } catch (Exception ex) { @@ -139,7 +143,7 @@ public class JimiImage extends AbstractFopImage { } } catch (Throwable ex) { log.error("Error while loading image " - + this.m_href.toString(), ex); + + "", ex); return; } diff --git a/src/org/apache/fop/image/JpegImage.java b/src/org/apache/fop/image/JpegImage.java index 2fa105b52..62b64bac1 100644 --- a/src/org/apache/fop/image/JpegImage.java +++ b/src/org/apache/fop/image/JpegImage.java @@ -33,8 +33,8 @@ public class JpegImage extends AbstractFopImage { boolean found_icc_profile = false; boolean found_dimensions = false; - public JpegImage(URL href, FopImage.ImageInfo imgReader) { - super(href, imgReader); + public JpegImage(FopImage.ImageInfo imgReader) { + super(imgReader); } protected boolean loadOriginalData(FOUserAgent ua) { @@ -47,14 +47,16 @@ public class JpegImage extends AbstractFopImage { boolean cont = true; try { - inStream = this.m_href.openStream(); + inStream = inputStream; while ((bytes_read = inStream.read(readBuf)) != -1) { baos.write(readBuf, 0, bytes_read); } + inputStream.close(); + inputStream = null; } catch (java.io.IOException ex) { ua.getLogger().error("Error while loading image " + - this.m_href.toString() + " : " + ex.getClass() + + "" + " : " + ex.getClass() + " - " + ex.getMessage(), ex); return false; } @@ -94,7 +96,7 @@ public class JpegImage extends AbstractFopImage { ColorSpace.CS_CIEXYZ); } else { ua.getLogger().error("Unknown ColorSpace for image: " - + this.m_href.toString()); + + ""); return false; } @@ -140,7 +142,7 @@ public class JpegImage extends AbstractFopImage { } } else { ua.getLogger().error( "1 Error while loading image " + - this.m_href.toString() + + "" + " : JpegImage - Invalid JPEG Header."); return false; } @@ -150,14 +152,14 @@ public class JpegImage extends AbstractFopImage { iccStream.write(align); } catch (Exception e) { ua.getLogger().error( "1 Error while loading image " + - this.m_href.toString() + " : " + + "" + " : " + e.getMessage(), e); return false; } iccProfile = ICC_Profile.getInstance(iccStream.toByteArray()); } else if(this.m_colorSpace == null) { ua.getLogger().error("ColorSpace not specified for image: " - + this.m_href.toString()); + + ""); return false; } return true; diff --git a/src/org/apache/fop/image/XMLImage.java b/src/org/apache/fop/image/XMLImage.java index aa74eb0c9..d544efbed 100644 --- a/src/org/apache/fop/image/XMLImage.java +++ b/src/org/apache/fop/image/XMLImage.java @@ -10,6 +10,7 @@ package org.apache.fop.image; // Java import java.net.URL; import org.w3c.dom.Document; +import java.io.InputStream; // FOP import org.apache.fop.apps.Driver; @@ -25,8 +26,8 @@ public class XMLImage extends AbstractFopImage { Document doc; String ns = ""; - public XMLImage(URL href, FopImage.ImageInfo imgInfo) { - super(href, imgInfo); + public XMLImage(FopImage.ImageInfo imgInfo) { + super(imgInfo); if(imgInfo.data instanceof Document) { doc = (Document)imgInfo.data; loaded = loaded | ORIGINAL_DATA; diff --git a/src/org/apache/fop/image/analyser/BMPReader.java b/src/org/apache/fop/image/analyser/BMPReader.java index b15fd731c..7a1ea9afe 100644 --- a/src/org/apache/fop/image/analyser/BMPReader.java +++ b/src/org/apache/fop/image/analyser/BMPReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -26,13 +26,15 @@ public class BMPReader implements ImageReader { protected static final int BMP_SIG_LENGTH = 26; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(bis); boolean supported = ((header[0] == (byte) 0x42) && (header[1] == (byte) 0x4d)); if (supported) { - return getDimension(header); + FopImage.ImageInfo info = getDimension(header); + info.inputStream = bis; + return info; } else { return null; } @@ -69,7 +71,7 @@ public class BMPReader implements ImageReader { return info; } - private byte[] getDefaultHeader(BufferedInputStream imageStream) + private byte[] getDefaultHeader(InputStream imageStream) throws IOException { byte[] header = new byte[BMP_SIG_LENGTH]; try { @@ -87,4 +89,4 @@ public class BMPReader implements ImageReader { return header; } -} \ No newline at end of file +} diff --git a/src/org/apache/fop/image/analyser/EPSReader.java b/src/org/apache/fop/image/analyser/EPSReader.java index 4a0621a76..4cb7f0a5b 100644 --- a/src/org/apache/fop/image/analyser/EPSReader.java +++ b/src/org/apache/fop/image/analyser/EPSReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -27,7 +27,7 @@ public class EPSReader implements ImageReader { private static final byte[] BOUNDINGBOX = "%%BoundingBox: ".getBytes(); /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { boolean isEPS = false; @@ -73,6 +73,11 @@ public class EPSReader implements ImageReader { if (data.bbox != null) { info.width = (int) (data.bbox[2] - data.bbox[0]); info.height = (int) (data.bbox[3] - data.bbox[1]); + + // image data read + bis.close(); + info.inputStream = null; + return info; } else { // Ain't eps if no BoundingBox @@ -108,7 +113,7 @@ public class EPSReader implements ImageReader { * @param data EPSData object to write the results to * @exception IOException If an I/O error occurs */ - private void readEPSImage(BufferedInputStream bis, EPSImage.EPSData data) + private void readEPSImage(InputStream bis, EPSImage.EPSData data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] file; diff --git a/src/org/apache/fop/image/analyser/GIFReader.java b/src/org/apache/fop/image/analyser/GIFReader.java index 4b4a0cf57..ac7cadc54 100644 --- a/src/org/apache/fop/image/analyser/GIFReader.java +++ b/src/org/apache/fop/image/analyser/GIFReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -25,7 +25,7 @@ public class GIFReader implements ImageReader { private static final int GIF_SIG_LENGTH = 10; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(bis); boolean supported = ((header[0] == 'G') @@ -37,6 +37,7 @@ public class GIFReader implements ImageReader { if (supported) { FopImage.ImageInfo info = getDimension(header); info.mimeType = getMimeType(); + info.inputStream = bis; return info; } else { return null; @@ -65,7 +66,7 @@ public class GIFReader implements ImageReader { return info; } - private byte[] getDefaultHeader(BufferedInputStream imageStream) + private byte[] getDefaultHeader(InputStream imageStream) throws IOException { byte[] header = new byte[GIF_SIG_LENGTH]; try { @@ -83,4 +84,5 @@ public class GIFReader implements ImageReader { return header; } -} \ No newline at end of file +} + diff --git a/src/org/apache/fop/image/analyser/ImageReader.java b/src/org/apache/fop/image/analyser/ImageReader.java index 8137cc239..576874aa9 100644 --- a/src/org/apache/fop/image/analyser/ImageReader.java +++ b/src/org/apache/fop/image/analyser/ImageReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -35,7 +35,7 @@ public interface ImageReader { * @return true if image type is the handled one * @exception IOException if an I/O error occurs */ - FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException; diff --git a/src/org/apache/fop/image/analyser/ImageReaderFactory.java b/src/org/apache/fop/image/analyser/ImageReaderFactory.java index 124182a0b..894ce40e9 100644 --- a/src/org/apache/fop/image/analyser/ImageReaderFactory.java +++ b/src/org/apache/fop/image/analyser/ImageReaderFactory.java @@ -8,7 +8,6 @@ package org.apache.fop.image.analyser; // Java import java.io.InputStream; -import java.io.BufferedInputStream; import java.io.IOException; import java.util.ArrayList; @@ -60,11 +59,10 @@ public class ImageReaderFactory { FOUserAgent ua) { ImageReader reader; - BufferedInputStream bis = new BufferedInputStream(in); try { for (int count = 0; count < formats.size(); count++) { reader = (ImageReader) formats.get(count); - FopImage.ImageInfo info = reader.verifySignature(uri, bis, ua); + FopImage.ImageInfo info = reader.verifySignature(uri, in, ua); if (info != null) { return info; } diff --git a/src/org/apache/fop/image/analyser/JPEGReader.java b/src/org/apache/fop/image/analyser/JPEGReader.java index bddbc2040..90652b14e 100644 --- a/src/org/apache/fop/image/analyser/JPEGReader.java +++ b/src/org/apache/fop/image/analyser/JPEGReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -42,7 +42,7 @@ public class JPEGReader implements ImageReader { private static final int JPG_SIG_LENGTH = 2; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, InputStream fis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(fis); boolean supported = ((header[0] == (byte) 0xff) @@ -50,6 +50,7 @@ public class JPEGReader implements ImageReader { if (supported) { FopImage.ImageInfo info = getDimension(fis); info.mimeType = getMimeType(); + info.inputStream = fis; return info; } else { return null; @@ -65,7 +66,7 @@ public class JPEGReader implements ImageReader { return "image/jpeg"; } - private byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + private byte[] getDefaultHeader(InputStream imageStream) throws IOException { byte[] header = new byte[JPG_SIG_LENGTH]; try { imageStream.mark(JPG_SIG_LENGTH + 1); @@ -82,12 +83,13 @@ public class JPEGReader implements ImageReader { return header; } - private FopImage.ImageInfo getDimension(BufferedInputStream imageStream) throws IOException { + private FopImage.ImageInfo getDimension(InputStream imageStream) throws IOException { FopImage.ImageInfo info = new FopImage.ImageInfo(); try { + imageStream.mark(imageStream.available()); int marker = NULL; long length, skipped; - outer: +outer: while (imageStream.available() > 0) { while ((marker = imageStream.read()) != MARK) { //nop, simply skip @@ -118,6 +120,7 @@ public class JPEGReader implements ImageReader { } } } + imageStream.reset(); } catch (IOException ioe) { try { imageStream.reset(); @@ -129,13 +132,13 @@ public class JPEGReader implements ImageReader { return info; } - private int read2bytes(BufferedInputStream imageStream) throws IOException { + private int read2bytes(InputStream imageStream) throws IOException { int byte1 = imageStream.read(); int byte2 = imageStream.read(); return (int) ((byte1 << 8) | byte2); } - private long skip(BufferedInputStream imageStream, long n) throws IOException { + private long skip(InputStream imageStream, long n) throws IOException { long discarded = 0; while (discarded != n) { imageStream.read(); @@ -144,4 +147,5 @@ public class JPEGReader implements ImageReader { return discarded; // scope for exception } -} \ No newline at end of file +} + diff --git a/src/org/apache/fop/image/analyser/PNGReader.java b/src/org/apache/fop/image/analyser/PNGReader.java index 277ed2d5e..e1ef0d7e6 100644 --- a/src/org/apache/fop/image/analyser/PNGReader.java +++ b/src/org/apache/fop/image/analyser/PNGReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -25,7 +25,7 @@ public class PNGReader implements ImageReader { private static final int PNG_SIG_LENGTH = 24; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(bis); boolean supported = ((header[0] == (byte) 0x89) @@ -40,6 +40,7 @@ public class PNGReader implements ImageReader { if (supported) { FopImage.ImageInfo info = getDimension(header); info.mimeType = getMimeType(); + info.inputStream = bis; return info; } else { return null; @@ -78,7 +79,7 @@ public class PNGReader implements ImageReader { return info; } - private byte[] getDefaultHeader(BufferedInputStream imageStream) + private byte[] getDefaultHeader(InputStream imageStream) throws IOException { byte[] header = new byte[PNG_SIG_LENGTH]; try { @@ -96,4 +97,4 @@ public class PNGReader implements ImageReader { return header; } -} \ No newline at end of file +} diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index 8f4c7d979..13fab2b85 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; import java.io.InputStream; import java.awt.geom.AffineTransform; @@ -37,10 +37,17 @@ public class SVGReader implements ImageReader { private boolean batik = true; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, InputStream fis, FOUserAgent ua) throws IOException { - return loadImage(uri, fis, ua); + FopImage.ImageInfo info = loadImage(uri, fis, ua); + if (info != null) { + try { + fis.close(); + } catch (Exception e) { + } + } + return info; } /** @@ -61,7 +68,7 @@ public class SVGReader implements ImageReader { * @param ua @todo Description of the Parameter * @return @todo Description of the Return Value */ - private FopImage.ImageInfo loadImage(String uri, BufferedInputStream bis, + private FopImage.ImageInfo loadImage(String uri, InputStream bis, FOUserAgent ua) { if (batik) { try { diff --git a/src/org/apache/fop/image/analyser/TIFFReader.java b/src/org/apache/fop/image/analyser/TIFFReader.java index 0be2c33c4..c2e0f9877 100644 --- a/src/org/apache/fop/image/analyser/TIFFReader.java +++ b/src/org/apache/fop/image/analyser/TIFFReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; // FOP @@ -25,7 +25,7 @@ public class TIFFReader implements ImageReader { private static final int TIFF_SIG_LENGTH = 8; /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, InputStream bis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(bis); boolean supported = false; @@ -51,6 +51,7 @@ public class TIFFReader implements ImageReader { if (supported) { FopImage.ImageInfo info = getDimension(header); info.mimeType = getMimeType(); + info.inputStream = bis; return info; } else { return null; @@ -95,7 +96,7 @@ public class TIFFReader implements ImageReader { return info; } - private byte[] getDefaultHeader(BufferedInputStream imageStream) + private byte[] getDefaultHeader(InputStream imageStream) throws IOException { byte[] header = new byte[TIFF_SIG_LENGTH]; try { diff --git a/src/org/apache/fop/image/analyser/XMLReader.java b/src/org/apache/fop/image/analyser/XMLReader.java index 7399075e3..6e2365e3c 100644 --- a/src/org/apache/fop/image/analyser/XMLReader.java +++ b/src/org/apache/fop/image/analyser/XMLReader.java @@ -7,7 +7,7 @@ package org.apache.fop.image.analyser; // Java -import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; import java.util.Map; @@ -36,10 +36,17 @@ public class XMLReader implements ImageReader { } /** @see org.apache.fop.image.analyser.ImageReader */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, InputStream fis, FOUserAgent ua) throws IOException { - return loadImage(uri, fis, ua); + FopImage.ImageInfo info = loadImage(uri, fis, ua); + if (info != null) { + try { + fis.close(); + } catch (Exception e) { + } + } + return info; } /** @@ -62,7 +69,7 @@ public class XMLReader implements ImageReader { * @param ua The user agent * @return An ImageInfo object describing the image */ - protected FopImage.ImageInfo loadImage(String uri, BufferedInputStream bis, + protected FopImage.ImageInfo loadImage(String uri, InputStream bis, FOUserAgent ua) { return createDocument(bis, ua); } @@ -74,7 +81,7 @@ public class XMLReader implements ImageReader { * @param ua The user agent * @return An ImageInfo object describing the image */ - public FopImage.ImageInfo createDocument(BufferedInputStream is, FOUserAgent ua) { + public FopImage.ImageInfo createDocument(InputStream is, FOUserAgent ua) { Document doc = null; FopImage.ImageInfo info = new FopImage.ImageInfo(); info.mimeType = getMimeType(); diff --git a/src/org/apache/fop/svg/PDFGraphics2D.java b/src/org/apache/fop/svg/PDFGraphics2D.java index 194868f5a..d4b51156f 100644 --- a/src/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/org/apache/fop/svg/PDFGraphics2D.java @@ -294,7 +294,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { * @param height the height to draw the image */ public void addJpegImage(JpegImage jpeg, float x, float y, float width, float height) { - FopPDFImage fopimage = new FopPDFImage(jpeg, jpeg.getURL()); + FopPDFImage fopimage = new FopPDFImage(jpeg, ""); int xObjectNum = this.pdfDoc.addImage(resourceContext, fopimage).getXNumber(); AffineTransform at = getTransform();