private static POIFSDocument getDocument(DocumentEntry document) throws IOException { if (!(document instanceof DocumentNode)) { throw new IOException("Cannot open internal document storage, " + document + " not a Document Node"); } return new POIFSDocument((DocumentNode)document); }
/** * add a new POIFSDocument to the FileSytem * * @param document the POIFSDocument being added */ void addDocument(final POIFSDocument document) { _property_table.addProperty(document.getDocumentProperty()); }
Iterator<ByteBuffer> getBlockIterator() { return (getSize() > 0 ? _stream : Collections.<ByteBuffer>emptyList()).iterator(); }
DocumentOutputStream(POIFSDocument document, long limit) throws IOException { _document = document; _document.free(); _property = document.getDocumentProperty(); _limit = limit; }
/** * Create an InputStream from the specified Document * * @param document the Document to be read */ public DocumentInputStream(POIFSDocument document) { _current_offset = 0; _current_block_count = 0; _marked_offset = 0; _marked_offset_count = 0; _document_size = document.getSize(); _closed = false; _document = document; _data = _document.getBlockIterator(); }
/** * Set the contents of a document, creating if needed, * otherwise updating. Returns the created / updated DocumentEntry * * @param name the name of the new or existing DocumentEntry * @param stream the InputStream from which to populate the DocumentEntry * * @return the new or updated DocumentEntry * * @exception IOException if the document can't be created or its content be replaced */ @SuppressWarnings("WeakerAccess") public DocumentEntry createOrUpdateDocument(final String name, final InputStream stream) throws IOException { if (! hasEntry(name)) { return createDocument(name, stream); } else { DocumentNode existing = (DocumentNode)getEntry(name); POIFSDocument nDoc = new POIFSDocument(existing); nDoc.replaceContents(stream); return existing; } }
/** * remove an entry * * @param entry to be removed */ void remove(EntryNode entry) throws IOException { // If it's a document, free the blocks if (entry instanceof DocumentEntry) { POIFSDocument doc = new POIFSDocument((DocumentProperty)entry.getProperty(), this); doc.free(); } // Now zap it from the properties list _property_table.removeProperty(entry.getProperty()); }
/** * Create an InputStream from the specified DocumentEntry * * @param document the DocumentEntry to be read * * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has * been deleted?) */ public DocumentInputStream(DocumentEntry document) throws IOException { if (!(document instanceof DocumentNode)) { throw new IOException("Cannot open internal document storage, " + document + " not a Document Node"); } _current_offset = 0; _current_block_count = 0; _marked_offset = 0; _marked_offset_count = 0; _document_size = document.getSize(); _closed = false; // can't be asserted ... see bug 61300 // assert (_document_size >= 0) : "Document size can't be < 0"; DocumentNode doc = (DocumentNode)document; DocumentProperty property = (DocumentProperty)doc.getProperty(); _document = new POIFSDocument( property, ((DirectoryNode)doc.getParent()).getNFileSystem() ); _data = _document.getBlockIterator(); }
/** * Constructor from small blocks * * @param name the name of the POIFSDocument * @param blocks the small blocks making up the POIFSDocument * @param length the actual length of the POIFSDocument */ public POIFSDocument(String name, POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks, int length) throws IOException { _size = length; _bigBigBlockSize = bigBlockSize; _property = new DocumentProperty(name, _size); _property.setDocument(this); if (Property.isSmall(_size)) { _big_store = new BigBlockStore(bigBlockSize,EMPTY_BIG_BLOCK_ARRAY); _small_store = new SmallBlockStore(bigBlockSize,convertRawBlocksToSmallBlocks(blocks)); } else { _big_store = new BigBlockStore(bigBlockSize,convertRawBlocksToBigBlocks(blocks)); _small_store = new SmallBlockStore(bigBlockSize,EMPTY_SMALL_BLOCK_ARRAY); } } public POIFSDocument(String name, ListManagedBlock[] blocks, int length) throws IOException {
BlockWritable[] blocks = doc.getSmallBlocks(); doc.setStartBlock(_sbat.allocateSpace(blocks.length)); for (int j = 0; j < blocks.length; j++) doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
@Override public Iterator<ByteBuffer> iterator() { return getBlockIterator(); }
private DataInputBlock getDataInputBlock(int offset) { return _document.getDataInputBlock(offset); }
@Override public void write(byte[] b, int off, int len) throws IOException { if (_closed) { throw new IOException("cannot perform requested operation on a closed stream"); } if (_limit > -1 && (size() + len) > _limit) { throw new IOException("tried to write too much data"); } if (_buffer != null) { _buffer.write(b, off, len); checkBufferSize(); } else { if (_stream == null) { _stream = new POIFSStream(_document.getFileSystem()); _stream_output = _stream.getOutputStream(); } _stream_output.write(b, off, len); _document_size += len; } }
/** * Constructor from large blocks * * @param name the name of the POIFSDocument * @param blocks the big blocks making up the POIFSDocument * @param length the actual length of the POIFSDocument */ public POIFSDocument(String name, RawDataBlock[] blocks, int length) throws IOException { _size = length; if(blocks.length == 0) { _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS; } else { _bigBigBlockSize = (blocks[0].getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ? POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS : POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS ); } _big_store = new BigBlockStore(_bigBigBlockSize, convertRawBlocksToBigBlocks(blocks)); _property = new DocumentProperty(name, _size); _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY); _property.setDocument(this); }
/** * Write out this workbook to the currently open {@link File} via the * writeable {@link POIFSFileSystem} it was opened as. * * <p>This will fail (with an {@link IllegalStateException} if the * Workbook was opened read-only, opened from an {@link InputStream} * instead of a File, or if this is not the root document. For those cases, * you must use {@link #write(OutputStream)} or {@link #write(File)} to * write to a brand new document. */ @Override public void write() throws IOException { validateInPlaceWritePossible(); final DirectoryNode dir = getDirectory(); // Update the Workbook stream in the file DocumentNode workbookNode = (DocumentNode)dir.getEntry( getWorkbookDirEntryName(dir)); POIFSDocument workbookDoc = new POIFSDocument(workbookNode); workbookDoc.replaceContents(new ByteArrayInputStream(getBytes())); // Update the properties streams in the file writeProperties(); // Sync with the File on disk dir.getFileSystem().writeFilesystem(); }
DocumentOutputStream(POIFSDocument document, long limit) throws IOException { _document = document; _document.free(); _property = document.getDocumentProperty(); _limit = limit; }
/** * remove an entry * * @param entry to be removed */ void remove(EntryNode entry) throws IOException { // If it's a document, free the blocks if (entry instanceof DocumentEntry) { POIFSDocument doc = new POIFSDocument((DocumentProperty)entry.getProperty(), this); doc.free(); } // Now zap it from the properties list _property_table.removeProperty(entry.getProperty()); }
/** * Create an InputStream from the specified Document * * @param document the Document to be read */ public DocumentInputStream(POIFSDocument document) { _current_offset = 0; _current_block_count = 0; _marked_offset = 0; _marked_offset_count = 0; _document_size = document.getSize(); _closed = false; _document = document; _data = _document.getBlockIterator(); }
/** * Create an InputStream from the specified DocumentEntry * * @param document the DocumentEntry to be read * * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has * been deleted?) */ public DocumentInputStream(DocumentEntry document) throws IOException { if (!(document instanceof DocumentNode)) { throw new IOException("Cannot open internal document storage, " + document + " not a Document Node"); } _current_offset = 0; _current_block_count = 0; _marked_offset = 0; _marked_offset_count = 0; _document_size = document.getSize(); _closed = false; // can't be asserted ... see bug 61300 // assert (_document_size >= 0) : "Document size can't be < 0"; DocumentNode doc = (DocumentNode)document; DocumentProperty property = (DocumentProperty)doc.getProperty(); _document = new POIFSDocument( property, ((DirectoryNode)doc.getParent()).getNFileSystem() ); _data = _document.getBlockIterator(); }