From f0f35a920fc508fb50bd34db271b7e7390193907 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 4 Aug 2010 18:35:16 -0600 Subject: [PATCH] return a useful value from InflaterInputStream.available Note the following excerpt from PNGFileFormat.java in SWT: /* * InflaterInputStream does not consume all bytes in the stream * when it is closed. This may leave unread IDAT chunks. The fix * is to read all available bytes before closing it. */ while (stream.available() > 0) stream.read(); stream.close(); This code relies on the documented behavior of InflaterInputStream.available, which must return "0 after EOF has been reached, otherwise always return 1". This is unlike InputStream.available, which is documented to return "the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream", and says nothing about how many bytes are left until the end of stream. This commit modifies InflaterInputStream.available to behave according to Sun's documentation. --- classpath/java/io/BufferedInputStream.java | 4 ++++ classpath/java/util/zip/InflaterInputStream.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/classpath/java/io/BufferedInputStream.java b/classpath/java/io/BufferedInputStream.java index b58cf8efd4..b1bdec24a0 100644 --- a/classpath/java/io/BufferedInputStream.java +++ b/classpath/java/io/BufferedInputStream.java @@ -72,6 +72,10 @@ public class BufferedInputStream extends InputStream { return count; } + public int available() throws IOException { + return in.available() + (limit - position); + } + public void close() throws IOException { in.close(); } diff --git a/classpath/java/util/zip/InflaterInputStream.java b/classpath/java/util/zip/InflaterInputStream.java index f453fbb9a8..1548c7eff4 100644 --- a/classpath/java/util/zip/InflaterInputStream.java +++ b/classpath/java/util/zip/InflaterInputStream.java @@ -61,6 +61,8 @@ public class InflaterInputStream extends InputStream { return count; } else if (inflater.needsDictionary()) { throw new IOException("missing dictionary"); + } else if (inflater.finished()) { + return -1; } } catch (DataFormatException e) { throw new IOException(e); @@ -68,6 +70,10 @@ public class InflaterInputStream extends InputStream { } } + public int available() throws IOException { + return inflater.finished() ? 0 : 1; + } + public void close() throws IOException { in.close(); inflater.dispose();