public class POIFSFileSystem extends BlockStore implements POIFSViewable, java.io.Closeable
This is the main class of the POIFS system; it manages the entire life cycle of the filesystem.
This is the new NIO version, which uses less memory
BlockStore.ChainLoopDetector
Modifier and Type | Field and Description |
---|---|
protected DataSource |
_data |
Constructor and Description |
---|
POIFSFileSystem()
Constructor, intended for writing
|
POIFSFileSystem(java.io.File file)
Creates a POIFSFileSystem from a File.
|
POIFSFileSystem(java.io.File file,
boolean readOnly)
Creates a POIFSFileSystem from a File.
|
POIFSFileSystem(java.nio.channels.FileChannel channel)
Creates a POIFSFileSystem from an open FileChannel.
|
POIFSFileSystem(java.nio.channels.FileChannel channel,
boolean readOnly)
Creates a POIFSFileSystem from an open FileChannel.
|
POIFSFileSystem(java.io.InputStream stream)
Create a POIFSFileSystem from an InputStream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the FileSystem, freeing any underlying files, streams
and buffers.
|
static POIFSFileSystem |
create(java.io.File file)
Creates a new
POIFSFileSystem in a new File . |
protected java.nio.ByteBuffer |
createBlockIfNeeded(int offset)
Load the block at the given offset,
extending the file if needed
|
DirectoryEntry |
createDirectory(java.lang.String name)
create a new DirectoryEntry in the root directory
|
DocumentEntry |
createDocument(java.io.InputStream stream,
java.lang.String name)
Create a new document to be added to the root directory
|
DocumentEntry |
createDocument(java.lang.String name,
int size,
POIFSWriterListener writer)
create a new DocumentEntry in the root entry; the data will be
provided later
|
DocumentInputStream |
createDocumentInputStream(java.lang.String documentName)
open a document in the root entry's list of entries
|
protected void |
createNewDataSource() |
DocumentEntry |
createOrUpdateDocument(java.io.InputStream stream,
java.lang.String name)
Set the contents of a document in the root directory,
creating if needed, otherwise updating
|
protected BATBlock.BATBlockAndIndex |
getBATBlockAndIndex(int offset)
Returns the BATBlock that handles the specified offset,
and the relative index within it
|
int |
getBigBlockSize() |
POIFSBigBlockSize |
getBigBlockSizeDetails() |
protected java.nio.ByteBuffer |
getBlockAt(int offset)
Load the block at the given offset.
|
protected int |
getBlockStoreBlockSize()
Returns the size of the blocks managed through the block store.
|
protected BlockStore.ChainLoopDetector |
getChainLoopDetector()
Creates a Detector for loops in the chain
|
protected int |
getFreeBlock()
Finds a free block, and returns its offset.
|
HeaderBlock |
getHeaderBlock() |
protected int |
getNextBlock(int offset)
Works out what block follows the specified one.
|
PropertyTable |
getPropertyTable() |
DirectoryNode |
getRoot()
Get the root entry
|
java.lang.String |
getShortDescription()
Provides a short description of the object, to be used when a
POIFSViewable object has not provided its contents.
|
java.lang.Object[] |
getViewableArray()
Get an array of objects, some of which may implement
POIFSViewable
|
java.util.Iterator<java.lang.Object> |
getViewableIterator()
Get an Iterator of objects, some of which may implement
POIFSViewable
|
boolean |
isInPlaceWriteable()
Does the filesystem support an in-place write via
writeFilesystem() ? If false, only writing out to
a brand new file via writeFilesystem(OutputStream)
is supported. |
static void |
main(java.lang.String[] args)
read in a file and write it back out again
|
boolean |
preferArray()
Give viewers a hint as to whether to call getViewableArray or
getViewableIterator
|
protected void |
setNextBlock(int offset,
int nextBlock)
Changes the record of what block follows the specified one.
|
protected long |
size() |
void |
writeFilesystem()
Write the filesystem out to the open file.
|
void |
writeFilesystem(java.io.OutputStream stream)
Write the filesystem out
|
protected DataSource _data
public POIFSFileSystem()
public POIFSFileSystem(java.io.File file) throws java.io.IOException
Creates a POIFSFileSystem from a File. This uses less memory than creating from an InputStream. The File will be opened read-only
Note that with this constructor, you will need to call 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.
file
- the File from which to read the datajava.io.IOException
- on errors reading, or on invalid datapublic POIFSFileSystem(java.io.File file, boolean readOnly) throws java.io.IOException
Creates a POIFSFileSystem from a File. This uses less memory than creating from an InputStream.
Note that with this constructor, you will need to call 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.
file
- the File from which to read or read/write the datareadOnly
- whether the POIFileSystem will only be used in read-only modejava.io.IOException
- on errors reading, or on invalid datapublic POIFSFileSystem(java.nio.channels.FileChannel channel) throws java.io.IOException
Creates a POIFSFileSystem from an open FileChannel. This uses less memory than creating from an InputStream. The stream will be used in read-only mode.
Note that with this constructor, you will need to call 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.
channel
- the FileChannel from which to read the datajava.io.IOException
- on errors reading, or on invalid datapublic POIFSFileSystem(java.nio.channels.FileChannel channel, boolean readOnly) throws java.io.IOException
Creates a POIFSFileSystem from an open FileChannel. This uses less memory than creating from an InputStream.
Note that with this constructor, you will need to call 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.
channel
- the FileChannel from which to read or read/write the datareadOnly
- whether the POIFileSystem will only be used in read-only modejava.io.IOException
- on errors reading, or on invalid datapublic POIFSFileSystem(java.io.InputStream stream) throws java.io.IOException
Some streams are usable after reaching EOF (typically those that return true
for markSupported()). In the unlikely case that the caller has such a stream
and needs to use it after this constructor completes, a work around is to wrap the
stream in order to trap the close() call. A convenience method (
createNonClosingInputStream()) has been provided for this purpose:
InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is); HSSFWorkbook wb = new HSSFWorkbook(wrappedStream); is.reset(); doSomethingElse(is);Note also the special case of ByteArrayInputStream for which the close() method does nothing.
ByteArrayInputStream bais = ... HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() ! bais.reset(); // no problem doSomethingElse(bais);
stream
- the InputStream from which to read the datajava.io.IOException
- on errors reading, or on invalid dataprotected void createNewDataSource()
protected java.nio.ByteBuffer getBlockAt(int offset) throws java.io.IOException
getBlockAt
in class BlockStore
java.io.IOException
protected java.nio.ByteBuffer createBlockIfNeeded(int offset) throws java.io.IOException
createBlockIfNeeded
in class BlockStore
java.io.IOException
protected BATBlock.BATBlockAndIndex getBATBlockAndIndex(int offset)
getBATBlockAndIndex
in class BlockStore
protected int getNextBlock(int offset)
getNextBlock
in class BlockStore
protected void setNextBlock(int offset, int nextBlock)
setNextBlock
in class BlockStore
protected int getFreeBlock() throws java.io.IOException
getFreeBlock
in class BlockStore
java.io.IOException
protected long size() throws java.io.IOException
java.io.IOException
protected BlockStore.ChainLoopDetector getChainLoopDetector() throws java.io.IOException
BlockStore
getChainLoopDetector
in class BlockStore
java.io.IOException
public DocumentEntry createDocument(java.io.InputStream stream, java.lang.String name) throws java.io.IOException
stream
- the InputStream from which the document's data
will be obtainedname
- the name of the new POIFSDocumentjava.io.IOException
- on error creating the new POIFSDocumentpublic DocumentEntry createDocument(java.lang.String name, int size, POIFSWriterListener writer) throws java.io.IOException
name
- the name of the new DocumentEntrysize
- the size of the new DocumentEntrywriter
- the writer of the new DocumentEntryjava.io.IOException
- if the writer exceeds the given sizepublic DirectoryEntry createDirectory(java.lang.String name) throws java.io.IOException
name
- the name of the new DirectoryEntryjava.io.IOException
- on name duplicationpublic DocumentEntry createOrUpdateDocument(java.io.InputStream stream, java.lang.String name) throws java.io.IOException
stream
- the InputStream from which the document's data
will be obtainedname
- the name of the new or existing POIFSDocumentjava.io.IOException
- on error populating the POIFSDocumentpublic boolean isInPlaceWriteable()
writeFilesystem()
? If false, only writing out to
a brand new file via writeFilesystem(OutputStream)
is supported.public void writeFilesystem() throws java.io.IOException
IllegalArgumentException
if opened from an
InputStream
.java.io.IOException
- thrown on errors writing to the streampublic void writeFilesystem(java.io.OutputStream stream) throws java.io.IOException
stream
- the OutputStream to which the filesystem will be
writtenjava.io.IOException
- thrown on errors writing to the streampublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public static void main(java.lang.String[] args) throws java.io.IOException
args
- names of the files; arg[ 0 ] is the input file,
arg[ 1 ] is the output filejava.io.IOException
public DirectoryNode getRoot()
public DocumentInputStream createDocumentInputStream(java.lang.String documentName) throws java.io.IOException
documentName
- the name of the document to be openedjava.io.IOException
- if the document does not exist or the
name is that of a DirectoryEntrypublic java.lang.Object[] getViewableArray()
getViewableArray
in interface POIFSViewable
public java.util.Iterator<java.lang.Object> getViewableIterator()
getViewableIterator
in interface POIFSViewable
public boolean preferArray()
preferArray
in interface POIFSViewable
public java.lang.String getShortDescription()
getShortDescription
in interface POIFSViewable
public int getBigBlockSize()
public POIFSBigBlockSize getBigBlockSizeDetails()
public static POIFSFileSystem create(java.io.File file) throws java.io.IOException
POIFSFileSystem
in a new File
.
Use POIFSFileSystem(File)
to open an existing File,
this should only be used to create a new empty filesystem.file
- The file to create and openPOIFSFileSystem
java.io.IOException
protected int getBlockStoreBlockSize()
BlockStore
getBlockStoreBlockSize
in class BlockStore
@Internal public PropertyTable getPropertyTable()
@Internal public HeaderBlock getHeaderBlock()
Copyright 2020 The Apache Software Foundation or its licensors, as applicable.