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);
dump(fs, startBlock, "mini-stream", file);
}
}
+
+ fs.close();
}
}
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()));
* 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
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.POIDocument;
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;
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 );
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;
}
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;
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
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;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.PrintStream;
import java.util.Locale;
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;
/**
* @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;
}
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;
/**
* 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)));
}
* Creates an extractor from a given input stream
* @param iStream
*/
+ @SuppressWarnings("resource")
public QuickButCruddyTextExtractor(InputStream iStream) throws IOException {
this(new NPOIFSFileSystem(iStream));
is = iStream;
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();
}
import java.awt.image.BufferedImage;\r
import java.io.IOException;\r
\r
+import org.apache.poi.util.IOUtils;\r
import org.apache.poi.util.LittleEndianConsts;\r
import org.apache.poi.util.LittleEndianInputStream;\r
\r
size += 18+LittleEndianConsts.INT_SIZE;\r
}\r
\r
- int bytes = (((width * bitsPixel + 15) >> 4) << 1) * height;\r
- byte buf[] = new byte[bytes];\r
- leis.read(buf);\r
+ int length = (((width * bitsPixel + 15) >> 4) << 1) * height;\r
+ @SuppressWarnings("unused")\r
+ byte buf[] = IOUtils.toByteArray(leis, length);\r
\r
// TODO: this is not implemented ... please provide a sample, if it\r
// ever happens to you, to come here ...\r
\r
import javax.imageio.ImageIO;\r
\r
-import org.apache.poi.hssf.record.RecordFormatException;\r
+import org.apache.poi.util.IOUtils;\r
import org.apache.poi.util.LittleEndian;\r
import org.apache.poi.util.LittleEndianConsts;\r
import org.apache.poi.util.LittleEndianInputStream;\r
import org.apache.poi.util.POILogFactory;\r
import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.RecordFormatException;\r
\r
/**\r
* The DeviceIndependentBitmap Object defines an image in device-independent bitmap (DIB) format.\r
\r
int fileSize = (headerImageSize < headerSize) ? recordSize : (int)Math.min(introSize+headerImageSize,recordSize);\r
\r
- imageData = new byte[fileSize];\r
leis.reset();\r
- leis.read(imageData, 0, fileSize);\r
+ imageData = IOUtils.toByteArray(leis, fileSize);\r
\r
assert( headerSize != 0x0C || ((((headerWidth * headerPlanes * headerBitCount.flag + 31) & ~31) / 8) * Math.abs(headerHeight)) == headerImageSize);\r
\r
\r
import org.apache.poi.hwmf.draw.HwmfGraphics;\r
import org.apache.poi.util.HexDump;\r
+import org.apache.poi.util.IOUtils;\r
import org.apache.poi.util.LittleEndianConsts;\r
import org.apache.poi.util.LittleEndianInputStream;\r
\r
public int init(LittleEndianInputStream leis, long recordSize, int recordFunction) throws IOException {\r
escapeFunction = EscapeFunction.valueOf(leis.readUShort());\r
byteCount = leis.readUShort();\r
- escapeData = new byte[byteCount];\r
- leis.read(escapeData);\r
+ escapeData = IOUtils.toByteArray(leis,byteCount);\r
+\r
return 2*LittleEndianConsts.SHORT_SIZE+byteCount;\r
}\r
\r
int y1 = leis.readShort();\r
int x2 = leis.readShort();\r
int y2 = leis.readShort();\r
- bounds = new Rectangle2D.Double(x1, y1, x2-x1, y2-y1);\r
+ bounds = new Rectangle2D.Double(Math.min(x1,x2), Math.min(y1,y2), Math.abs(x2-x1), Math.abs(y2-y1));\r
\r
/*\r
* Inch (2 bytes): The number of logical units per inch used to represent the image.\r
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;
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;
/**
}
// 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);
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;
}
/**
- * 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'