You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by da...@apache.org on 2012/02/28 21:06:34 UTC
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...
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) {
+ }
+ }
+ }
}
}