classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Patch: throw IOExceptions on closed PushbackInputStream ope


From: Anthony Green
Subject: [cp-patches] Patch: throw IOExceptions on closed PushbackInputStream operations
Date: Wed, 14 Sep 2005 18:00:04 -0700

If you try an operation on a closed PushbackInputStream, you'll probably
get a NullPointerException.  This patch turns them into IOExceptions
with an appropriate message.  I think this is a better response based on
the spec (any error should throw an IOException).

OK?

AG



2005-09-14  Anthony Green  <address@hidden>

        * java/io/PushbackInputStream.java (available, read, skip): Handle
        closed stream operations gracefully.


Index: java/io/PushbackInputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/PushbackInputStream.java,v
retrieving revision 1.16
diff -u -r1.16 PushbackInputStream.java
--- java/io/PushbackInputStream.java    2 Jul 2005 20:32:38 -0000       1.16
+++ java/io/PushbackInputStream.java    15 Sep 2005 00:18:49 -0000
@@ -116,7 +116,11 @@
    */
   public int available() throws IOException
   {
-    return (buf.length - pos) + super.available();
+    try {
+      return (buf.length - pos) + super.available();
+    } catch (NullPointerException npe) {
+      throw new IOException ("Stream closed");
+    }
   }
 
   /**
@@ -168,8 +172,11 @@
    */
   public synchronized int read() throws IOException
   {
+    if (buf == null)
+      throw new IOException ("Stream closed");
+
     if (pos < buf.length)
-      return ((int) buf[pos++]) & 0xFF;
+       return ((int) buf[pos++]) & 0xFF;
 
     return super.read();
   }
@@ -200,6 +207,9 @@
    */
   public synchronized int read(byte[] b, int off, int len) throws IOException
   {
+    if (buf == null)
+      throw new IOException ("Stream closed");
+
     int numBytes = Math.min(buf.length - pos, len);
 
     if (numBytes > 0)
@@ -312,6 +322,9 @@
    */
   public synchronized long skip(long n) throws IOException
   {
+    if (buf == null)
+      throw new IOException ("Stream closed");
+
     final long origN = n;
 
     if (n > 0L)






reply via email to

[Prev in Thread] Current Thread [Next in Thread]