harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r447286 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/DataInputStream.java test/java/tests/api/java/io/DataInputStreamTest.java
Date Mon, 18 Sep 2006 06:46:46 GMT
Author: pyang
Date: Sun Sep 17 23:46:45 2006
New Revision: 447286

URL: http://svn.apache.org/viewvc?view=rev&rev=447286
Log:
Patch applied for HARMONY-1103 ([classlib][io] compatibility: different checks order for DataInputStream(null).readFully(byte[],
int, int))

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java?view=diff&rev=447286&r1=447285&r2=447286
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java
Sun Sep 17 23:46:45 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -189,33 +189,52 @@
 		readFully(buffer, 0, buffer.length);
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * 
-	 * @see java.io.DataInput#readFully(byte[], int, int)
-	 */
-	public final void readFully(byte[] buffer, int offset, int length)
-			throws IOException {
-		if (buffer != null) {
-			// avoid int overflow
-			if (0 <= offset && offset <= buffer.length && 0 <= length
-					&& length <= buffer.length - offset) {
-				while (length > 0) {
-					int result = in.read(buffer, offset, length);
-					if (result >= 0) {
-						offset += result;
-						length -= result;
-					} else {
-                        throw new EOFException();
-                    }
-				}
-			} else {
-                throw new IndexOutOfBoundsException();
-            }
-		} else {
+    /**
+     * Reads bytes from this stream and stores them in the byte array
+     * <code>buffer</code> starting at the position <code>offset</code>.
+     * This method blocks until <code>count</code> bytes have been read.
+     * 
+     * @param buffer
+     *            the byte array into which the data is read
+     * @param offset
+     *            the offset the operation start at
+     * @param count
+     *            the maximum number of bytes to read
+     * 
+     * @throws IOException
+     *             if a problem occurs while reading from this stream
+     * @throws EOFException
+     *             if reaches the end of the stream before enough bytes have
+     *             been read
+     * @see java.io.DataInput#readFully(byte[], int, int)
+     */
+    public final void readFully(byte[] buffer, int offset, int length)
+            throws IOException {
+        if (length < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (length == 0) {
+            return;
+        }
+        if (in == null) {
+            throw new NullPointerException(Msg.getString("KA00b")); //$NON-NLS-1$
+        }
+        if (buffer == null) {
             throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
         }
-	}
+        if (offset < 0 || offset > buffer.length - length) {
+            throw new IndexOutOfBoundsException();
+        }
+        while (length > 0) {
+            int result = in.read(buffer, offset, length);
+            if (result >= 0) {
+                offset += result;
+                length -= result;
+            } else {
+                throw new EOFException();
+            }
+        }
+    }
 
 	/**
 	 * Reads a 32-bit integer value from this stream.

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/DataInputStreamTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/DataInputStreamTest.java?view=diff&rev=447286&r1=447285&r2=447286
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/DataInputStreamTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/DataInputStreamTest.java
Sun Sep 17 23:46:45 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -201,6 +201,241 @@
 			fail("IOException during readFully test : " + e.getMessage());
 		}
 	}
+    
+    /**
+     * @tests java.io.DataInputStream#readFully(byte[], int, int)
+     */
+    public void test_readFully$BII_Exception() throws IOException {
+        DataInputStream is =  new DataInputStream(new ByteArrayInputStream(new byte[fileString.length()]));
+
+        byte[] byteArray = new byte[fileString.length()]; 
+        
+        try {
+            is.readFully(byteArray, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 0, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        is.readFully(byteArray, -1, 0);
+        is.readFully(byteArray, 0, 0);
+        is.readFully(byteArray, 1, 0);
+        
+        try {
+            is.readFully(byteArray, -1, 1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        is.readFully(byteArray, 0, 1);
+        is.readFully(byteArray, 1, 1);
+        try {
+            is.readFully(byteArray, 0, Integer.MAX_VALUE);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * @tests java.io.DataInputStream#readFully(byte[], int, int)
+     */
+    public void test_readFully$BII_NullArray() throws IOException {
+        DataInputStream is =  new DataInputStream(new ByteArrayInputStream(new byte[fileString.length()]));
+        
+        byte[] nullByteArray = null;
+       
+        try {
+            is.readFully(nullByteArray, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        is.readFully(nullByteArray, -1, 0);
+        is.readFully(nullByteArray, 0, 0);
+        is.readFully(nullByteArray, 1, 0);
+        
+        try {
+            is.readFully(nullByteArray, -1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            is.readFully(nullByteArray, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * @tests java.io.DataInputStream#readFully(byte[], int, int)
+     */
+    public void test_readFully$BII_NullStream() throws IOException {
+        DataInputStream is = new DataInputStream(null);
+        byte[] byteArray = new byte[fileString.length()]; 
+           
+        try {
+            is.readFully(byteArray, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 0, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        is.readFully(byteArray, -1, 0);
+        is.readFully(byteArray, 0, 0);
+        is.readFully(byteArray, 1, 0);
+        
+        try {
+            is.readFully(byteArray, -1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 0, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            is.readFully(byteArray, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(byteArray, 0, Integer.MAX_VALUE);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * @tests java.io.DataInputStream#readFully(byte[], int, int)
+     */
+    public void test_readFully$BII_NullStream_NullArray() throws IOException {
+        DataInputStream is = new DataInputStream(null);
+        byte[] nullByteArray = null;
+        
+        try {
+            is.readFully(nullByteArray, -1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 1, -1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        is.readFully(nullByteArray, -1, 0);
+        is.readFully(nullByteArray, 0, 0);
+        is.readFully(nullByteArray, 1, 0);
+        
+        try {
+            is.readFully(nullByteArray, -1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            is.readFully(nullByteArray, 1, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+    }
 
 	/**
 	 * @tests java.io.DataInputStream#readInt()
@@ -385,8 +620,11 @@
 	protected void tearDown() {
 		try {
 			os.close();
-			dis.close();
 		} catch (Exception e) {
 		}
+        try {
+            dis.close();
+        } catch (Exception e) {
+        }
 	}
 }



Mime
View raw message