commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r463154 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/IOUtils.java
Date Thu, 12 Oct 2006 08:24:24 GMT
Author: niallp
Date: Thu Oct 12 01:24:23 2006
New Revision: 463154

URL: http://svn.apache.org/viewvc?view=rev&rev=463154
Log:
IO-84 Modify copy() methods that return an int to throw an ArithmeticException if the returned
size is too large for an int to handle. Add two new equivalent copyLarge() methods that return
a long for large files.

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?view=diff&rev=463154&r1=463153&r2=463154
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Thu Oct 12 01:24:23 2006
@@ -60,8 +60,12 @@
   - This now handles the situation where an error occurs when deleting the file
 
 - IOUtils.copy [IO-84]
-  - Comment about result byte/char count being limited to an int, thus
-    being inacurate for large streams
+  - The copy(InputStream, OutputStream) method now throws an exception if
+    the count is greater than an int
+  - The copy(Reader, Writer) method now throws an exception if
+    the count is greater than an int
+  - Added a new copyLarge(InputStream, OutputStream) method that returns a long
+  - Added a new copyLarge(Reader, Writer) method that returns a long
 
 - CountingInputStream/CountingOutputStream [IO-84]
   - methods were declared as int thus the count was innacurate for large streams

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java?view=diff&rev=463154&r1=463153&r2=463154
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java Thu Oct 12
01:24:23 2006
@@ -982,20 +982,46 @@
      * This method buffers the input internally, so there is no need to use a
      * <code>BufferedInputStream</code>.
      * <p>
-     * Large streams (over 2GB) will return an inaccurate result count as the
-     * type is an int. The copy will complete correctly however.
+     * Large streams (over 2GB) will throw an {@link ArithmeticException}
+     * after the copy has completed since the correct number of bytes
+     * cannot be returned as an int. For large streams use the
+     * <code>copyLarge(InputStream, OutputStream)</code> method.
      * 
      * @param input  the <code>InputStream</code> to read from
      * @param output  the <code>OutputStream</code> to write to
      * @return the number of bytes copied
      * @throws NullPointerException if the input or output is null
      * @throws IOException if an I/O error occurs
+     * @throws ArithmeticException if the byte count is too large
      * @since Commons IO 1.1
      */
     public static int copy(InputStream input, OutputStream output)
             throws IOException {
+        long count = copyLarge(input, output);
+        if (count > Integer.MAX_VALUE) {
+            throw new ArithmeticException("The byte count " + count + " is too large to be
converted to an int");
+        }
+        return (int)count;
+    }
+
+    /**
+     * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
+     * <code>OutputStream</code>.
+     * <p>
+     * This method buffers the input internally, so there is no need to use a
+     * <code>BufferedInputStream</code>.
+     * 
+     * @param input  the <code>InputStream</code> to read from
+     * @param output  the <code>OutputStream</code> to write to
+     * @return the number of bytes copied
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 1.3
+     */
+    public static long copyLarge(InputStream input, OutputStream output)
+            throws IOException {
         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
-        int count = 0;
+        long count = 0;
         int n = 0;
         while (-1 != (n = input.read(buffer))) {
             output.write(buffer, 0, n);
@@ -1062,19 +1088,43 @@
      * This method buffers the input internally, so there is no need to use a
      * <code>BufferedReader</code>.
      * <p>
-     * Large streams (over 2GB) will return an inaccurate result count as the
-     * type is an int. The copy will complete correctly however.
+     * Large streams (over 2GB) will throw an {@link ArithmeticException}
+     * after the copy has completed since the correct number of characters
+     * cannot be returned as an int. For large streams use the
+     * <code>copyLarge(Reader, Writer)</code> method.
      *
      * @param input  the <code>Reader</code> to read from
      * @param output  the <code>Writer</code> to write to
      * @return the number of characters copied
      * @throws NullPointerException if the input or output is null
      * @throws IOException if an I/O error occurs
+     * @throws ArithmeticException if the character count is too large
      * @since Commons IO 1.1
      */
     public static int copy(Reader input, Writer output) throws IOException {
+        long count = copyLarge(input, output);
+        if (count > Integer.MAX_VALUE) {
+            throw new ArithmeticException("The character count " + count + " is too large
to be converted to an int");
+        }
+        return (int)count;
+    }
+
+    /**
+     * Copy chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
+     * <p>
+     * This method buffers the input internally, so there is no need to use a
+     * <code>BufferedReader</code>.
+     *
+     * @param input  the <code>Reader</code> to read from
+     * @param output  the <code>Writer</code> to write to
+     * @return the number of characters copied
+     * @throws NullPointerException if the input or output is null
+     * @throws IOException if an I/O error occurs
+     * @since Commons IO 1.3
+     */
+    public static long copyLarge(Reader input, Writer output) throws IOException {
         char[] buffer = new char[DEFAULT_BUFFER_SIZE];
-        int count = 0;
+        long count = 0;
         int n = 0;
         while (-1 != (n = input.read(buffer))) {
             output.write(buffer, 0, n);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message