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/10 11:08:48 UTC

svn commit: r430327 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/RandomAccessFile.java test/java/tests/api/java/io/RandomAccessFileTest.java

Author: pyang
Date: Thu Aug 10 02:08:46 2006
New Revision: 430327

URL: http://svn.apache.org/viewvc?rev=430327&view=rev
Log:
Patch applied for HARMONY-1122 ([classlib][luni] RandomAccessFile.read(byte[],int,int) method should not throw IOExcepiton when the RandomAccessFile is closed)

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=430327&r1=430326&r2=430327&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 10 02:08:46 2006
@@ -296,11 +296,12 @@
      */
     public int read(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 (count > buffer.length - offset || count < 0 || offset < 0) {
             throw new IndexOutOfBoundsException();
         }
-
+        if (0 == count) {
+            return 0;
+        }
         openCheck();
         synchronized (repositionLock) {
             return (int) fileSystem.read(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=430327&r1=430326&r2=430327&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 10 02:08:46 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.
@@ -622,6 +622,70 @@
         }
 
         raf.close();
+    }
+    
+    /**
+     * @tests java.io.RandomAccessFile#read(byte[],int,int) 
+     */
+    public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
+        FileOutputStream fos = new java.io.FileOutputStream(fileName);
+        fos.write(fileString.getBytes(), 0, fileString.length());
+        fos.close();
+
+        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
+        byte[] rbuf = new byte[100];
+        raf.close();
+        try {
+            raf.read(rbuf,-1,0);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+          //expected
+        }
+    }
+    
+    /**
+     * @tests java.io.RandomAccessFile#read(byte[],int,int) 
+     */
+    public void test_read_$BII_IOException() throws IOException {
+        FileOutputStream fos = new java.io.FileOutputStream(fileName);
+        fos.write(fileString.getBytes(), 0, fileString.length());
+        fos.close();
+
+        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
+        byte[] rbuf = new byte[100];
+        raf.close();
+        int read = raf.read(rbuf,0,0);
+        assertEquals(0,read);
+    }
+    
+    /**
+     * @tests java.io.RandomAccessFile#read(byte[])
+     */
+    public void test_read_$B_IOException() throws IOException {
+        FileOutputStream fos = new java.io.FileOutputStream(fileName);
+        fos.write(fileString.getBytes(), 0, fileString.length());
+        fos.close();
+
+        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
+        byte[] rbuf = new byte[0];
+        raf.close();
+        int read = raf.read(rbuf);
+        assertEquals(0,read);
+    }
+    
+    /**
+     * @tests java.io.RandomAccessFile#read(byte[],int,int) 
+     */
+    public void test_read_$BII_NullPointerException() throws IOException {
+        RandomAccessFile raf = new RandomAccessFile(File.createTempFile("tmp",
+                "tmp"), "r");
+        byte[] rbuf = null;
+        try {
+            raf.read(rbuf, 0, -1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
     }
 
     /**