findbugs fixes - RR_NOT_CHECKED

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736112 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-03-21 23:29:06 +00:00
parent 8d71080604
commit 762864d0f2
13 changed files with 77 additions and 67 deletions

View File

@ -33,12 +33,13 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.NPOIFSStream; import org.apache.poi.poifs.filesystem.NPOIFSStream;
import org.apache.poi.poifs.property.NPropertyTable; import org.apache.poi.poifs.property.NPropertyTable;
import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.util.IOUtils;
/** /**
* Dump internal structure of a OLE2 file into file system * Dump internal structure of a OLE2 file into file system
*/ */
public class POIFSDump { public class POIFSDump {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws IOException {
if (args.length == 0) { if (args.length == 0) {
System.err.println("Must specify at least one file to dump"); System.err.println("Must specify at least one file to dump");
System.exit(1); System.exit(1);
@ -84,6 +85,8 @@ public class POIFSDump {
dump(fs, startBlock, "mini-stream", file); dump(fs, startBlock, "mini-stream", file);
} }
} }
fs.close();
} }
} }
@ -93,8 +96,7 @@ public class POIFSDump {
if(entry instanceof DocumentNode){ if(entry instanceof DocumentNode){
DocumentNode node = (DocumentNode)entry; DocumentNode node = (DocumentNode)entry;
DocumentInputStream is = new DocumentInputStream(node); DocumentInputStream is = new DocumentInputStream(node);
byte[] bytes = new byte[node.getSize()]; byte[] bytes = IOUtils.toByteArray(is);
is.read(bytes);
is.close(); is.close();
OutputStream out = new FileOutputStream(new File(parent, node.getName().trim())); OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));

View File

@ -69,20 +69,33 @@ public final class IOUtils {
* Reads all the data from the input stream, and returns the bytes read. * Reads all the data from the input stream, and returns the bytes read.
*/ */
public static byte[] toByteArray(InputStream stream) throws IOException { 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]; byte[] buffer = new byte[4096];
int read = 0; int totalBytes = 0, readBytes = 0;
while (read != -1) { do {
read = stream.read(buffer); readBytes = stream.read(buffer, 0, Math.min(buffer.length, length-totalBytes));
if (read > 0) { totalBytes += Math.max(readBytes,0);
baos.write(buffer, 0, read); 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(); return baos.toByteArray();
} }
/** /**
* Returns an array (that shouldn't be written to!) of the * Returns an array (that shouldn't be written to!) of the
* ByteBuffer. Will be of the requested length, or possibly * ByteBuffer. Will be of the requested length, or possibly

View File

@ -19,6 +19,7 @@ package org.apache.poi.hdgf;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import org.apache.poi.POIDocument; 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.StringsStream;
import org.apache.poi.hdgf.streams.TrailerStream; import org.apache.poi.hdgf.streams.TrailerStream;
import org.apache.poi.poifs.filesystem.DirectoryNode; 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.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
@ -73,12 +74,10 @@ public final class HDGFDiagram extends POIDocument {
public HDGFDiagram(DirectoryNode dir) throws IOException { public HDGFDiagram(DirectoryNode dir) throws IOException {
super(dir); super(dir);
DocumentEntry docProps =
(DocumentEntry)dir.getEntry("VisioDocument");
// Grab the document stream // Grab the document stream
_docstream = new byte[docProps.getSize()]; InputStream is = dir.createDocumentInputStream("VisioDocument");
dir.createDocumentInputStream("VisioDocument").read(_docstream); _docstream = IOUtils.toByteArray(is);
is.close();
// Check it's really visio // Check it's really visio
String typeString = new String(_docstream, 0, 20, LocaleUtil.CHARSET_1252 ); String typeString = new String(_docstream, 0, 20, LocaleUtil.CHARSET_1252 );

View File

@ -24,8 +24,8 @@ import java.io.InputStream;
import org.apache.poi.ddf.DefaultEscherRecordFactory; import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherRecord; import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.poifs.filesystem.DirectoryNode; 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.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@ -47,12 +47,10 @@ public final class HPBFDumper {
} }
private static byte[] getData(DirectoryNode dir, String name) throws IOException { private static byte[] getData(DirectoryNode dir, String name) throws IOException {
DocumentEntry docProps =
(DocumentEntry)dir.getEntry(name);
// Grab the document stream // Grab the document stream
byte[] d = new byte[docProps.getSize()]; InputStream is = dir.createDocumentInputStream(name);
dir.createDocumentInputStream(name).read(d); byte[] d = IOUtils.toByteArray(is);
is.close();
// All done // All done
return d; return d;

View File

@ -20,9 +20,10 @@ package org.apache.poi.hpbf.model;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.poifs.filesystem.DirectoryNode; 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 * Parent class of all HPBF sub-parts, handling
@ -39,17 +40,14 @@ public abstract class HPBFPart {
DirectoryNode dir = getDir(path, baseDir); DirectoryNode dir = getDir(path, baseDir);
String name = path[path.length-1]; String name = path[path.length-1];
DocumentEntry docProps; if (!dir.hasEntry(name)) {
try { throw new IllegalArgumentException("File invalid - failed to find document entry '" + name + "'");
docProps = (DocumentEntry)dir.getEntry(name);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("File invalid - failed to find document entry '"
+ name + "'");
} }
// Grab the data from the part stream // Grab the data from the part stream
data = new byte[docProps.getSize()]; InputStream is = dir.createDocumentInputStream(name);
dir.createDocumentInputStream(name).read(data); data = IOUtils.toByteArray(is);
is.close();
} }
private DirectoryNode getDir(String[] path, DirectoryNode baseDir) { private DirectoryNode getDir(String[] path, DirectoryNode baseDir) {
DirectoryNode dir = baseDir; DirectoryNode dir = baseDir;

View File

@ -19,6 +19,7 @@ package org.apache.poi.hslf.dev;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Locale; import java.util.Locale;
@ -28,9 +29,9 @@ import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherTextboxRecord; import org.apache.poi.ddf.EscherTextboxRecord;
import org.apache.poi.hslf.record.HSLFEscherRecordFactory; import org.apache.poi.hslf.record.HSLFEscherRecordFactory;
import org.apache.poi.hslf.record.RecordTypes; 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.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; 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. * @throws IOException if there is a problem while parsing the document.
*/ */
public SlideShowDumper(NPOIFSFileSystem filesystem, PrintStream out) throws IOException { 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 // Grab the document stream
docstream = new byte[docProps.getSize()]; InputStream is = filesystem.createDocumentInputStream("PowerPoint Document");
filesystem.createDocumentInputStream("PowerPoint Document").read(docstream); docstream = IOUtils.toByteArray(is);
is.close();
this.out = out; this.out = out;
} }

View File

@ -17,18 +17,20 @@
package org.apache.poi.hslf.extractor; package org.apache.poi.hslf.extractor;
import java.io.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.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.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
/** /**
@ -77,7 +79,8 @@ public final class QuickButCruddyTextExtractor {
* Creates an extractor from a given file name * Creates an extractor from a given file name
* @param fileName * @param fileName
*/ */
public QuickButCruddyTextExtractor(String fileName) throws IOException { @SuppressWarnings("resource")
public QuickButCruddyTextExtractor(String fileName) throws IOException {
this(new NPOIFSFileSystem(new File(fileName))); this(new NPOIFSFileSystem(new File(fileName)));
} }
@ -85,6 +88,7 @@ public final class QuickButCruddyTextExtractor {
* Creates an extractor from a given input stream * Creates an extractor from a given input stream
* @param iStream * @param iStream
*/ */
@SuppressWarnings("resource")
public QuickButCruddyTextExtractor(InputStream iStream) throws IOException { public QuickButCruddyTextExtractor(InputStream iStream) throws IOException {
this(new NPOIFSFileSystem(iStream)); this(new NPOIFSFileSystem(iStream));
is = iStream; is = iStream;
@ -98,10 +102,9 @@ public final class QuickButCruddyTextExtractor {
fs = poifs; fs = poifs;
// Find the PowerPoint bit, and get out the bytes // Find the PowerPoint bit, and get out the bytes
DocumentEntry docProps = InputStream pptIs = fs.createDocumentInputStream("PowerPoint Document");
(DocumentEntry)fs.getRoot().getEntry("PowerPoint Document"); pptContents = IOUtils.toByteArray(pptIs);
pptContents = new byte[docProps.getSize()]; pptIs.close();
fs.createDocumentInputStream("PowerPoint Document").read(pptContents);
} }

View File

@ -20,6 +20,7 @@ package org.apache.poi.hwmf.record;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianInputStream; import org.apache.poi.util.LittleEndianInputStream;
@ -72,9 +73,9 @@ public class HwmfBitmap16 {
size += 18+LittleEndianConsts.INT_SIZE; size += 18+LittleEndianConsts.INT_SIZE;
} }
int bytes = (((width * bitsPixel + 15) >> 4) << 1) * height; int length = (((width * bitsPixel + 15) >> 4) << 1) * height;
byte buf[] = new byte[bytes]; @SuppressWarnings("unused")
leis.read(buf); byte buf[] = IOUtils.toByteArray(leis, length);
// TODO: this is not implemented ... please provide a sample, if it // TODO: this is not implemented ... please provide a sample, if it
// ever happens to you, to come here ... // ever happens to you, to come here ...

View File

@ -26,12 +26,13 @@ import java.io.InputStream;
import javax.imageio.ImageIO; 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.LittleEndian;
import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianInputStream; import org.apache.poi.util.LittleEndianInputStream;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.util.RecordFormatException;
/** /**
* The DeviceIndependentBitmap Object defines an image in device-independent bitmap (DIB) format. * 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); int fileSize = (headerImageSize < headerSize) ? recordSize : (int)Math.min(introSize+headerImageSize,recordSize);
imageData = new byte[fileSize];
leis.reset(); 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); assert( headerSize != 0x0C || ((((headerWidth * headerPlanes * headerBitCount.flag + 31) & ~31) / 8) * Math.abs(headerHeight)) == headerImageSize);

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.poi.hwmf.draw.HwmfGraphics; import org.apache.poi.hwmf.draw.HwmfGraphics;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianInputStream; 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 { public int init(LittleEndianInputStream leis, long recordSize, int recordFunction) throws IOException {
escapeFunction = EscapeFunction.valueOf(leis.readUShort()); escapeFunction = EscapeFunction.valueOf(leis.readUShort());
byteCount = leis.readUShort(); byteCount = leis.readUShort();
escapeData = new byte[byteCount]; escapeData = IOUtils.toByteArray(leis,byteCount);
leis.read(escapeData);
return 2*LittleEndianConsts.SHORT_SIZE+byteCount; return 2*LittleEndianConsts.SHORT_SIZE+byteCount;
} }

View File

@ -45,7 +45,7 @@ public class HwmfPlaceableHeader {
int y1 = leis.readShort(); int y1 = leis.readShort();
int x2 = leis.readShort(); int x2 = leis.readShort();
int y2 = 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. * Inch (2 bytes): The number of logical units per inch used to represent the image.

View File

@ -18,7 +18,6 @@
package org.apache.poi.hwpf; package org.apache.poi.hwpf;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; 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.EntryUtils;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
/** /**
@ -230,17 +230,14 @@ public final class HWPFDocument extends HWPFDocumentCore
} }
// Grab the table stream. // Grab the table stream.
DocumentEntry tableProps; if (!directory.hasEntry(name)) {
try {
tableProps =
(DocumentEntry)directory.getEntry(name);
} catch(FileNotFoundException fnfe) {
throw new IllegalStateException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)"); throw new IllegalStateException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)");
} }
// read in the table stream. // read in the table stream.
_tableStream = new byte[tableProps.getSize()]; InputStream is = directory.createDocumentInputStream(name);
directory.createDocumentInputStream(name).read(_tableStream); _tableStream = IOUtils.toByteArray(is);
is.close();
_fib.fillVariableFields(_mainStream, _tableStream); _fib.fillVariableFields(_mainStream, _tableStream);

View File

@ -38,6 +38,7 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; 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. * POIFSFileSystem from it, and returns that.
*/ */
public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException { public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException {
// Open a PushbackInputStream, so we can peek at the first few bytes // Open a PushbackInputStream, so we can peek at the first few bytes
PushbackInputStream pis = new PushbackInputStream(istream,6); PushbackInputStream pis = new PushbackInputStream(istream,6);
byte[] first6 = new byte[6]; byte[] first6 = IOUtils.toByteArray(pis, 6);
pis.read(first6);
// Does it start with {\rtf ? If so, it's really RTF // Does it start with {\rtf ? If so, it's really RTF
if(first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r' if(first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r'