Class OPOIFSFileSystem
java.lang.Object
org.docx4j.org.apache.poi.poifs.filesystem.OPOIFSFileSystem
- All Implemented Interfaces:
POIFSViewable
This is the main class of the POIFS system; it manages the entire life cycle of the filesystem.
This is the older version, which uses more memory, and doesn't support in-place writes.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor, intended for writingOPOIFSFileSystem(InputStream stream) Create a OPOIFSFileSystem from an InputStream. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcloseInputStream(InputStream stream, boolean success) createDirectory(String name) create a new DirectoryEntry in the root directorycreateDocument(InputStream stream, String name) Create a new document to be added to the root directorycreateDocument(String name, int size, POIFSWriterListener writer) create a new DocumentEntry in the root entry; the data will be provided latercreateDocumentInputStream(String documentName) open a document in the root entry's list of entriesstatic InputStreamConvenience method for clients that want to avoid the auto-close behaviour of the constructor.intgetRoot()get the root entryProvides a short description of the object, to be used when a POIFSViewable object has not provided its contents.Object[]Get an array of objects, some of which may implement POIFSViewableGet an Iterator of objects, some of which may implement POIFSViewablestatic booleanhasPOIFSHeader(byte[] header8Bytes) Checks if the supplied first 8 bytes of a stream / file has a POIFS (OLE2) header.static booleanChecks 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 voidread in a file and write it back out againbooleanGive viewers a hint as to whether to call getViewableArray or getViewableIteratorvoidwriteFilesystem(OutputStream stream) Write the filesystem out
-
Constructor Details
-
OPOIFSFileSystem
public OPOIFSFileSystem()Constructor, intended for writing -
OPOIFSFileSystem
Create a OPOIFSFileSystem from an InputStream. Normally the stream is read until EOF. The stream is always closed. Some streams are usable after reaching EOF (typically those that returntruefor 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 = OPOIFSFileSystem.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);
- Parameters:
stream- the InputStream from which to read the data- Throws:
IOException- on errors reading, or on invalid data
-
-
Method Details
-
createNonClosingInputStream
Convenience method for clients that want to avoid the auto-close behaviour of the constructor. -
closeInputStream
- Parameters:
stream- the stream to be closedsuccess-falseif an exception is currently being thrown in the calling method
-
hasPOIFSHeader
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. If your InputStream does not support mark / reset, then wrap it in a PushBackInputStream, then be sure to always use that, and not the original!- Parameters:
inp- An InputStream which supports either mark/reset, or is a PushbackInputStream- Throws:
IOException
-
hasPOIFSHeader
public static boolean hasPOIFSHeader(byte[] header8Bytes) Checks if the supplied first 8 bytes of a stream / file has a POIFS (OLE2) header. -
createDocument
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
-
createDirectory
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
-
writeFilesystem
Write the filesystem out- Parameters:
stream- the OutputStream to which the filesystem will be written- Throws:
IOException- thrown on errors writing to the stream
-
main
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
get the root entry- Returns:
- the root entry
-
createDocumentInputStream
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
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
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
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
- Returns:
- The Big Block size, normally 512 bytes, sometimes 4096 bytes
-