commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1415648 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/IOUtils.java test/java/org/apache/commons/io/IOUtilsTestCase.java
Date Fri, 30 Nov 2012 14:18:48 GMT
Author: ggregory
Date: Fri Nov 30 14:18:47 2012
New Revision: 1415648

URL: http://svn.apache.org/viewvc?rev=1415648&view=rev
Log:
<action issue="IO-359" dev="ggregory" type="add" due-to="yukoba">Add IOUtils.skip and
skipFully(ReadableByteChannel, long).</action>

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1415648&r1=1415647&r2=1415648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri Nov 30 14:18:47 2012
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="201?-??-??" description="New features and bug fixes.">
   
+      <action issue="IO-359" dev="ggregory" type="add" due-to="yukoba">
+        Add IOUtils.skip and skipFully(ReadableByteChannel, long).
+      </action>            
       <action issue="IO-358" dev="ggregory" type="add" due-to="yukoba">
         Add IOUtils.read and readFully(ReadableByteChannel, ByteBuffer buffer).
       </action>            

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1415648&r1=1415647&r2=1415648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Fri Nov 30 14:18:47
2012
@@ -2343,6 +2343,37 @@ public class IOUtils {
     }
 
     /**
+     * Skip bytes from a ReadableByteChannel.
+     * This implementation guarantees that it will read as many bytes
+     * as possible before giving up.
+     *
+     * @param input ReadableByteChannel to skip
+     * @param toSkip number of bytes to skip.
+     * @return number of bytes actually skipped.
+     *
+     * @throws IOException if there is a problem reading the ReadableByteChannel
+     * @throws IllegalArgumentException if toSkip is negative
+     * @since 2.5
+     */
+    public static long skip(ReadableByteChannel input, long toSkip) throws IOException {
+        if (toSkip < 0) {
+            throw new IllegalArgumentException("Skip count must be non-negative, actual:
" + toSkip);
+        }
+        ByteBuffer skipByteBuffer = ByteBuffer.allocate((int) Math.min(toSkip, SKIP_BUFFER_SIZE));
+        long remain = toSkip;
+        while (remain > 0) {
+            skipByteBuffer.position(0);
+            skipByteBuffer.limit((int) Math.min(remain, SKIP_BUFFER_SIZE));
+            int n = input.read(skipByteBuffer);
+            if (n == EOF) {
+                break;
+            }
+            remain -= n;
+        }
+        return toSkip - remain;
+    }
+
+    /**
      * Skip characters from an input character stream.
      * This implementation guarantees that it will read as many characters
      * as possible before giving up; this may not always be the case for
@@ -2420,6 +2451,27 @@ public class IOUtils {
     }
 
     /**
+     * Skip the requested number of bytes or fail if there are not enough left.
+     *
+     * @param input ReadableByteChannel to skip
+     * @param toSkip the number of bytes to skip
+     *
+     * @throws IOException if there is a problem reading the ReadableByteChannel
+     * @throws IllegalArgumentException if toSkip is negative
+     * @throws EOFException if the number of bytes skipped was incorrect
+     * @since 2.5
+     */
+    public static void skipFully(ReadableByteChannel input, long toSkip) throws IOException
{
+        if (toSkip < 0) {
+            throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
+        }
+        long skipped = skip(input, toSkip);
+        if (skipped != toSkip) {
+            throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
+        }
+    }
+
+    /**
      * Skip the requested number of characters or fail if there are not enough left.
      * <p>
      * This allows for the possibility that {@link Reader#skip(long)} may

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1415648&r1=1415647&r2=1415648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Fri Nov
30 14:18:47 2012
@@ -739,6 +739,19 @@ public class IOUtilsTestCase extends Fil
         }
     }
 
+    public void testSkip_ReadableByteChannel() throws Exception {
+        FileInputStream fileInputStream = new FileInputStream(m_testFile);
+        FileChannel fileChannel = fileInputStream.getChannel();
+        try {
+            assertEquals(FILE_SIZE - 10, IOUtils.skip(fileChannel, FILE_SIZE - 10));
+            assertEquals(10, IOUtils.skip(fileChannel, 20));
+            assertEquals(0, IOUtils.skip(fileChannel, 10));
+        } finally {
+            IOUtils.closeQuietly(fileChannel);
+            IOUtils.closeQuietly(fileInputStream);
+        }
+    }
+
     public void testSkipFully_InputStream() throws Exception {
         final int size = 1027;
 
@@ -761,6 +774,30 @@ public class IOUtilsTestCase extends Fil
 
     }
 
+    public void testSkipFully_ReadableByteChannel() throws Exception {
+        FileInputStream fileInputStream = new FileInputStream(m_testFile);
+        FileChannel fileChannel = fileInputStream.getChannel();
+        try {
+            try {
+                IOUtils.skipFully(fileChannel, -1);
+                fail("Should have failed with IllegalArgumentException");
+            } catch (IllegalArgumentException expected) {
+                // expected
+            }
+            IOUtils.skipFully(fileChannel, 0);
+            IOUtils.skipFully(fileChannel, FILE_SIZE - 1);
+            try {
+                IOUtils.skipFully(fileChannel, 2);
+                fail("Should have failed with IOException");
+            } catch (IOException expected) {
+                // expected
+            }
+        } finally {
+            IOUtils.closeQuietly(fileChannel);
+            IOUtils.closeQuietly(fileInputStream);
+        }
+    }
+
     public void testSkipFully_Reader() throws Exception {
         final int size = 1027;
 



Mime
View raw message