You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/11 13:38:54 UTC

svn commit: r430762 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/BufferedInputStream.java test/java/tests/api/java/io/BufferedInputStreamTest.java

Author: pyang
Date: Fri Aug 11 04:38:54 2006
New Revision: 430762

URL: http://svn.apache.org/viewvc?rev=430762&view=rev
Log:
Patch applied for HARMONY-1140 ([classlib][luni] java.io.BufferedInputStream methods close() and read(byte[],int,int) throw wrong exceptions)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedInputStreamTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java?rev=430762&r1=430761&r2=430762&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java Fri Aug 11 04:38:54 2006
@@ -52,6 +52,10 @@
      * The current position within the byte array <code>buf</code>.
      */
     protected int pos;
+    
+    private boolean closed = false;
+    
+    
 
     /**
      * Constructs a new <code>BufferedInputStream</code> on the InputStream
@@ -112,11 +116,12 @@
      */
     @Override
     public synchronized void close() throws IOException {
-        if (null == in) {
-            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
-        }
-        super.close();
+    	if(null != in ){
+            super.close();
+            in = null;
+    	}
         buf = null;
+        closed = true;
     }
 
     private int fillbuf() throws IOException {
@@ -233,68 +238,65 @@
      */
     @Override
     public synchronized int read(byte[] buffer, int offset, int length)
-            throws IOException {
-        if (buf != null && buffer != null) {
-            // avoid int overflow
-            if (0 <= offset && offset <= buffer.length && 0 <= length
-                    && length <= buffer.length - offset) {
-                if (length == 0) {
-                    return 0;
-                }
-
-                int required;
-                if (pos < count) {
-                    /* There are bytes available in the buffer. */
-                    int copylength = count - pos >= length ? length : count
-                            - pos;
-                    System.arraycopy(buf, pos, buffer, offset, copylength);
-                    pos += copylength;
-                    if (copylength == length || in.available() == 0) {
-                        return copylength;
-                    }
-                    offset += copylength;
-                    required = length - copylength;
-                } else {
-                    required = length;
-                }
-
-                while (true) {
-                    int read;
-                    /*
-                     * If we're not marked and the required size is greater than
-                     * the buffer, simply read the bytes directly bypassing the
-                     * buffer.
-                     */
-                    if (markpos == -1 && required >= buf.length) {
-                        read = in.read(buffer, offset, required);
-                        if (read == -1) {
-                            return required == length ? -1 : length - required;
-                        }
-                    } else {
-                        if (fillbuf() == -1) {
-                            return required == length ? -1 : length - required;
-                        }
-                        read = count - pos >= required ? required : count - pos;
-                        System.arraycopy(buf, pos, buffer, offset, read);
-                        pos += read;
-                    }
-                    required -= read;
-                    if (required == 0) {
-                        return length;
-                    }
-                    if (in.available() == 0) {
-                        return length - required;
-                    }
-                    offset += read;
-                }
-            }
-            throw new ArrayIndexOutOfBoundsException();
-        }
-        if (buf == null) {
-            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
-        }
-        throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
-    }
+			throws IOException {
+		if (closed) {
+			throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+		}
+		// avoid int overflow
+		if (offset > buffer.length - length || offset < 0 || length < 0) {
+			throw new IndexOutOfBoundsException();
+		}
+		if (length == 0) {
+			return 0;
+		}
+		if (null == buf) {
+			throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+		}
+
+		int required;
+		if (pos < count) {
+			/* There are bytes available in the buffer. */
+			int copylength = count - pos >= length ? length : count - pos;
+			System.arraycopy(buf, pos, buffer, offset, copylength);
+			pos += copylength;
+			if (copylength == length || in.available() == 0) {
+				return copylength;
+			}
+			offset += copylength;
+			required = length - copylength;
+		} else {
+			required = length;
+		}
+
+		while (true) {
+			int read;
+			/*
+			 * If we're not marked and the required size is greater than the
+			 * buffer, simply read the bytes directly bypassing the buffer.
+			 */
+			if (markpos == -1 && required >= buf.length) {
+				read = in.read(buffer, offset, required);
+				if (read == -1) {
+					return required == length ? -1 : length - required;
+				}
+			} else {
+				if (fillbuf() == -1) {
+					return required == length ? -1 : length - required;
+				}
+				read = count - pos >= required ? required : count - pos;
+				System.arraycopy(buf, pos, buffer, offset, read);
+				pos += read;
+			}
+			required -= read;
+			if (required == 0) {
+				return length;
+			}
+			if (in.available() == 0) {
+				return length - required;
+			}
+			offset += read;
+		}
+	}
 
     /**
      * Reset this BufferedInputStream to the last marked location. If the

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedInputStreamTest.java?rev=430762&r1=430761&r2=430762&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedInputStreamTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedInputStreamTest.java Fri Aug 11 04:38:54 2006
@@ -222,6 +222,50 @@
 	/**
 	 * @tests java.io.BufferedInputStream#read(byte[], int, int)
 	 */
+	public void test_read$BII_Exception() throws IOException {
+		BufferedInputStream bis = new BufferedInputStream(null);
+
+		try {
+			bis.read(null, -1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			//expected
+		}
+
+		try {
+			bis.read(new byte[0], -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+
+		try {
+			bis.read(new byte[0], 1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+
+		try {
+			bis.read(new byte[0], 1, 1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		bis.close();
+		
+		try {
+			bis.read(null, -1, -1);
+			fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
+	}
+	
+	/**
+	 * @tests java.io.BufferedInputStream#read(byte[], int, int)
+	 */
 	public void test_read$BII() {
 		// Test for method int java.io.BufferedInputStream.read(byte [], int,
 		// int)