/** * Has our in-memory objects write their state * to their backing blocks */ private void syncWithDataSource() { // Mini Stream + SBATs first, as mini-stream details have // to be stored in the Root Property _mini_store.SyncWithDataSource(); // Properties NPOIFSStream propStream = new NPOIFSStream(this, _header.PropertyStart); _property_table.PreWrite(); _property_table.Write(propStream); // _header.setPropertyStart has been updated on write ... // HeaderBlock HeaderBlockWriter hbw = new HeaderBlockWriter(_header); hbw.WriteBlock(GetBlockAt(-1)); // BATs foreach (BATBlock bat in _bat_blocks) { ByteBuffer block = GetBlockAt(bat.OurBlockIndex); //byte[] block = GetBlockAt(bat.OurBlockIndex); BlockAllocationTableWriter.WriteBlock(bat, block); } // XBats foreach (BATBlock bat in _xbat_blocks) { ByteBuffer block = GetBlockAt(bat.OurBlockIndex); BlockAllocationTableWriter.WriteBlock(bat, block); } }
public NPOIFSDocument(String name, int size, NPOIFSFileSystem filesystem, POIFSWriterListener Writer) { this._filesystem = filesystem; if (size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { _stream = new NPOIFSStream(filesystem.GetMiniStore()); _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize(); } else { _stream = new NPOIFSStream(filesystem); _block_size = _filesystem.GetBlockStoreBlockSize(); } Stream innerOs = _stream.GetOutputStream(); DocumentOutputStream os = new DocumentOutputStream(innerOs, size); POIFSDocumentPath path = new POIFSDocumentPath(name.Split(new string[] { "\\\\" }, StringSplitOptions.RemoveEmptyEntries)); String docName = path.GetComponent(path.Length - 1); POIFSWriterEvent event1 = new POIFSWriterEvent(os, path, docName, size); Writer.ProcessPOIFSWriterEvent(event1); innerOs.Close(); // And build the property for it this._property = new DocumentProperty(name, size); _property.StartBlock = (/*setter*/ _stream.GetStartBlock()); }
protected internal StreamBlockByteBuffer(NPOIFSStream pStream) { this.pStream = pStream; loopDetector = pStream.blockStore.GetChainLoopDetector(); prevBlock = POIFSConstants.END_OF_CHAIN; nextBlock = pStream.startBlock; }
public NPOIFSMiniStore(NPOIFSFileSystem filesystem, RootProperty root, List <BATBlock> sbats, HeaderBlock header) { this._filesystem = filesystem; this._sbat_blocks = sbats; this._header = header; this._root = root; this._mini_stream = new NPOIFSStream(filesystem, root.StartBlock); }
/** * Load the block, extending the underlying stream if needed */ public override ByteBuffer CreateBlockIfNeeded(int offset) { bool firstInStore = false; // If we are the first block to be allocated, initialise the stream if (_mini_stream.GetStartBlock() == POIFSConstants.END_OF_CHAIN) { firstInStore = true; } // Try to Get it without extending the stream if (!firstInStore) { try { return(GetBlockAt(offset)); }catch (IndexOutOfRangeException) {} } // Need to extend the stream // TODO Replace this with proper append support // For now, do the extending by hand... // Ask for another block int newBigBlock = _filesystem.GetFreeBlock(); _filesystem.CreateBlockIfNeeded(newBigBlock); // If we are the first block to be allocated, initialise the stream if (firstInStore) { _filesystem.PropertyTable.Root.StartBlock = (newBigBlock); _mini_stream = new NPOIFSStream(_filesystem, newBigBlock); } else { // Tack it onto the end of our chain ChainLoopDetector loopDetector = _filesystem.GetChainLoopDetector(); int block = _mini_stream.GetStartBlock(); while (true) { loopDetector.Claim(block); int next = _filesystem.GetNextBlock(block); if (next == POIFSConstants.END_OF_CHAIN) { break; } block = next; } _filesystem.SetNextBlock(block, newBigBlock); } _filesystem.SetNextBlock(newBigBlock, POIFSConstants.END_OF_CHAIN); // Now try again, to get the real small block return(CreateBlockIfNeeded(offset)); }
public void Close() { // If we're overwriting, free any remaining blocks NPOIFSStream toFree = new NPOIFSStream(pStream.blockStore, nextBlock); toFree.Free(loopDetector); // Mark the end of the stream pStream.blockStore.SetNextBlock(prevBlock, POIFSConstants.END_OF_CHAIN); base.Flush(); }
public StreamBlockByteBufferIterator(NPOIFSStream pStream, int firstBlock) { this.pStream = pStream; this.nextBlock = firstBlock; try { this.loopDetector = pStream.blockStore.GetChainLoopDetector(); } catch (IOException e) { //throw new System.RuntimeException(e); throw new Exception(e.Message); } }
/** * Constructor for an existing Document */ public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem) { this._property = property; this._filesystem = filesystem; if (property.Size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { _stream = new NPOIFSStream(_filesystem.GetMiniStore(), property.StartBlock); _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize(); } else { _stream = new NPOIFSStream(_filesystem, property.StartBlock); _block_size = _filesystem.GetBlockStoreBlockSize(); } }
public override void Write(byte[] b, int off, int len) { dieIfClosed(); if (_buffer != null) { _buffer.Write(b, off, len); CheckBufferSize(); } else { if (_stream == null) { _stream = new NPOIFSStream(_document.FileSystem); _stream_output = _stream.GetOutputStream(); } _stream_output.Write(b, off, len); _document_size += len; } }
/** * Constructor for a new Document * * @param name the name of the POIFSDocument * @param stream the InputStream we read data from */ public NPOIFSDocument(String name, NPOIFSFileSystem filesystem, Stream stream) { this._filesystem = filesystem; // Buffer the contents into memory. This is a bit icky... // TODO Replace with a buffer up to the mini stream size, then streaming write byte[] contents; if (stream is MemoryStream) { MemoryStream bais = (MemoryStream)stream; contents = new byte[bais.Length]; bais.Read(contents, 0, contents.Length); } else { MemoryStream baos = new MemoryStream(); IOUtils.Copy(stream, baos); contents = baos.ToArray(); } // Do we need to store as a mini stream or a full one? if (contents.Length <= POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { _stream = new NPOIFSStream(filesystem.GetMiniStore()); _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize(); } else { _stream = new NPOIFSStream(filesystem); _block_size = _filesystem.GetBlockStoreBlockSize(); } // Store it _stream.UpdateContents(contents); // And build the property for it this._property = new DocumentProperty(name, contents.Length); _property.StartBlock = _stream.GetStartBlock(); }
/** * Updates the contents of the stream to the new * Set of bytes. * Note - if this is property based, you'll still * need to update the size in the property yourself */ public void UpdateContents(byte[] contents) { // How many blocks are we going to need? int blockSize = blockStore.GetBlockStoreBlockSize(); int blocks = (int)Math.Ceiling(((double)contents.Length) / blockSize); // Make sure we don't encounter a loop whilst overwriting // the existing blocks ChainLoopDetector loopDetector = blockStore.GetChainLoopDetector(); // Start writing int prevBlock = POIFSConstants.END_OF_CHAIN; int nextBlock = startBlock; for (int i = 0; i < blocks; i++) { int thisBlock = nextBlock; // Allocate a block if needed, otherwise figure // out what the next block will be if (thisBlock == POIFSConstants.END_OF_CHAIN) { thisBlock = blockStore.GetFreeBlock(); loopDetector.Claim(thisBlock); // We're on the end of the chain nextBlock = POIFSConstants.END_OF_CHAIN; // Mark the previous block as carrying on to us if needed if (prevBlock != POIFSConstants.END_OF_CHAIN) { blockStore.SetNextBlock(prevBlock, thisBlock); } blockStore.SetNextBlock(thisBlock, POIFSConstants.END_OF_CHAIN); // If we've just written the first block on a // new stream, save the start block offset if (this.startBlock == POIFSConstants.END_OF_CHAIN) { this.startBlock = thisBlock; } } else { loopDetector.Claim(thisBlock); nextBlock = blockStore.GetNextBlock(thisBlock); } // Write it //byte[] buffer = blockStore.CreateBlockIfNeeded(thisBlock); ByteBuffer buffer = blockStore.CreateBlockIfNeeded(thisBlock); int startAt = i * blockSize; int endAt = Math.Min(contents.Length - startAt, blockSize); buffer.Write(contents, startAt, endAt); //for (int index = startAt, j = 0; index < endAt; index++, j++) // buffer[j] = contents[index]; // Update pointers prevBlock = thisBlock; } int lastBlock = prevBlock; // If we're overwriting, free any remaining blocks NPOIFSStream toFree = new NPOIFSStream(blockStore, nextBlock); toFree.free(loopDetector); // Mark the end of the stream blockStore.SetNextBlock(lastBlock, POIFSConstants.END_OF_CHAIN); }
private int Store(Stream inStream) { int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE; //BufferedStream bis = new BufferedStream(stream, bigBlockSize + 1); //bis.mark(bigBlockSize); //// Buffer the contents into memory. This is a bit icky... //// TODO Replace with a buffer up to the mini stream size, then streaming write //byte[] contents; //if (stream is MemoryStream) //{ // MemoryStream bais = (MemoryStream)stream; // contents = new byte[bais.Length]; // bais.Read(contents, 0, contents.Length); //} //else //{ // MemoryStream baos = new MemoryStream(); // IOUtils.Copy(stream, baos); // contents = baos.ToArray(); //} // Do we need to store as a mini stream or a full one? if (inStream.Length < bigBlockSize) { _stream = new NPOIFSStream(_filesystem.GetMiniStore()); _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize(); } else { _stream = new NPOIFSStream(_filesystem); _block_size = _filesystem.GetBlockStoreBlockSize(); } // start from the beginning //bis.Seek(0, SeekOrigin.Begin); // Store it Stream outStream = _stream.GetOutputStream(); byte[] buf = new byte[1024]; int length = 0; //for (int readBytes; (readBytes = bis.Read(buf, 0, buf.Length)) != 0; length += readBytes) //{ // outStream.Write(buf, 0, readBytes); //} for (int readBytes = 0; ;) { readBytes = inStream.Read(buf, 0, buf.Length); if (readBytes <= 0) { break; } length += readBytes; outStream.Write(buf, 0, readBytes); } outStream.Close(); return(length); }