Skip to content

Commit 28caccb

Browse files
committed
IO-410 Readfully() That Returns A Byte Array
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1545141 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9e15115 commit 28caccb

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50+
<action issue="IO-410" dev="sebb" type="add" due-to="BELUGA BEHR">
51+
Readfully() That Returns A Byte Array
52+
</action>
5053
<action issue="IO-395" dev="brentworden" type="add" due-to="BELUGA BEHR">
5154
Overload IOUtils buffer methods to accept buffer size
5255
</action>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,25 @@ public static void readFully(final InputStream input, final byte[] buffer) throw
29662966
readFully(input, buffer, 0, buffer.length);
29672967
}
29682968

2969+
/**
2970+
* Reads the requested number of bytes or fail if there are not enough left.
2971+
* <p>
2972+
* This allows for the possibility that {@link InputStream#read(byte[], int, int)} may
2973+
* not read as many bytes as requested (most likely because of reaching EOF).
2974+
*
2975+
* @param input where to read input from
2976+
* @param length length to read, must be >= 0
2977+
*
2978+
* @throws IOException if there is a problem reading the file
2979+
* @throws IllegalArgumentException if length is negative
2980+
* @throws EOFException if the number of bytes read was incorrect
2981+
*/
2982+
public static byte[] readFully(final InputStream input, final int length) throws IOException {
2983+
final byte[] buffer = new byte[length];
2984+
readFully(input, buffer, 0, buffer.length);
2985+
return buffer;
2986+
}
2987+
29692988
/**
29702989
* Reads the requested number of bytes or fail if there are not enough left.
29712990
* <p>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,17 @@ public void testReadFully_InputStream_ByteArray() throws Exception {
642642

643643
}
644644

645+
public void testReadFully_InputStream__ReturnByteArray() throws Exception {
646+
final byte[] bytes = "abcd1234".getBytes("UTF-8");
647+
final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
648+
649+
final byte[] result = IOUtils.readFully(stream, bytes.length);
650+
651+
IOUtils.closeQuietly(stream);
652+
653+
assertEqualContent(result, bytes);
654+
}
655+
645656
public void testReadFully_InputStream_Offset() throws Exception {
646657
final byte[] bytes = "abcd1234".getBytes("UTF-8");
647658
final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);

0 commit comments

Comments
 (0)