Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B838F90A4 for ; Tue, 28 Feb 2012 20:07:00 +0000 (UTC) Received: (qmail 52215 invoked by uid 500); 28 Feb 2012 20:07:00 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 52152 invoked by uid 500); 28 Feb 2012 20:07:00 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 52145 invoked by uid 99); 28 Feb 2012 20:07:00 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Feb 2012 20:07:00 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Feb 2012 20:06:56 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id CC9C5238890D for ; Tue, 28 Feb 2012 20:06:34 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1294807 - in /commons/proper/sanselan/trunk/src: main/java/org/apache/commons/sanselan/common/bytesource/ main/java/org/apache/commons/sanselan/formats/bmp/ main/java/org/apache/commons/sanselan/formats/pcx/ main/java/org/apache/commons/sa... Date: Tue, 28 Feb 2012 20:06:34 -0000 To: commits@commons.apache.org From: damjan@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120228200634.CC9C5238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: damjan Date: Tue Feb 28 20:06:34 2012 New Revision: 1294807 URL: http://svn.apache.org/viewvc?rev=1294807&view=rev Log: Fix many ByteSource.getInputStream() leaks, where the stream returned wasn't closed in a finally block. Jira issue key: SANSELAN-63 Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/common/bytesource/ByteSource.java commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/bmp/BmpImageParser.java commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/pcx/PcxImageParser.java commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/psd/PsdImageParser.java commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/icc/IccProfileParser.java commons/proper/sanselan/trunk/src/test/java/org/apache/commons/sanselan/common/bytesource/ByteSourceDataTest.java Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/common/bytesource/ByteSource.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/common/bytesource/ByteSource.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/common/bytesource/ByteSource.java (original) +++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/common/bytesource/ByteSource.java Tue Feb 28 20:06:34 2012 @@ -32,10 +32,19 @@ public abstract class ByteSource extends public final InputStream getInputStream(int start) throws IOException { - InputStream is = getInputStream(); - - skipBytes(is, start); - + InputStream is = null; + boolean succeeded = false; + try { + is = getInputStream(); + skipBytes(is, start); + succeeded = true; + } finally { + if (!succeeded) { + if (is != null) { + is.close(); + } + } + } return is; } Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/bmp/BmpImageParser.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/bmp/BmpImageParser.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/bmp/BmpImageParser.java (original) +++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/bmp/BmpImageParser.java Tue Feb 28 20:06:34 2012 @@ -593,8 +593,19 @@ public class BmpImageParser extends Imag throw new ImageReadException("Unknown parameter: " + firstKey); } - ImageContents ic = readImageContents(byteSource.getInputStream(), - FormatCompliance.getDefault(), verbose); + InputStream is = null; + ImageContents ic = null; + try { + is = byteSource.getInputStream(); + ic = readImageContents(is, FormatCompliance.getDefault(), verbose); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } if (ic == null) throw new ImageReadException("Couldn't read BMP Data"); @@ -671,7 +682,18 @@ public class BmpImageParser extends Imag FormatCompliance result = new FormatCompliance(byteSource .getDescription()); - readImageContents(byteSource.getInputStream(), result, verbose); + InputStream is = null; + try { + is = byteSource.getInputStream(); + readImageContents(is, result, verbose); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } return result; } @@ -679,7 +701,18 @@ public class BmpImageParser extends Imag public BufferedImage getBufferedImage(ByteSource byteSource, Map params) throws ImageReadException, IOException { - return getBufferedImage(byteSource.getInputStream(), params); + InputStream is = null; + try { + is = byteSource.getInputStream(); + return getBufferedImage(is, params); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } } public BufferedImage getBufferedImage(InputStream inputStream, Map params) Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/pcx/PcxImageParser.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/pcx/PcxImageParser.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/pcx/PcxImageParser.java (original) +++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/pcx/PcxImageParser.java Tue Feb 28 20:06:34 2012 @@ -372,8 +372,7 @@ public class PcxImageParser extends Imag { stream = byteSource.getInputStream(); long toSkip = byteSource.getLength() - 769; - while (toSkip > 0) - toSkip -= stream.skip(toSkip); + skipBytes(stream, (int)toSkip); return read256ColorPalette(stream); } finally Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/psd/PsdImageParser.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/psd/PsdImageParser.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/psd/PsdImageParser.java (original) +++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/formats/psd/PsdImageParser.java Tue Feb 28 20:06:34 2012 @@ -300,52 +300,63 @@ public class PsdImageParser extends Imag private InputStream getInputStream(ByteSource byteSource, int section) throws ImageReadException, IOException { - InputStream is = byteSource.getInputStream(); - - if (section == PSD_SECTION_HEADER) - return is; - - skipBytes(is, PSD_HEADER_LENGTH); - // is.skip(kHeaderLength); - - int ColorModeDataLength = read4Bytes("ColorModeDataLength", is, - "Not a Valid PSD File"); - - if (section == PSD_SECTION_COLOR_MODE) - return is; - - skipBytes(is, ColorModeDataLength); - // byte ColorModeData[] = readByteArray("ColorModeData", - // ColorModeDataLength, is, "Not a Valid PSD File"); - - int ImageResourcesLength = read4Bytes("ImageResourcesLength", is, - "Not a Valid PSD File"); - - if (section == PSD_SECTION_IMAGE_RESOURCES) - return is; - - skipBytes(is, ImageResourcesLength); - // byte ImageResources[] = readByteArray("ImageResources", - // ImageResourcesLength, is, "Not a Valid PSD File"); - - int LayerAndMaskDataLength = read4Bytes("LayerAndMaskDataLength", is, - "Not a Valid PSD File"); - - if (section == PSD_SECTION_LAYER_AND_MASK_DATA) - return is; - - skipBytes(is, LayerAndMaskDataLength); - // byte LayerAndMaskData[] = readByteArray("LayerAndMaskData", - // LayerAndMaskDataLength, is, "Not a Valid PSD File"); - - int Compression = read2Bytes("Compression", is, "Not a Valid PSD File"); - - // byte ImageData[] = readByteArray("ImageData", - // LayerAndMaskDataLength, is, "Not a Valid PSD File"); - - if (section == PSD_SECTION_IMAGE_DATA) - return is; + InputStream is = null; + boolean notFound = false; + try { + is = byteSource.getInputStream(); + if (section == PSD_SECTION_HEADER) + return is; + + skipBytes(is, PSD_HEADER_LENGTH); + // is.skip(kHeaderLength); + + int ColorModeDataLength = read4Bytes("ColorModeDataLength", is, + "Not a Valid PSD File"); + + if (section == PSD_SECTION_COLOR_MODE) + return is; + + skipBytes(is, ColorModeDataLength); + // byte ColorModeData[] = readByteArray("ColorModeData", + // ColorModeDataLength, is, "Not a Valid PSD File"); + + int ImageResourcesLength = read4Bytes("ImageResourcesLength", is, + "Not a Valid PSD File"); + + if (section == PSD_SECTION_IMAGE_RESOURCES) + return is; + + skipBytes(is, ImageResourcesLength); + // byte ImageResources[] = readByteArray("ImageResources", + // ImageResourcesLength, is, "Not a Valid PSD File"); + + int LayerAndMaskDataLength = read4Bytes("LayerAndMaskDataLength", is, + "Not a Valid PSD File"); + + if (section == PSD_SECTION_LAYER_AND_MASK_DATA) + return is; + + skipBytes(is, LayerAndMaskDataLength); + // byte LayerAndMaskData[] = readByteArray("LayerAndMaskData", + // LayerAndMaskDataLength, is, "Not a Valid PSD File"); + + int Compression = read2Bytes("Compression", is, "Not a Valid PSD File"); + + // byte ImageData[] = readByteArray("ImageData", + // LayerAndMaskDataLength, is, "Not a Valid PSD File"); + + if (section == PSD_SECTION_IMAGE_DATA) + return is; + notFound = true; + } finally { + if (notFound && is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } throw new ImageReadException("getInputStream: Unknown Section: " + section); } Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/icc/IccProfileParser.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/icc/IccProfileParser.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/icc/IccProfileParser.java (original) +++ commons/proper/sanselan/trunk/src/main/java/org/apache/commons/sanselan/icc/IccProfileParser.java Tue Feb 28 20:06:34 2012 @@ -344,33 +344,43 @@ public class IccProfileParser extends Bi // if (debug) // Debug.debug("length: " + length); - InputStream is = byteSource.getInputStream(); - - int ProfileSize = read4Bytes("ProfileSize", is, - "Not a Valid ICC Profile"); - - // if (length != ProfileSize) - // return null; - - this.skipBytes(is, 4 * 5); - - skipBytes(is, 12, "Not a Valid ICC Profile"); - - this.skipBytes(is, 4 * 3); - - int DeviceManufacturer = read4Bytes("ProfileFileSignature", is, - "Not a Valid ICC Profile"); - if (debug) - printCharQuad("DeviceManufacturer", DeviceManufacturer); - - int DeviceModel = read4Bytes("DeviceModel", is, - "Not a Valid ICC Profile"); - if (debug) - printCharQuad("DeviceModel", DeviceModel); - - boolean result = ((DeviceManufacturer == IEC) && (DeviceModel == sRGB)); - - return result; + InputStream is = null; + try { + is = byteSource.getInputStream(); + + int ProfileSize = read4Bytes("ProfileSize", is, + "Not a Valid ICC Profile"); + + // if (length != ProfileSize) + // return null; + + this.skipBytes(is, 4 * 5); + + skipBytes(is, 12, "Not a Valid ICC Profile"); + + this.skipBytes(is, 4 * 3); + + int DeviceManufacturer = read4Bytes("ProfileFileSignature", is, + "Not a Valid ICC Profile"); + if (debug) + printCharQuad("DeviceManufacturer", DeviceManufacturer); + + int DeviceModel = read4Bytes("DeviceModel", is, + "Not a Valid ICC Profile"); + if (debug) + printCharQuad("DeviceModel", DeviceModel); + + boolean result = ((DeviceManufacturer == IEC) && (DeviceModel == sRGB)); + + return result; + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } } catch (Exception e) { Modified: commons/proper/sanselan/trunk/src/test/java/org/apache/commons/sanselan/common/bytesource/ByteSourceDataTest.java URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/test/java/org/apache/commons/sanselan/common/bytesource/ByteSourceDataTest.java?rev=1294807&r1=1294806&r2=1294807&view=diff ============================================================================== --- commons/proper/sanselan/trunk/src/test/java/org/apache/commons/sanselan/common/bytesource/ByteSourceDataTest.java (original) +++ commons/proper/sanselan/trunk/src/test/java/org/apache/commons/sanselan/common/bytesource/ByteSourceDataTest.java Tue Feb 28 20:06:34 2012 @@ -82,13 +82,23 @@ public class ByteSourceDataTest extends // test cache during interrupted read cache by reading only first N bytes. { - InputStream is = byteSource.getInputStream(); - byte prefix[] = new byte[256]; - int read = is.read(prefix); - - assertTrue(read <= src.length); - for (int i = 0; i < read; i++) - assertTrue(src[i] == prefix[i]); + InputStream is = null; + try { + is = byteSource.getInputStream(); + byte prefix[] = new byte[256]; + int read = is.read(prefix); + + assertTrue(read <= src.length); + for (int i = 0; i < read; i++) + assertTrue(src[i] == prefix[i]); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignore) { + } + } + } } // test cache by completely reading InputStream N times. @@ -112,12 +122,22 @@ public class ByteSourceDataTest extends int start = src.length / 2; - InputStream is = byteSource.getInputStream(start); - byte dst[] = IoUtils.getInputStreamBytes(is); - - assertTrue(src.length == dst.length + start); - for (int i = 0; i < dst.length; i++) - assertTrue(dst[i] == src[i + start]); + InputStream is = null; + try { + is = byteSource.getInputStream(start); + byte dst[] = IoUtils.getInputStreamBytes(is); + + assertTrue(src.length == dst.length + start); + for (int i = 0; i < dst.length; i++) + assertTrue(dst[i] == src[i + start]); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ignored) { + } + } + } } }