Skip to content

Commit 09e43b2

Browse files
committed
Add skipFully() methods for InputStream and Reader
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@920114 13f79535-47bb-0310-9956-ffa450edef68
1 parent e5d3507 commit 09e43b2

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.ByteArrayInputStream;
2222
import java.io.CharArrayWriter;
2323
import java.io.Closeable;
24+
import java.io.EOFException;
2425
import java.io.File;
2526
import java.io.IOException;
2627
import java.io.InputStream;
@@ -1398,4 +1399,51 @@ public static boolean contentEquals(Reader input1, Reader input2)
13981399
return (ch2 == -1);
13991400
}
14001401

1402+
/**
1403+
* Skip the requested number of bytes or fail if there are not enough left.
1404+
* <p>
1405+
* This allows for the possibility that {@link InputStream#skip(long)} may
1406+
* not skip as many bytes as requested (most likely because of reaching EOF).
1407+
*
1408+
* @param input stream to skip
1409+
* @param toSkip the number of bytes to skip
1410+
* @see InputStream#skip(long)
1411+
*
1412+
* @throws IOException if there is a problem reading the file
1413+
* @throws IllegalArgumentException if toSkip is negative
1414+
* @throws EOFException if the number of bytes skipped was incorrect
1415+
*/
1416+
public static void skipFully(InputStream input, long toSkip) throws IOException {
1417+
if (toSkip < 0){
1418+
throw new IllegalArgumentException("Bytes to skip must not be negative: "+toSkip);
1419+
}
1420+
long skipped = input.skip(toSkip);
1421+
if (skipped != toSkip) {
1422+
throw new EOFException("Bytes to skip: "+toSkip+" actual: "+skipped);
1423+
}
1424+
}
1425+
1426+
/**
1427+
* Skip the requested number of bytes or fail if there are not enough left.
1428+
* <p>
1429+
* This allows for the possibility that {@link Reader#skip(long)} may
1430+
* not skip as many bytes as requested (most likely because of reaching EOF).
1431+
*
1432+
* @param input stream to skip
1433+
* @param toSkip the number of bytes to skip
1434+
* @see Reader#skip(long)
1435+
*
1436+
* @throws IOException if there is a problem reading the file
1437+
* @throws IllegalArgumentException if toSkip is negative
1438+
* @throws EOFException if the number of bytes skipped was incorrect
1439+
*/
1440+
public static void skipFully(Reader input, long toSkip) throws IOException {
1441+
if (toSkip < 0){
1442+
throw new IllegalArgumentException("Bytes to skip must not be negative: "+toSkip);
1443+
}
1444+
long skipped = input.skip(toSkip);
1445+
if (skipped != toSkip) {
1446+
throw new EOFException("Bytes to skip: "+toSkip+" actual: "+skipped);
1447+
}
1448+
}
14011449
}

src/test/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import java.io.ByteArrayInputStream;
20+
import java.io.CharArrayReader;
1921
import java.io.File;
2022
import java.io.FileInputStream;
2123
import java.io.FileOutputStream;
@@ -459,4 +461,47 @@ public void testReadLines_Reader() throws Exception {
459461
deleteFile(file);
460462
}
461463
}
464+
465+
public void testSkipStream() throws Exception{
466+
final int size = 1027;
467+
468+
InputStream input = new ByteArrayInputStream(new byte [size]);
469+
try {
470+
IOUtils.skipFully(input, -1);
471+
fail("Should have failed with IllegalArgumentException");
472+
} catch (IllegalArgumentException expected){
473+
// expected
474+
}
475+
IOUtils.skipFully(input, 0);
476+
IOUtils.skipFully(input, size-1);
477+
try {
478+
IOUtils.skipFully(input, 2);
479+
fail("Should have failed with IOException");
480+
} catch (IOException expected) {
481+
// expected
482+
}
483+
IOUtils.closeQuietly(input);
484+
485+
}
486+
487+
public void testSkipReader() throws Exception{
488+
final int size = 1027;
489+
490+
Reader input = new CharArrayReader(new char[size]);
491+
IOUtils.skipFully(input, 0);
492+
IOUtils.skipFully(input, size-3);
493+
try {
494+
IOUtils.skipFully(input, -1);
495+
fail("Should have failed with IllegalArgumentException");
496+
} catch (IllegalArgumentException expected){
497+
// expected
498+
}
499+
try {
500+
IOUtils.skipFully(input, 5);
501+
fail("Should have failed with IOException");
502+
} catch (IOException expected) {
503+
// expected
504+
}
505+
IOUtils.closeQuietly(input);
506+
}
462507
}

0 commit comments

Comments
 (0)