From 3dfbdc46e5354b0233810ef57d9548858de4994e Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 21 Mar 2016 23:29:06 +0000 Subject: [PATCH] findbugs fixes - RR_NOT_CHECKED git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736112 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/poifs/dev/POIFSDump.java | 8 +++--- src/java/org/apache/poi/util/IOUtils.java | 27 ++++++++++++++----- .../src/org/apache/poi/hdgf/HDGFDiagram.java | 11 ++++---- .../org/apache/poi/hpbf/dev/HPBFDumper.java | 10 +++---- .../org/apache/poi/hpbf/model/HPBFPart.java | 16 +++++------ .../apache/poi/hslf/dev/SlideShowDumper.java | 12 ++++----- .../QuickButCruddyTextExtractor.java | 21 ++++++++------- .../apache/poi/hwmf/record/HwmfBitmap16.java | 7 ++--- .../apache/poi/hwmf/record/HwmfBitmapDib.java | 6 ++--- .../apache/poi/hwmf/record/HwmfEscape.java | 5 ++-- .../poi/hwmf/record/HwmfPlaceableHeader.java | 2 +- .../src/org/apache/poi/hwpf/HWPFDocument.java | 13 ++++----- .../org/apache/poi/hwpf/HWPFDocumentCore.java | 6 ++--- 13 files changed, 77 insertions(+), 67 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 diff --git a/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java b/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java index 405bf69c4b..687bdb3131 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/HDGFDiagram.java @@ -19,6 +19,7 @@ package org.apache.poi.hdgf; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.POIDocument; @@ -30,9 +31,9 @@ import org.apache.poi.hdgf.streams.Stream; import org.apache.poi.hdgf.streams.StringsStream; import org.apache.poi.hdgf.streams.TrailerStream; import org.apache.poi.poifs.filesystem.DirectoryNode; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LocaleUtil; @@ -73,12 +74,10 @@ public final class HDGFDiagram extends POIDocument { public HDGFDiagram(DirectoryNode dir) throws IOException { super(dir); - DocumentEntry docProps = - (DocumentEntry)dir.getEntry("VisioDocument"); - // Grab the document stream - _docstream = new byte[docProps.getSize()]; - dir.createDocumentInputStream("VisioDocument").read(_docstream); + InputStream is = dir.createDocumentInputStream("VisioDocument"); + _docstream = IOUtils.toByteArray(is); + is.close(); // Check it's really visio String typeString = new String(_docstream, 0, 20, LocaleUtil.CHARSET_1252 ); diff --git a/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java b/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java index 7786950374..48ad50973f 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java @@ -24,8 +24,8 @@ import java.io.InputStream; import org.apache.poi.ddf.DefaultEscherRecordFactory; import org.apache.poi.ddf.EscherRecord; import org.apache.poi.poifs.filesystem.DirectoryNode; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.StringUtil; @@ -47,12 +47,10 @@ public final class HPBFDumper { } private static byte[] getData(DirectoryNode dir, String name) throws IOException { - DocumentEntry docProps = - (DocumentEntry)dir.getEntry(name); - // Grab the document stream - byte[] d = new byte[docProps.getSize()]; - dir.createDocumentInputStream(name).read(d); + InputStream is = dir.createDocumentInputStream(name); + byte[] d = IOUtils.toByteArray(is); + is.close(); // All done return d; diff --git a/src/scratchpad/src/org/apache/poi/hpbf/model/HPBFPart.java b/src/scratchpad/src/org/apache/poi/hpbf/model/HPBFPart.java index bd727e1b37..4379f4a81f 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/model/HPBFPart.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/model/HPBFPart.java @@ -20,9 +20,10 @@ package org.apache.poi.hpbf.model; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import org.apache.poi.poifs.filesystem.DirectoryNode; -import org.apache.poi.poifs.filesystem.DocumentEntry; +import org.apache.poi.util.IOUtils; /** * Parent class of all HPBF sub-parts, handling @@ -39,17 +40,14 @@ public abstract class HPBFPart { DirectoryNode dir = getDir(path, baseDir); String name = path[path.length-1]; - DocumentEntry docProps; - try { - docProps = (DocumentEntry)dir.getEntry(name); - } catch (FileNotFoundException e) { - throw new IllegalArgumentException("File invalid - failed to find document entry '" - + name + "'"); + if (!dir.hasEntry(name)) { + throw new IllegalArgumentException("File invalid - failed to find document entry '" + name + "'"); } // Grab the data from the part stream - data = new byte[docProps.getSize()]; - dir.createDocumentInputStream(name).read(data); + InputStream is = dir.createDocumentInputStream(name); + data = IOUtils.toByteArray(is); + is.close(); } private DirectoryNode getDir(String[] path, DirectoryNode baseDir) { DirectoryNode dir = baseDir; diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java index df2d38bdc4..0cfaa3fc1e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.dev; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.PrintStream; import java.util.Locale; @@ -28,9 +29,9 @@ import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherTextboxRecord; import org.apache.poi.hslf.record.HSLFEscherRecordFactory; import org.apache.poi.hslf.record.RecordTypes; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.util.HexDump; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; /** @@ -94,13 +95,10 @@ public final class SlideShowDumper { * @throws IOException if there is a problem while parsing the document. */ public SlideShowDumper(NPOIFSFileSystem filesystem, PrintStream out) throws IOException { - // Get the main document stream - DocumentEntry docProps = - (DocumentEntry)filesystem.getRoot().getEntry("PowerPoint Document"); - // Grab the document stream - docstream = new byte[docProps.getSize()]; - filesystem.createDocumentInputStream("PowerPoint Document").read(docstream); + InputStream is = filesystem.createDocumentInputStream("PowerPoint Document"); + docstream = IOUtils.toByteArray(is); + is.close(); this.out = out; } diff --git a/src/scratchpad/src/org/apache/poi/hslf/extractor/QuickButCruddyTextExtractor.java b/src/scratchpad/src/org/apache/poi/hslf/extractor/QuickButCruddyTextExtractor.java index 8396ae1234..1ecc49a94a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/extractor/QuickButCruddyTextExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hslf/extractor/QuickButCruddyTextExtractor.java @@ -17,18 +17,20 @@ package org.apache.poi.hslf.extractor; -import java.io.*; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; -import org.apache.poi.hslf.record.*; +import org.apache.poi.hslf.record.CString; +import org.apache.poi.hslf.record.Record; +import org.apache.poi.hslf.record.RecordTypes; +import org.apache.poi.hslf.record.TextBytesAtom; +import org.apache.poi.hslf.record.TextCharsAtom; import org.apache.poi.hslf.usermodel.HSLFTextParagraph; -import org.apache.poi.hslf.usermodel.HSLFTextShape; -import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; /** @@ -77,7 +79,8 @@ public final class QuickButCruddyTextExtractor { * Creates an extractor from a given file name * @param fileName */ - public QuickButCruddyTextExtractor(String fileName) throws IOException { + @SuppressWarnings("resource") + public QuickButCruddyTextExtractor(String fileName) throws IOException { this(new NPOIFSFileSystem(new File(fileName))); } @@ -85,6 +88,7 @@ public final class QuickButCruddyTextExtractor { * Creates an extractor from a given input stream * @param iStream */ + @SuppressWarnings("resource") public QuickButCruddyTextExtractor(InputStream iStream) throws IOException { this(new NPOIFSFileSystem(iStream)); is = iStream; @@ -98,10 +102,9 @@ public final class QuickButCruddyTextExtractor { fs = poifs; // Find the PowerPoint bit, and get out the bytes - DocumentEntry docProps = - (DocumentEntry)fs.getRoot().getEntry("PowerPoint Document"); - pptContents = new byte[docProps.getSize()]; - fs.createDocumentInputStream("PowerPoint Document").read(pptContents); + InputStream pptIs = fs.createDocumentInputStream("PowerPoint Document"); + pptContents = IOUtils.toByteArray(pptIs); + pptIs.close(); } diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmap16.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmap16.java index 2ff0d118a8..4439b784dd 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmap16.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmap16.java @@ -20,6 +20,7 @@ package org.apache.poi.hwmf.record; import java.awt.image.BufferedImage; import java.io.IOException; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInputStream; @@ -72,9 +73,9 @@ public class HwmfBitmap16 { size += 18+LittleEndianConsts.INT_SIZE; } - int bytes = (((width * bitsPixel + 15) >> 4) << 1) * height; - byte buf[] = new byte[bytes]; - leis.read(buf); + int length = (((width * bitsPixel + 15) >> 4) << 1) * height; + @SuppressWarnings("unused") + byte buf[] = IOUtils.toByteArray(leis, length); // TODO: this is not implemented ... please provide a sample, if it // ever happens to you, to come here ... diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java index ae7f5cb307..ba935f39f1 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfBitmapDib.java @@ -26,12 +26,13 @@ import java.io.InputStream; import javax.imageio.ImageIO; -import org.apache.poi.hssf.record.RecordFormatException; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInputStream; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.util.RecordFormatException; /** * The DeviceIndependentBitmap Object defines an image in device-independent bitmap (DIB) format. @@ -224,9 +225,8 @@ public class HwmfBitmapDib { int fileSize = (headerImageSize < headerSize) ? recordSize : (int)Math.min(introSize+headerImageSize,recordSize); - imageData = new byte[fileSize]; leis.reset(); - leis.read(imageData, 0, fileSize); + imageData = IOUtils.toByteArray(leis, fileSize); assert( headerSize != 0x0C || ((((headerWidth * headerPlanes * headerBitCount.flag + 31) & ~31) / 8) * Math.abs(headerHeight)) == headerImageSize); diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfEscape.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfEscape.java index b005abc8c0..c1714674a1 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfEscape.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfEscape.java @@ -21,6 +21,7 @@ import java.io.IOException; import org.apache.poi.hwmf.draw.HwmfGraphics; import org.apache.poi.util.HexDump; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianInputStream; @@ -192,8 +193,8 @@ public class HwmfEscape implements HwmfRecord { public int init(LittleEndianInputStream leis, long recordSize, int recordFunction) throws IOException { escapeFunction = EscapeFunction.valueOf(leis.readUShort()); byteCount = leis.readUShort(); - escapeData = new byte[byteCount]; - leis.read(escapeData); + escapeData = IOUtils.toByteArray(leis,byteCount); + return 2*LittleEndianConsts.SHORT_SIZE+byteCount; } diff --git a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPlaceableHeader.java b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPlaceableHeader.java index 8d65ba002f..2ca0cea151 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPlaceableHeader.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/record/HwmfPlaceableHeader.java @@ -45,7 +45,7 @@ public class HwmfPlaceableHeader { int y1 = leis.readShort(); int x2 = leis.readShort(); int y2 = leis.readShort(); - bounds = new Rectangle2D.Double(x1, y1, x2-x1, y2-y1); + bounds = new Rectangle2D.Double(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x2-x1), Math.abs(y2-y1)); /* * Inch (2 bytes): The number of logical units per inch used to represent the image. diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java index 19f7b84185..8bc5ba6f78 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java @@ -18,7 +18,6 @@ package org.apache.poi.hwpf; import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -69,6 +68,7 @@ import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.EntryUtils; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; /** @@ -230,17 +230,14 @@ public final class HWPFDocument extends HWPFDocumentCore } // Grab the table stream. - DocumentEntry tableProps; - try { - tableProps = - (DocumentEntry)directory.getEntry(name); - } catch(FileNotFoundException fnfe) { + if (!directory.hasEntry(name)) { throw new IllegalStateException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)"); } // read in the table stream. - _tableStream = new byte[tableProps.getSize()]; - directory.createDocumentInputStream(name).read(_tableStream); + InputStream is = directory.createDocumentInputStream(name); + _tableStream = IOUtils.toByteArray(is); + is.close(); _fib.fillVariableFields(_mainStream, _tableStream); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java index 74c73d7524..12153e6abd 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocumentCore.java @@ -38,6 +38,7 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; @@ -85,14 +86,13 @@ public abstract class HWPFDocumentCore extends POIDocument } /** - * Takens an InputStream, verifies that it's not RTF or PDF, builds a + * Takes an InputStream, verifies that it's not RTF or PDF, builds a * POIFSFileSystem from it, and returns that. */ public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException { // Open a PushbackInputStream, so we can peek at the first few bytes PushbackInputStream pis = new PushbackInputStream(istream,6); - byte[] first6 = new byte[6]; - pis.read(first6); + byte[] first6 = IOUtils.toByteArray(pis, 6); // Does it start with {\rtf ? If so, it's really RTF if(first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r' -- 2.39.5