From e7015eb126ea5a302045a207e565edde4f239898 Mon Sep 17 00:00:00 2001 From: Glenn Adams Date: Sun, 6 Jul 2014 05:34:16 +0000 Subject: [PATCH] Fix leaked resource warnings from Eclipse (Kepler). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1608162 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/afp/apps/FontPatternExtractor.java | 1 + .../apache/fop/afp/goca/GraphicsSetProcessColor.java | 10 ++++++++-- src/java/org/apache/fop/fonts/type1/PFMFile.java | 2 ++ .../org/apache/fop/hyphenation/HyphenationTree.java | 8 ++++++-- .../apache/fop/image/loader/batik/PreloaderWMF.java | 1 + src/java/org/apache/fop/pdf/PDFFactory.java | 2 ++ src/java/org/apache/fop/pdf/PDFOutputIntent.java | 5 ++++- src/java/org/apache/fop/render/pcl/PCLGenerator.java | 3 +++ .../apache/fop/render/pdf/AbstractImageAdapter.java | 4 +++- .../org/apache/fop/tools/anttasks/FileCompare.java | 10 ++++++++-- 10 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/java/org/apache/fop/afp/apps/FontPatternExtractor.java b/src/java/org/apache/fop/afp/apps/FontPatternExtractor.java index 7a2b8ed36..66150dda3 100644 --- a/src/java/org/apache/fop/afp/apps/FontPatternExtractor.java +++ b/src/java/org/apache/fop/afp/apps/FontPatternExtractor.java @@ -63,6 +63,7 @@ public class FontPatternExtractor { } ByteArrayInputStream bin = new ByteArrayInputStream(baout.toByteArray()); + IOUtils.closeQuietly(baout); DataInputStream din = new DataInputStream(bin); long len = din.readInt() & 0xFFFFFFFFL; println("Length: " + len); diff --git a/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java b/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java index 7a4575cd3..54826a2ec 100644 --- a/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java +++ b/src/java/org/apache/fop/afp/goca/GraphicsSetProcessColor.java @@ -21,10 +21,11 @@ package org.apache.fop.afp.goca; import java.awt.Color; import java.awt.color.ColorSpace; -import java.io.DataOutput; +import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.xmlgraphics.java2d.color.CIELabColorSpace; @@ -105,6 +106,7 @@ public class GraphicsSetProcessColor extends AbstractGraphicsDrawingOrder { ColorSpace cs = color.getColorSpace(); int colSpaceType = cs.getType(); ByteArrayOutputStream baout = new ByteArrayOutputStream(); + DataOutputStream dout = null; byte[] colsizes; if (colSpaceType == ColorSpace.TYPE_CMYK) { colspace = CMYK; @@ -121,7 +123,7 @@ public class GraphicsSetProcessColor extends AbstractGraphicsDrawingOrder { } else if (cs instanceof CIELabColorSpace) { colspace = CIELAB; colsizes = new byte[] {0x08, 0x08, 0x08, 0x00}; - DataOutput dout = new java.io.DataOutputStream(baout); + dout = new DataOutputStream(baout); //According to GOCA, I'd expect the multiplicator below to be 255f, not 100f //But only IBM AFP Workbench seems to support Lab colors and it requires "c * 100f" int l = Math.round(colorComponents[0] * 100f); @@ -131,6 +133,8 @@ public class GraphicsSetProcessColor extends AbstractGraphicsDrawingOrder { dout.writeByte(a); dout.writeByte(b); } else { + IOUtils.closeQuietly(dout); + IOUtils.closeQuietly(baout); throw new IllegalStateException(); } @@ -151,6 +155,8 @@ public class GraphicsSetProcessColor extends AbstractGraphicsDrawingOrder { os.write(data); baout.writeTo(os); + IOUtils.closeQuietly(dout); + IOUtils.closeQuietly(baout); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/fonts/type1/PFMFile.java b/src/java/org/apache/fop/fonts/type1/PFMFile.java index 3b171e85a..d19c8d478 100644 --- a/src/java/org/apache/fop/fonts/type1/PFMFile.java +++ b/src/java/org/apache/fop/fonts/type1/PFMFile.java @@ -85,6 +85,7 @@ public class PFMFile { short sh2 = in.readByte(); if (sh1 == 128 && sh2 == 1) { //Found the first section header of a PFB file! + IOUtils.closeQuietly(in); throw new IOException("Cannot parse PFM file. You probably specified the PFB file" + " of a Type 1 font as parameter instead of the PFM."); } @@ -93,6 +94,7 @@ public class PFMFile { bufin.read(b); if (new String(b, "US-ASCII").equalsIgnoreCase("StartFontMetrics")) { //Found the header of a AFM file! + IOUtils.closeQuietly(in); throw new IOException("Cannot parse PFM file. You probably specified the AFM file" + " of a Type 1 font as parameter instead of the PFM."); } diff --git a/src/java/org/apache/fop/hyphenation/HyphenationTree.java b/src/java/org/apache/fop/hyphenation/HyphenationTree.java index 70fc3f05a..60d280f7c 100644 --- a/src/java/org/apache/fop/hyphenation/HyphenationTree.java +++ b/src/java/org/apache/fop/hyphenation/HyphenationTree.java @@ -36,6 +36,8 @@ import java.util.List; import org.xml.sax.InputSource; +import org.apache.commons.io.IOUtils; + /** *

This tree structure stores the hyphenation patterns in an efficient * way for fast lookup. It provides the provides the method to @@ -709,9 +711,9 @@ public class HyphenationTree extends TernaryTree token = in.readLine().trim(); long starttime = 0; int counter = 0; + BufferedReader reader = null; try { - BufferedReader reader - = new BufferedReader(new FileReader(token)); + reader = new BufferedReader(new FileReader(token)); String line; starttime = System.currentTimeMillis(); @@ -731,6 +733,8 @@ public class HyphenationTree extends TernaryTree } catch (Exception ioe) { System.out.println("Exception " + ioe); ioe.printStackTrace(); + } finally { + IOUtils.closeQuietly(reader); } long endtime = System.currentTimeMillis(); long result = endtime - starttime; diff --git a/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java b/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java index 43341cbe6..582c35a82 100644 --- a/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java +++ b/src/java/org/apache/fop/image/loader/batik/PreloaderWMF.java @@ -97,6 +97,7 @@ public class PreloaderWMF extends AbstractImagePreloader { int magic = EndianUtils.swapInteger(din.readInt()); din.reset(); if (magic != WMFConstants.META_ALDUS_APM) { + IOUtils.closeQuietly(din); return null; //Not a WMF file } diff --git a/src/java/org/apache/fop/pdf/PDFFactory.java b/src/java/org/apache/fop/pdf/PDFFactory.java index eb4f61077..e4a9b1b55 100644 --- a/src/java/org/apache/fop/pdf/PDFFactory.java +++ b/src/java/org/apache/fop/pdf/PDFFactory.java @@ -1587,6 +1587,8 @@ public class PDFFactory { log.error( "Failed to write CIDSet [" + cidFont + "] " + cidFont.getEmbedFontName(), ioe); + } finally { + IOUtils.closeQuietly(baout); } } diff --git a/src/java/org/apache/fop/pdf/PDFOutputIntent.java b/src/java/org/apache/fop/pdf/PDFOutputIntent.java index 1c0373944..32a9f01ec 100644 --- a/src/java/org/apache/fop/pdf/PDFOutputIntent.java +++ b/src/java/org/apache/fop/pdf/PDFOutputIntent.java @@ -21,6 +21,7 @@ package org.apache.fop.pdf; import java.io.IOException; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; /** @@ -167,7 +168,9 @@ public class PDFOutputIntent extends PDFObject { } catch (IOException ioe) { log.error("Ignored I/O exception", ioe); } - return bout.toByteArray(); + byte[] bytes = bout.toByteArray(); + IOUtils.closeQuietly(bout); + return bytes; } diff --git a/src/java/org/apache/fop/render/pcl/PCLGenerator.java b/src/java/org/apache/fop/render/pcl/PCLGenerator.java index 0db295a5a..99aa40f22 100644 --- a/src/java/org/apache/fop/render/pcl/PCLGenerator.java +++ b/src/java/org/apache/fop/render/pcl/PCLGenerator.java @@ -42,6 +42,7 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.xmlgraphics.image.GraphicsUtil; @@ -447,6 +448,8 @@ public class PCLGenerator { writeCommand("*c" + patternID + "G"); writeCommand("*c" + baout.size() + "W"); baout.writeTo(this.out); + IOUtils.closeQuietly(data); + IOUtils.closeQuietly(baout); writeCommand("*c4Q"); //temporary pattern } diff --git a/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java b/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java index 471efc567..c61ca8cda 100644 --- a/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java +++ b/src/java/org/apache/fop/render/pdf/AbstractImageAdapter.java @@ -26,6 +26,7 @@ import java.awt.image.Raster; import java.io.IOException; import java.util.Arrays; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -241,12 +242,12 @@ public abstract class AbstractImageAdapter implements PDFImage { + " The image may not be handled correctly." + " Base color space: " + icm.getColorSpace() + " Image: " + image.getInfo()); } - ByteArrayOutputStream baout = new ByteArrayOutputStream(); int c = icm.getMapSize(); int hival = c - 1; if (hival > MAX_HIVAL) { throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL); } + ByteArrayOutputStream baout = new ByteArrayOutputStream(); boolean isDeviceGray = false; int[] palette = new int[c]; icm.getRGBs(palette); @@ -276,6 +277,7 @@ public abstract class AbstractImageAdapter implements PDFImage { indexed.add(hival); indexed.add(baout.toByteArray()); + IOUtils.closeQuietly(baout); dict.put("ColorSpace", indexed); diff --git a/src/java/org/apache/fop/tools/anttasks/FileCompare.java b/src/java/org/apache/fop/tools/anttasks/FileCompare.java index 4906bdc6c..8591babe2 100644 --- a/src/java/org/apache/fop/tools/anttasks/FileCompare.java +++ b/src/java/org/apache/fop/tools/anttasks/FileCompare.java @@ -28,6 +28,7 @@ import java.util.Date; import java.util.List; import java.util.StringTokenizer; +import org.apache.commons.io.IOUtils; import org.apache.tools.ant.BuildException; /** @@ -98,6 +99,7 @@ public class FileCompare { * @return true if files are same byte-by-byte, false otherwise */ private static boolean compareBytes(File file1, File file2) throws IOException { + boolean same = true; BufferedInputStream file1Input = new BufferedInputStream(new java.io.FileInputStream(file1)); BufferedInputStream file2Input @@ -111,11 +113,15 @@ public class FileCompare { charact1 = file1Input.read(); charact2 = file2Input.read(); } else { - return false; + same = false; + break; } } - return true; + IOUtils.closeQuietly(file1Input); + IOUtils.closeQuietly(file2Input); + + return same; } /** -- 2.39.5