aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/util/IOUtils.java
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2016-03-21 23:29:06 +0000
committerAndreas Beeker <kiwiwings@apache.org>2016-03-21 23:29:06 +0000
commit3dfbdc46e5354b0233810ef57d9548858de4994e (patch)
tree14499fe877a5f0aa0903401dc73c580c713094b0 /src/java/org/apache/poi/util/IOUtils.java
parent95f959bcc6aec8d16466b2d80903ac95d097a5ab (diff)
downloadpoi-3dfbdc46e5354b0233810ef57d9548858de4994e.tar.gz
poi-3dfbdc46e5354b0233810ef57d9548858de4994e.zip
findbugs fixes - RR_NOT_CHECKED
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736112 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/util/IOUtils.java')
-rw-r--r--src/java/org/apache/poi/util/IOUtils.java27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java
index cee48561d3..eef58b30c4 100644
--- a/src/java/org/apache/poi/util/IOUtils.java
+++ b/src/java/org/apache/poi/util/IOUtils.java
@@ -69,20 +69,33 @@ public final class IOUtils {
* Reads all the data from the input stream, and returns the bytes read.
*/
public static byte[] toByteArray(InputStream stream) throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ return toByteArray(stream, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Reads up to {@code length} bytes from the input stream, and returns the bytes read.
+ */
+ public static byte[] toByteArray(InputStream stream, int length) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(length == Integer.MAX_VALUE ? 4096 : length);
byte[] buffer = new byte[4096];
- int read = 0;
- while (read != -1) {
- read = stream.read(buffer);
- if (read > 0) {
- baos.write(buffer, 0, read);
+ int totalBytes = 0, readBytes = 0;
+ do {
+ readBytes = stream.read(buffer, 0, Math.min(buffer.length, length-totalBytes));
+ totalBytes += Math.max(readBytes,0);
+ if (readBytes > 0) {
+ baos.write(buffer, 0, readBytes);
}
- }
+ } while (totalBytes < length && readBytes > -1);
+ if (length != Integer.MAX_VALUE && totalBytes < length) {
+ throw new IOException("unexpected EOF");
+ }
+
return baos.toByteArray();
}
+
/**
* Returns an array (that shouldn't be written to!) of the
* ByteBuffer. Will be of the requested length, or possibly