You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2011/11/16 03:47:59 UTC
svn commit: r1202513 - 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
Author: sebb
Date: Wed Nov 16 02:47:59 2011
New Revision: 1202513
URL: http://svn.apache.org/viewvc?rev=1202513&view=rev
Log:
IO-290 Add read/readFully methods to IOUtils
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=1202513&r1=1202512&r2=1202513&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Nov 16 02:47:59 2011
@@ -39,7 +39,10 @@ The <action> type attribute can be add,u
</properties>
<body>
- <release version="2.1.1" date="TBA">
+ <release version="2.2" date="TBA">
+ <action dev="sebb" type="add" issue="IO-290" due-to="sebb">
+ Add read/readFully methods to IOUtils
+ </action>
<action dev="sebb" type="add" issue="IO-288" due-to="Georg Henzler">
Supply a ReversedLinesFileReader
</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=1202513&r1=1202512&r2=1202513&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 Wed Nov 16 02:47:59 2011
@@ -1791,7 +1791,174 @@ public class IOUtils {
public static void skipFully(Reader input, long toSkip) throws IOException {
long skipped = skip(input, toSkip);
if (skipped != toSkip) {
- throw new EOFException("Bytes to skip: "+toSkip+" actual: "+skipped);
+ throw new EOFException("Chars to skip: "+toSkip+" actual: "+skipped);
}
}
+
+
+ /**
+ * Read 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
+ * subclasses of {@link Reader}.
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @param offset inital offset into buffer
+ * @param length length to read, must be >= 0
+ * @return actual length read; may be less than requested if EOF was reached
+ * @throws IOException if a read error occurs
+ */
+ public static int read(Reader input, char[] buffer, int offset, int length) throws IOException {
+ if (length < 0){
+ throw new IllegalArgumentException("Length must not be negative: "+length);
+ }
+ int remaining = length;
+ while ( remaining > 0 ) {
+ int location = ( length - remaining );
+ int count = input.read( buffer, location, remaining );
+ if ( -1 == count ) { // EOF
+ break;
+ }
+ remaining -= count;
+ }
+ return length - remaining;
+ }
+
+ /**
+ * Read 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
+ * subclasses of {@link Reader}.
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @return actual length read; may be less than requested if EOF was reached
+ * @throws IOException if a read error occurs
+ */
+ public static int read(Reader input, char[] buffer) throws IOException {
+ return read(input, buffer, 0, buffer.length);
+ }
+
+ /**
+ * Read bytes from an input stream.
+ * This implementation guarantees that it will read as many bytes
+ * as possible before giving up; this may not always be the case for
+ * subclasses of {@link InputStream}.
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @param offset inital offset into buffer
+ * @param length length to read, must be >= 0
+ * @return actual length read; may be less than requested if EOF was reached
+ * @throws IOException if a read error occurs
+ */
+ public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException {
+ if (length < 0){
+ throw new IllegalArgumentException("Length must not be negative: "+length);
+ }
+ int remaining = length;
+ while ( remaining > 0 ) {
+ int location = ( length - remaining );
+ int count = input.read( buffer, location, remaining );
+ if ( -1 == count ) { // EOF
+ break;
+ }
+ remaining -= count;
+ }
+ return length - remaining;
+ }
+
+ /**
+ * Read bytes from an input stream.
+ * This implementation guarantees that it will read as many bytes
+ * as possible before giving up; this may not always be the case for
+ * subclasses of {@link InputStream}.
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @return actual length read; may be less than requested if EOF was reached
+ * @throws IOException if a read error occurs
+ */
+ public static int read(InputStream input, byte[] buffer) throws IOException {
+ return read(input, buffer, 0, buffer.length);
+ }
+
+ /**
+ * Read the requested number of characters or fail if there are not enough left.
+ * <p>
+ * This allows for the possibility that {@link Reader#read(char[], int, int)} may
+ * not skip as many characters as requested (most likely because of reaching EOF).
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @param offset inital offset into buffer
+ * @param length length to read, must be >= 0
+ *
+ * @throws IOException if there is a problem reading the file
+ * @throws IllegalArgumentException if length is negative
+ * @throws EOFException if the number of characters read was incorrect
+ */
+ public static void readFully(Reader input, char[] buffer, int offset, int length) throws IOException {
+ int actual = read(input, buffer, offset, length);
+ if (actual != length){
+ throw new EOFException("Length to read: "+length+" actual: "+actual);
+ }
+ }
+
+ /**
+ * Read the requested number of characters or fail if there are not enough left.
+ * <p>
+ * This allows for the possibility that {@link Reader#read(char[], int, int)} may
+ * not skip as many characters as requested (most likely because of reaching EOF).
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ *
+ * @throws IOException if there is a problem reading the file
+ * @throws IllegalArgumentException if length is negative
+ * @throws EOFException if the number of characters read was incorrect
+ */
+ public static void readFully(Reader input, char[] buffer) throws IOException {
+ readFully(input, buffer, 0, buffer.length);
+ }
+
+ /**
+ * Read the requested number of bytes or fail if there are not enough left.
+ * <p>
+ * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
+ * not skip as many bytes as requested (most likely because of reaching EOF).
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ * @param offset inital offset into buffer
+ * @param length length to read, must be >= 0
+ *
+ * @throws IOException if there is a problem reading the file
+ * @throws IllegalArgumentException if length is negative
+ * @throws EOFException if the number of bytes read was incorrect
+ */
+ public static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException {
+ int actual = read(input, buffer, offset, length);
+ if (actual != length){
+ throw new EOFException("Length to read: "+length+" actual: "+actual);
+ }
+ }
+
+ /**
+ * Read the requested number of bytes or fail if there are not enough left.
+ * <p>
+ * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
+ * not skip as many bytes as requested (most likely because of reaching EOF).
+ *
+ * @param input where to read input from
+ * @param buffer destination
+ *
+ * @throws IOException if there is a problem reading the file
+ * @throws IllegalArgumentException if length is negative
+ * @throws EOFException if the number of bytes read was incorrect
+ */
+ public static void readFully(InputStream input, byte[] buffer) throws IOException {
+ readFully(input, buffer, 0, buffer.length);
+ }
}
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=1202513&r1=1202512&r2=1202513&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 Wed Nov 16 02:47:59 2011
@@ -18,6 +18,7 @@ package org.apache.commons.io;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
+import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -687,4 +688,52 @@ public class IOUtilsTestCase extends Fil
r2 = new CharArrayReader("321\r\n".toCharArray());
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
}
+
+ public void testReadStream() throws Exception{
+ final int size = 1027;
+
+ byte[] buffer = new byte[size];
+
+ InputStream input = new ByteArrayInputStream(new byte [size]);
+ try {
+ IOUtils.readFully(input, buffer, 0, -1);
+ fail("Should have failed with IllegalArgumentException");
+ } catch (IllegalArgumentException expected){
+ // expected
+ }
+ IOUtils.readFully(input, buffer, 0, 0);
+ IOUtils.readFully(input, buffer, 0, size-1);
+ try {
+ IOUtils.readFully(input, buffer, 0, 2);
+ fail("Should have failed with EOFxception");
+ } catch (EOFException expected) {
+ // expected
+ }
+ IOUtils.closeQuietly(input);
+
+ }
+
+ public void testReadReader() throws Exception{
+ final int size = 1027;
+
+ char [] buffer = new char[size];
+
+ Reader input = new CharArrayReader(new char[size]);
+ IOUtils.readFully(input, buffer, 0, 0);
+ IOUtils.readFully(input, buffer, 0, size-3);
+ try {
+ IOUtils.readFully(input, buffer, 0, -1);
+ fail("Should have failed with IllegalArgumentException");
+ } catch (IllegalArgumentException expected){
+ // expected
+ }
+ try {
+ IOUtils.readFully(input, buffer, 0, 5);
+ fail("Should have failed with EOFException");
+ } catch (EOFException expected) {
+ // expected
+ }
+ IOUtils.closeQuietly(input);
+ }
+
}