harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r432228 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/RandomAccessFile.java test/java/tests/api/java/io/RandomAccessFileTest.java
Date Thu, 17 Aug 2006 13:18:46 GMT
Author: hindessm
Date: Thu Aug 17 06:18:45 2006
New Revision: 432228

URL: http://svn.apache.org/viewvc?rev=432228&view=rev
Log:
Applied patch from "[#HARMONY-1214] [classlib][luni] java.io.RandomAccessFile#write(byte[],
int , int) method's exception order is different from RI's".

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java?rev=432228&r1=432227&r2=432228&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/RandomAccessFile.java
Thu Aug 17 06:18:45 2006
@@ -34,7 +34,7 @@
     /**
      * The FileDescriptor representing this RandomAccessFile.
      */
-    FileDescriptor fd;
+    private FileDescriptor fd;
 
     private boolean syncMetadata = false;
 
@@ -733,12 +733,15 @@
      * @see #read(byte[], int, int)
      */
     public void write(byte[] buffer, int offset, int count) throws IOException {
-        // have to have four comparisions to not miss integer overflow cases
-        if (count < 0 || offset < 0 || offset > buffer.length
-                || count > buffer.length - offset) {
+    	if (null == buffer) {
+    		throw new NullPointerException();
+    	}
+        if (count < 0 || offset < 0 || count > buffer.length - offset) {
             throw new IndexOutOfBoundsException();
         }
-
+        if (0 == count){
+        	return;
+        }
         openCheck();
         synchronized (repositionLock) {
             fileSystem.write(fd.descriptor, buffer, offset, count);

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java?rev=432228&r1=432227&r2=432228&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/RandomAccessFileTest.java
Thu Aug 17 06:18:45 2006
@@ -384,13 +384,40 @@
     public void test_write$B() throws IOException {
         // Test for method void java.io.RandomAccessFile.write(byte [])
         RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
+        
+        byte[] nullByteArray = null;
+        try {
+        	raf.write(nullByteArray);
+        	fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+        	//expected
+        }   
+        
         byte[] rbuf = new byte[4000];
         raf.write(fileString.getBytes());
         raf.close();
+        
+        try {
+        	raf.write(nullByteArray);
+        	fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+        	//expected
+        }  
+        
+        //will not throw IOException if array's length is 0
+        raf.write(new byte[0]);
+        
+        try {
+        	raf.write(fileString.getBytes());
+        	fail("should throw IOException");
+        } catch (IOException e) {
+        	//expected
+        }  
+        
         FileInputStream fis = new java.io.FileInputStream(fileName);
         fis.read(rbuf, 0, fileString.length());
         assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
-                fileString.length()));
+                fileString.length()));    
     }
 
     /**
@@ -408,6 +435,146 @@
         assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
                 fileString.length()));
     }
+    
+    /**
+     * @tests java.io.RandomAccessFile#write(byte[], int, int)
+     */
+    public void test_write_$BII_Exception() throws IOException {
+    	raf = new java.io.RandomAccessFile(f, "rw");
+		byte[] nullByteArray = null;
+		byte[] byteArray = new byte[10];
+		
+		try {
+			raf.write(nullByteArray, -1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			raf.write(nullByteArray, 0, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			raf.write(nullByteArray, 1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+
+		try {
+			raf.write(nullByteArray, 1, 0);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			raf.write(nullByteArray, 1, 1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			raf.write(byteArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			raf.write(byteArray, -1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+		
+		try {
+			raf.write(byteArray, -1, 1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		try {
+			raf.write(byteArray, 0, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+
+		raf.write(byteArray, 0, 0);
+        raf.write(byteArray, 0, byteArray.length);
+		raf.write(byteArray, 1, 0);
+        raf.write(byteArray, byteArray.length, 0);
+		
+		try {
+			raf.write(byteArray, byteArray.length + 1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		try {
+			raf.write(byteArray, byteArray.length + 1, 1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+
+		raf.close();
+
+		try {
+			raf.write(nullByteArray, -1, -1);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		
+		try {
+			raf.write(byteArray, -1, -1);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			// expected
+		}
+		
+		try {
+	        raf.write(byteArray, 0, 1);
+	        fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
+		
+		try {
+	        raf.write(byteArray, 0, byteArray.length);
+	        fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
+		
+		try {
+			raf.write(byteArray, 1, 1);
+	        fail("should throw IOException");
+		} catch (IOException e) {
+			//expected
+		}
+		
+		try {
+			raf.write(byteArray, byteArray.length + 1, 0);
+			fail("should throw IndexOutOfBoundsException");
+		} catch (IndexOutOfBoundsException e) {
+			//expected
+		}
+		
+		// will not throw IOException if count = 0
+		raf.write(byteArray, 0, 0);
+		raf.write(byteArray, byteArray.length, 0);
+    }
+    
 
     /**
      * @tests java.io.RandomAccessFile#write(int)



Mime
View raw message