Class POIFSFileSystem
- java.lang.Object
-
- org.apache.poi.poifs.filesystem.BlockStore
-
- org.apache.poi.poifs.filesystem.POIFSFileSystem
-
- All Implemented Interfaces:
Closeable,AutoCloseable,POIFSViewable
- Direct Known Subclasses:
TempFilePOIFSFileSystem
public class POIFSFileSystem extends BlockStore implements POIFSViewable, 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.poi.poifs.filesystem.BlockStore
BlockStore.ChainLoopDetector
-
-
Field Summary
Fields Modifier and Type Field Description protected DataSource_data
-
Constructor Summary
Constructors Constructor Description POIFSFileSystem()Constructor, intended for writingPOIFSFileSystem(File file)Creates a POIFSFileSystem from aFile.POIFSFileSystem(File file, boolean readOnly)Creates a POIFSFileSystem from aFile.POIFSFileSystem(InputStream stream)Create a POIFSFileSystem from anInputStream.POIFSFileSystem(FileChannel channel)Creates a POIFSFileSystem from an openFileChannel.POIFSFileSystem(FileChannel channel, boolean readOnly)Creates a POIFSFileSystem from an openFileChannel.POIFSFileSystem(FileChannel channel, boolean readOnly, boolean closeChannel)Creates a POIFSFileSystem from an openFileChannel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the FileSystem, freeing any underlying files, streams and buffers.static POIFSFileSystemcreate(File file)Creates a new POIFSFileSystem in a newFile.protected ByteBuffercreateBlockIfNeeded(int offset)Load the block at the given offset, extending the file if neededDirectoryEntrycreateDirectory(String name)create a new DirectoryEntry in the root directoryDocumentEntrycreateDocument(InputStream stream, String name)Create a new document to be added to the root directoryDocumentEntrycreateDocument(String name, int size, POIFSWriterListener writer)create a new DocumentEntry in the root entry; the data will be provided laterDocumentInputStreamcreateDocumentInputStream(String documentName)open a document in the root entry's list of entriesprotected voidcreateNewDataSource()DocumentEntrycreateOrUpdateDocument(InputStream stream, String name)Set the contents of a document in the root directory, creating if needed, otherwise updatingprotected BATBlock.BATBlockAndIndexgetBATBlockAndIndex(int offset)Returns the BATBlock that handles the specified offset, and the relative index within itintgetBigBlockSize()POIFSBigBlockSizegetBigBlockSizeDetails()protected ByteBuffergetBlockAt(int offset)Load the block at the given offset.protected intgetBlockStoreBlockSize()Returns the size of the blocks managed through the block store.protected BlockStore.ChainLoopDetectorgetChainLoopDetector()Creates a Detector for loops in the chainprotected intgetFreeBlock()Finds a free block, and returns its offset.HeaderBlockgetHeaderBlock()static intgetMaxRecordLength()protected intgetNextBlock(int offset)Works out what block follows the specified one.PropertyTablegetPropertyTable()DirectoryNodegetRoot()Get the root entryStringgetShortDescription()Provides a short description of the object, to be used when a POIFSViewable object has not provided its contents.Object[]getViewableArray()Get an array of objects, some of which may implement POIFSViewableIterator<Object>getViewableIterator()Get an Iterator of objects, some of which may implement POIFSViewablebooleanisInPlaceWriteable()Does the filesystem support an in-place write viawriteFilesystem()? If false, only writing out to a brand new file viawriteFilesystem(OutputStream)is supported.static voidmain(String[] args)read in a file and write it back out againbooleanpreferArray()Give viewers a hint as to whether to call getViewableArray or getViewableIteratorprotected voidreleaseBuffer(ByteBuffer buffer)Releases a mmap-ed buffer, which you are sure won't be used againstatic voidsetMaxRecordLength(int length)protected voidsetNextBlock(int offset, int nextBlock)Changes the record of what block follows the specified one.protected longsize()voidwriteFilesystem()Write the filesystem out to the open file.voidwriteFilesystem(OutputStream stream)Write the filesystem out
-
-
-
Field Detail
-
_data
protected DataSource _data
-
-
Constructor Detail
-
POIFSFileSystem
public POIFSFileSystem()
Constructor, intended for writing
-
POIFSFileSystem
public POIFSFileSystem(File file) throws IOException
Creates a POIFSFileSystem from a
File. This uses less memory than creating from anInputStream. The File will be opened read-onlyNote 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.- Parameters:
file- the File from which to read the data- Throws:
IOException- on errors reading, or on invalid data
-
POIFSFileSystem
public POIFSFileSystem(File file, boolean readOnly) throws IOException
Creates a POIFSFileSystem from a
File. This uses less memory than creating from anInputStream.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.- Parameters:
file- the File from which to read or read/write the datareadOnly- whether the POIFileSystem will only be used in read-only mode- Throws:
IOException- on errors reading, or on invalid data
-
POIFSFileSystem
public POIFSFileSystem(FileChannel channel) throws IOException
Creates a POIFSFileSystem from an open
FileChannel. This uses less memory than creating from anInputStream. 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. For legacy reasons, the channel is not closed if there is an error creating the POIFSFileSystem.- Parameters:
channel- the FileChannel from which to read the data- Throws:
IOException- on errors reading, or on invalid data- See Also:
this constructor gives more control over whether to close the provided channel
-
POIFSFileSystem
public POIFSFileSystem(FileChannel channel, boolean readOnly) throws IOException
Creates a POIFSFileSystem from an open
FileChannel. This uses less memory than creating from anInputStream.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. For legacy reasons, the channel is not closed if there is an error creating the POIFSFileSystem.- Parameters:
channel- the FileChannel from which to read or read/write the datareadOnly- whether the POIFileSystem will only be used in read-only mode- Throws:
IOException- on errors reading, or on invalid data- See Also:
this constructor gives more control over whether to close the provided channel
-
POIFSFileSystem
public POIFSFileSystem(FileChannel channel, boolean readOnly, boolean closeChannel) throws IOException
Creates a POIFSFileSystem from an open
FileChannel. This uses less memory than creating from anInputStream.Note that with this constructor, you will need to call
close()when you're done to have the underlying resources closed. ThecloseChannelparameter controls whether the provided channel is closed.- Parameters:
channel- the FileChannel from which to read or read/write the datareadOnly- whether the POIFileSystem will only be used in read-only modecloseChannel- whether the provided FileChannel should be closed whenclose()is called, or when this constructor throws an exception- Throws:
IOException- on errors reading, or on invalid data- Since:
- POI 5.1.0
-
POIFSFileSystem
public POIFSFileSystem(InputStream stream) throws IOException
Create a POIFSFileSystem from anInputStream. Normally the stream is read until EOF. The stream is always closed.Some streams are usable after reaching EOF (typically those that return
trueformarkSupported()). 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 theclose()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 ofByteArrayInputStreamfor which theclose()method does nothing.ByteArrayInputStream bais = ... HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() ! bais.reset(); // no problem doSomethingElse(bais);
- Parameters:
stream- the InputStream from which to read the data- Throws:
IOException- on errors reading, or on invalid data
-
-
Method Detail
-
setMaxRecordLength
public static void setMaxRecordLength(int length)
- Parameters:
length- the max record length allowed for POIFSFileSystem
-
getMaxRecordLength
public static int getMaxRecordLength()
- Returns:
- the max record length allowed for POIFSFileSystem
-
createNewDataSource
protected void createNewDataSource()
-
getBlockAt
protected ByteBuffer getBlockAt(int offset) throws IOException
Load the block at the given offset.- Specified by:
getBlockAtin classBlockStore- Throws:
IOException
-
createBlockIfNeeded
protected ByteBuffer createBlockIfNeeded(int offset) throws IOException
Load the block at the given offset, extending the file if needed- Specified by:
createBlockIfNeededin classBlockStore- Throws:
IOException
-
getBATBlockAndIndex
protected BATBlock.BATBlockAndIndex getBATBlockAndIndex(int offset)
Returns the BATBlock that handles the specified offset, and the relative index within it- Specified by:
getBATBlockAndIndexin classBlockStore
-
getNextBlock
protected int getNextBlock(int offset)
Works out what block follows the specified one.- Specified by:
getNextBlockin classBlockStore
-
setNextBlock
protected void setNextBlock(int offset, int nextBlock)Changes the record of what block follows the specified one.- Specified by:
setNextBlockin classBlockStore
-
getFreeBlock
protected int getFreeBlock() throws IOExceptionFinds a free block, and returns its offset. This method will extend the file if needed, and if doing so, allocate new FAT blocks to address the extra space.- Specified by:
getFreeBlockin classBlockStore- Throws:
IOException
-
size
protected long size() throws IOException- Throws:
IOException
-
getChainLoopDetector
protected BlockStore.ChainLoopDetector getChainLoopDetector() throws IOException
Description copied from class:BlockStoreCreates a Detector for loops in the chain- Specified by:
getChainLoopDetectorin classBlockStore- Throws:
IOException
-
createDocument
public DocumentEntry createDocument(InputStream stream, String name) throws IOException
Create a new document to be added to the root directory- Parameters:
stream- the InputStream from which the document's data will be obtainedname- the name of the new POIFSDocument- Returns:
- the new DocumentEntry
- Throws:
IOException- on error creating the new POIFSDocument
-
createDocument
public DocumentEntry createDocument(String name, int size, POIFSWriterListener writer) throws IOException
create a new DocumentEntry in the root entry; the data will be provided later- Parameters:
name- the name of the new DocumentEntrysize- the size of the new DocumentEntrywriter- the writer of the new DocumentEntry- Returns:
- the new DocumentEntry
- Throws:
IOException- if the writer exceeds the given size
-
createDirectory
public DirectoryEntry createDirectory(String name) throws IOException
create a new DirectoryEntry in the root directory- Parameters:
name- the name of the new DirectoryEntry- Returns:
- the new DirectoryEntry
- Throws:
IOException- on name duplication
-
createOrUpdateDocument
public DocumentEntry createOrUpdateDocument(InputStream stream, String name) throws IOException
Set the contents of a document in the root directory, creating if needed, otherwise updating- Parameters:
stream- the InputStream from which the document's data will be obtainedname- the name of the new or existing POIFSDocument- Returns:
- the new or updated DocumentEntry
- Throws:
IOException- on error populating the POIFSDocument
-
isInPlaceWriteable
public boolean isInPlaceWriteable()
Does the filesystem support an in-place write viawriteFilesystem()? If false, only writing out to a brand new file viawriteFilesystem(OutputStream)is supported.
-
writeFilesystem
public void writeFilesystem() throws IOExceptionWrite the filesystem out to the open file. Will thrown anIllegalArgumentExceptionif opened from anInputStream.- Throws:
IOException- thrown on errors writing to the stream
-
writeFilesystem
public void writeFilesystem(OutputStream stream) throws IOException
Write the filesystem out- Parameters:
stream- the OutputStream to which the filesystem will be written- Throws:
IOException- thrown on errors writing to the stream
-
close
public void close() throws IOExceptionCloses the FileSystem, freeing any underlying files, streams and buffers. After this, you will be unable to read or write from the FileSystem.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
main
public static void main(String[] args) throws IOException
read in a file and write it back out again- Parameters:
args- names of the files; arg[ 0 ] is the input file, arg[ 1 ] is the output file- Throws:
IOException
-
getRoot
public DirectoryNode getRoot()
Get the root entry- Returns:
- the root entry
-
createDocumentInputStream
public DocumentInputStream createDocumentInputStream(String documentName) throws IOException
open a document in the root entry's list of entries- Parameters:
documentName- the name of the document to be opened- Returns:
- a newly opened DocumentInputStream
- Throws:
IOException- if the document does not exist or the name is that of a DirectoryEntry
-
getViewableArray
public Object[] getViewableArray()
Get an array of objects, some of which may implement POIFSViewable- Specified by:
getViewableArrayin interfacePOIFSViewable- Returns:
- an array of Object; may not be null, but may be empty
-
getViewableIterator
public Iterator<Object> getViewableIterator()
Get an Iterator of objects, some of which may implement POIFSViewable- Specified by:
getViewableIteratorin interfacePOIFSViewable- Returns:
- an Iterator; may not be null, but may have an empty back end store
-
preferArray
public boolean preferArray()
Give viewers a hint as to whether to call getViewableArray or getViewableIterator- Specified by:
preferArrayin interfacePOIFSViewable- Returns:
- true if a viewer should call getViewableArray, false if a viewer should call getViewableIterator
-
getShortDescription
public String getShortDescription()
Provides a short description of the object, to be used when a POIFSViewable object has not provided its contents.- Specified by:
getShortDescriptionin interfacePOIFSViewable- Returns:
- short description
-
getBigBlockSize
public int getBigBlockSize()
- Returns:
- The Big Block size, normally 512 bytes, sometimes 4096 bytes
-
getBigBlockSizeDetails
public POIFSBigBlockSize getBigBlockSizeDetails()
- Returns:
- The Big Block size, normally 512 bytes, sometimes 4096 bytes
-
create
public static POIFSFileSystem create(File file) throws IOException
Creates a new POIFSFileSystem in a newFile. UsePOIFSFileSystem(File)to open an existing File, this should only be used to create a new empty filesystem.- Parameters:
file- The file to create and open- Returns:
- The created and opened POIFSFileSystem
- Throws:
IOException
-
getBlockStoreBlockSize
protected int getBlockStoreBlockSize()
Description copied from class:BlockStoreReturns the size of the blocks managed through the block store.- Specified by:
getBlockStoreBlockSizein classBlockStore
-
getPropertyTable
@Internal public PropertyTable getPropertyTable()
-
getHeaderBlock
@Internal public HeaderBlock getHeaderBlock()
-
releaseBuffer
protected void releaseBuffer(ByteBuffer buffer)
Description copied from class:BlockStoreReleases a mmap-ed buffer, which you are sure won't be used again- Specified by:
releaseBufferin classBlockStore- Parameters:
buffer- the buffer
-
-