diff options
author | Nick Burch <nick@apache.org> | 2011-01-05 09:38:10 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2011-01-05 09:38:10 +0000 |
commit | 5ce8c4e6b1e14b5b843663470acae854a0501253 (patch) | |
tree | 9675c6c39750d5820c5358770466d2d9d0473c5a /src/java | |
parent | 894d3edd241b01372fbbefbcb15fc3aff521cd1c (diff) | |
download | poi-5ce8c4e6b1e14b5b843663470acae854a0501253.tar.gz poi-5ce8c4e6b1e14b5b843663470acae854a0501253.zip |
Port more NIO changes over from trunk
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/NIO_32_BRANCH@1055375 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java')
3 files changed, 85 insertions, 18 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 3a7ca03fc2..95a567bb5b 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -249,7 +249,25 @@ public class HSSFWorkbook extends POIDocument public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) throws IOException { - super(directory, fs); + this(directory, preserveNodes); + } + /** + * given a POI POIFSFileSystem object, and a specific directory + * within it, read in its Workbook and populate the high and + * low level models. If you're reading in a workbook...start here. + * + * @param directory the POI filesystem directory to process from + * @param preserveNodes whether to preseve other nodes, such as + * macros. This takes more memory, so only say yes if you + * need to. If set, will store all of the POIFSFileSystem + * in memory + * @see org.apache.poi.poifs.filesystem.POIFSFileSystem + * @exception IOException if the stream cannot be read + */ + public HSSFWorkbook(DirectoryNode directory, boolean preserveNodes) + throws IOException + { + super(directory); String workbookName = getWorkbookDirEntryName(directory); this.preserveNodes = preserveNodes; diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java index 09536d4ade..3a2c286023 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java @@ -51,7 +51,7 @@ public final class NPOIFSDocument implements POIFSViewable { this._property = property; this._filesystem = filesystem; - if(property.getSize() <= POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { + if(property.getSize() < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { _stream = new NPOIFSStream(_filesystem.getMiniStore(), property.getStartBlock()); _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize(); } else { diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index e671d870bb..8a7b683315 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -107,7 +107,7 @@ public class NPOIFSFileSystem extends BlockStore /** * Creates a POIFSFileSystem from a <tt>File</tt>. This uses less memory than - * creating from an <tt>InputStream</tt>. + * creating from an <tt>InputStream</tt>. The File will be opened read-only * * Note that with this constructor, you will need to call {@link #close()} * when you're done to have the underlying file closed, as the file is @@ -120,21 +120,70 @@ public class NPOIFSFileSystem extends BlockStore public NPOIFSFileSystem(File file) throws IOException { + this(file, true); + } + + /** + * Creates a POIFSFileSystem from a <tt>File</tt>. This uses less memory than + * creating from an <tt>InputStream</tt>. + * + * Note that with this constructor, you will need to call {@link #close()} + * when you're done to have the underlying file closed, as the file is + * kept open during normal operation to read the data out. + * + * @param file the File from which to read the data + * + * @exception IOException on errors reading, or on invalid data + */ + public NPOIFSFileSystem(File file, boolean readOnly) + throws IOException + { + this( + (new RandomAccessFile(file, readOnly? "r" : "rw")).getChannel(), + true + ); + } + + /** + * Creates a POIFSFileSystem from an open <tt>FileChannel</tt>. This uses + * less memory than creating from an <tt>InputStream</tt>. + * + * Note that with this constructor, you will need to call {@link #close()} + * when you're done to have the underlying Channel closed, as the channel is + * kept open during normal operation to read the data out. + * + * @param channel the FileChannel from which to read the data + * + * @exception IOException on errors reading, or on invalid data + */ + public NPOIFSFileSystem(FileChannel channel) + throws IOException + { + this(channel, false); + } + + private NPOIFSFileSystem(FileChannel channel, boolean closeChannelOnError) + throws IOException + { this(); - - // Open the underlying channel - FileChannel channel = (new RandomAccessFile(file, "r")).getChannel(); - - // Get the header - ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE); - IOUtils.readFully(channel, headerBuffer); - - // Have the header processed - _header = new HeaderBlock(headerBuffer); - - // Now process the various entries - _data = new FileBackedDataSource(channel); - readCoreContents(); + + try { + // Get the header + ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE); + IOUtils.readFully(channel, headerBuffer); + + // Have the header processed + _header = new HeaderBlock(headerBuffer); + + // Now process the various entries + _data = new FileBackedDataSource(channel); + readCoreContents(); + } catch(IOException e) { + if(closeChannelOnError) { + channel.close(); + } + throw e; + } } /** @@ -435,7 +484,7 @@ public class NPOIFSFileSystem extends BlockStore // Oh joy, we need a new XBAT too... xbat = createBAT(offset+1, false); xbat.setValueAt(0, offset); - bat.setValueAt(offset+1, POIFSConstants.DIFAT_SECTOR_BLOCK); + bat.setValueAt(1, POIFSConstants.DIFAT_SECTOR_BLOCK); // Will go one place higher as XBAT added in offset++; |