public class NPOIFSFileSystem extends BlockStore implements POIFSViewable, Closeable
BlockStore.ChainLoopDetector
Constructor and Description |
---|
NPOIFSFileSystem()
Constructor, intended for writing
|
NPOIFSFileSystem(File file)
Creates a POIFSFileSystem from a File.
|
NPOIFSFileSystem(File file,
boolean readOnly)
Creates a POIFSFileSystem from a File.
|
NPOIFSFileSystem(FileChannel channel)
Creates a POIFSFileSystem from an open FileChannel.
|
NPOIFSFileSystem(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.
|
protected ByteBuffer |
createBlockIfNeeded(int offset)
Load the block at the given offset,
extending the file if needed
|
DirectoryEntry |
createDirectory(String name)
create a new DirectoryEntry in the root directory
|
DocumentEntry |
createDocument(InputStream stream,
String name)
Create a new document to be added to the root directory
|
DocumentEntry |
createDocument(String name,
int size,
POIFSWriterListener writer)
create a new DocumentEntry in the root entry; the data will be
provided later
|
DocumentInputStream |
createDocumentInputStream(String documentName)
open a document in the root entry's list of entries
|
static InputStream |
createNonClosingInputStream(InputStream is)
Convenience method for clients that want to avoid the auto-close behaviour of the constructor.
|
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 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.
|
NPOIFSMiniStore |
getMiniStore()
Returns the MiniStore, which performs a similar low
level function to this, except for the small blocks.
|
protected int |
getNextBlock(int offset)
Works out what block follows the specified one.
|
DirectoryNode |
getRoot()
Get the root entry
|
String |
getShortDescription()
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
POIFSViewable
|
Iterator |
getViewableIterator()
Get an Iterator of objects, some of which may implement
POIFSViewable
|
static boolean |
hasPOIFSHeader(InputStream inp)
Checks that the supplied InputStream (which MUST
support mark and reset, or be a PushbackInputStream)
has a POIFS (OLE2) header at the start of it.
|
static void |
main(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.
|
void |
writeFilesystem()
Write the filesystem out to the open file.
|
void |
writeFilesystem(OutputStream stream)
Write the filesystem out
|
public NPOIFSFileSystem()
public NPOIFSFileSystem(File file) throws IOException
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 dataIOException
- on errors reading, or on invalid datapublic NPOIFSFileSystem(File file, boolean readOnly) throws IOException
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 dataIOException
- on errors reading, or on invalid datapublic NPOIFSFileSystem(FileChannel channel) throws IOException
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 dataIOException
- on errors reading, or on invalid datapublic NPOIFSFileSystem(InputStream stream) throws IOException
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 dataIOException
- on errors reading, or on invalid datapublic static InputStream createNonClosingInputStream(InputStream is)
public static boolean hasPOIFSHeader(InputStream inp) throws IOException
inp
- An InputStream which supports either mark/reset, or is a PushbackInputStreamIOException
protected ByteBuffer getBlockAt(int offset) throws IOException
getBlockAt
in class BlockStore
IOException
protected ByteBuffer createBlockIfNeeded(int offset) throws IOException
createBlockIfNeeded
in class BlockStore
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 IOException
getFreeBlock
in class BlockStore
IOException
protected BlockStore.ChainLoopDetector getChainLoopDetector() throws IOException
BlockStore
getChainLoopDetector
in class BlockStore
IOException
public NPOIFSMiniStore getMiniStore()
public DocumentEntry createDocument(InputStream stream, String name) throws IOException
stream
- the InputStream from which the document's data
will be obtainedname
- the name of the new POIFSDocumentIOException
- on error creating the new POIFSDocumentpublic DocumentEntry createDocument(String name, int size, POIFSWriterListener writer) throws IOException
name
- the name of the new DocumentEntrysize
- the size of the new DocumentEntrywriter
- the writer of the new DocumentEntryIOException
public DirectoryEntry createDirectory(String name) throws IOException
name
- the name of the new DirectoryEntryIOException
- on name duplicationpublic void writeFilesystem() throws IOException
IllegalArgumentException
if opened from an
InputStream
.IOException
- thrown on errors writing to the streampublic void writeFilesystem(OutputStream stream) throws IOException
stream
- the OutputStream to which the filesystem will be
writtenIOException
- thrown on errors writing to the streampublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public static void main(String[] args) throws IOException
args
- names of the files; arg[ 0 ] is the input file,
arg[ 1 ] is the output fileIOException
public DirectoryNode getRoot()
public DocumentInputStream createDocumentInputStream(String documentName) throws IOException
documentName
- the name of the document to be openedIOException
- if the document does not exist or the
name is that of a DirectoryEntrypublic Object[] getViewableArray()
getViewableArray
in interface POIFSViewable
public Iterator getViewableIterator()
getViewableIterator
in interface POIFSViewable
public boolean preferArray()
preferArray
in interface POIFSViewable
public String getShortDescription()
getShortDescription
in interface POIFSViewable
public int getBigBlockSize()
public POIFSBigBlockSize getBigBlockSizeDetails()
protected int getBlockStoreBlockSize()
BlockStore
getBlockStoreBlockSize
in class BlockStore
Copyright © 2020. All rights reserved.