diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2016-03-21 23:29:06 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2016-03-21 23:29:06 +0000 |
commit | 3dfbdc46e5354b0233810ef57d9548858de4994e (patch) | |
tree | 14499fe877a5f0aa0903401dc73c580c713094b0 /src/java/org/apache/poi | |
parent | 95f959bcc6aec8d16466b2d80903ac95d097a5ab (diff) | |
download | poi-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')
-rw-r--r-- | src/java/org/apache/poi/poifs/dev/POIFSDump.java | 8 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/IOUtils.java | 27 |
2 files changed, 25 insertions, 10 deletions
diff --git a/src/java/org/apache/poi/poifs/dev/POIFSDump.java b/src/java/org/apache/poi/poifs/dev/POIFSDump.java index 0dfd4a290f..f8c00365ac 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSDump.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSDump.java @@ -33,12 +33,13 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSStream; import org.apache.poi.poifs.property.NPropertyTable; import org.apache.poi.poifs.storage.HeaderBlock; +import org.apache.poi.util.IOUtils; /** * Dump internal structure of a OLE2 file into file system */ public class POIFSDump { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { if (args.length == 0) { System.err.println("Must specify at least one file to dump"); System.exit(1); @@ -84,6 +85,8 @@ public class POIFSDump { dump(fs, startBlock, "mini-stream", file); } } + + fs.close(); } } @@ -93,8 +96,7 @@ public class POIFSDump { if(entry instanceof DocumentNode){ DocumentNode node = (DocumentNode)entry; DocumentInputStream is = new DocumentInputStream(node); - byte[] bytes = new byte[node.getSize()]; - is.read(bytes); + byte[] bytes = IOUtils.toByteArray(is); is.close(); OutputStream out = new FileOutputStream(new File(parent, node.getName().trim())); 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 |