private static DocumentBlock[] ConvertRawBlocksToBigBlocks(ListManagedBlock[] blocks) { DocumentBlock[] result = new DocumentBlock[blocks.Length]; for (int i = 0; i < result.Length; i++) result[i] = new DocumentBlock((RawDataBlock)blocks[i]); return result; }
/// <summary> /// Initializes a new instance of the <see cref="POIFSDocument"/> class. /// </summary> /// <param name="name">the name of the POIFSDocument</param> /// <param name="stream">the InputStream we read data from</param> public POIFSDocument(string name, Stream stream) { DocumentBlock block; IList list = new ArrayList(); this._size = 0; do { block = new DocumentBlock(stream); int size = block.Size; if (size > 0) { list.Add(block); this._size += size; } } while (!block.PartiallyRead); DocumentBlock[] blocks = (DocumentBlock[])((ArrayList)list).ToArray(typeof(DocumentBlock)); this._big_store = new BigBlockStore(this, blocks); this._property = new DocumentProperty(name, this._size); this._property.Document = this; if (this._property.ShouldUseSmallBlocks) { this._small_store = new SmallBlockStore(this, SmallDocumentBlock.Convert(blocks, this._size)); this._big_store = new BigBlockStore(this, new DocumentBlock[0]); } else { this._small_store = new SmallBlockStore(this, new BlockWritable[0]); } }
public void TestConvert1() { MemoryStream stream = new MemoryStream(testData); ArrayList documents = new ArrayList(); while (true) { DocumentBlock block = new DocumentBlock(stream, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS); documents.Add(block); if (block.PartiallyRead) { break; } } SmallDocumentBlock[] results = SmallDocumentBlock.Convert(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, (BlockWritable[])documents.ToArray(typeof(DocumentBlock)), testDataSize); Assert.AreEqual((testDataSize + 63) / 64, results.Length, "checking correct result size: "); MemoryStream output = new MemoryStream(); for (int j = 0; j < results.Length; j++) { results[j].WriteBlocks(output); } byte[] output_array = output.ToArray(); Assert.AreEqual(64 * results.Length, output_array.Length, "checking correct output size: "); int index = 0; for (; index < testDataSize; index++) { Assert.AreEqual(testData[index], output_array[index], "checking output " + index); } for (; index < output_array.Length; index++) { Assert.AreEqual((byte)0xff, output_array[index], "checking output " + index); } }
public void TestConstructor() { MemoryStream input = new MemoryStream(_testdata); int index = 0; int size = 0; while (true) { byte[] data = new byte[Math.Min(_testdata.Length - index, 512)]; Array.Copy(_testdata, index, data, 0, data.Length); DocumentBlock block = new DocumentBlock(input, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS); verifyOutput(block, data); size += block.Size; if (block.PartiallyRead) { break; } index += 512; } Assert.AreEqual(_testdata.Length, size); }
/// <summary> /// convert a single long array into an array of DocumentBlock /// instances /// </summary> /// <param name="bigBlockSize">the poifs bigBlockSize</param> /// <param name="array">the byte array to be converted</param> /// <param name="size">the intended size of the array (which may be smaller)</param> /// <returns>an array of DocumentBlock instances, filled from the /// input array</returns> public static DocumentBlock[] Convert(POIFSBigBlockSize bigBlockSize, byte[] array, int size) { DocumentBlock[] rval = new DocumentBlock[(size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE]; int offset = 0; for (int k = 0; k < rval.Length; k++) { rval[k] = new DocumentBlock(bigBlockSize); if (offset < array.Length) { int length = Math.Min(POIFSConstants.BIG_BLOCK_SIZE, array.Length - offset); Array.Copy(array, offset, rval[k]._data, 0, length); if (length != POIFSConstants.BIG_BLOCK_SIZE) { for (int j = (length > 0) ? (length - 1) : length; j < POIFSConstants.BIG_BLOCK_SIZE; j++) { rval[k]._data[j] = _default_value; } } } else { for (int j = 0; j < rval[k]._data.Length; j++) { rval[k]._data[j] = _default_value; } } offset += POIFSConstants.BIG_BLOCK_SIZE; } return(rval); }
public static DataInputBlock GetDataInputBlock(DocumentBlock[] blocks, int offset) { if (blocks == null || blocks.Length == 0) return null; POIFSBigBlockSize bigBlockSize = blocks[0].bigBlockSize; int BLOCK_SHIFT = bigBlockSize.GetHeaderValue(); int BLOCK_SIZE = bigBlockSize.GetBigBlockSize(); int BLOCK_MASK = BLOCK_SIZE - 1; int firstBlockIndex = offset >> BLOCK_SHIFT; int firstBlockOffset = offset & BLOCK_MASK; return new DataInputBlock(blocks[firstBlockIndex]._data, firstBlockOffset); }
/// <summary> /// Read data from an array of DocumentBlocks /// </summary> /// <param name="blocks">the blocks to Read from</param> /// <param name="buffer">the buffer to Write the data into</param> /// <param name="offset">the offset into the array of blocks to Read from</param> public static void Read(DocumentBlock[] blocks, byte[] buffer, int offset) { int firstBlockIndex = offset / POIFSConstants.BIG_BLOCK_SIZE; int firstBlockOffSet = offset % POIFSConstants.BIG_BLOCK_SIZE; int lastBlockIndex = (offset + buffer.Length - 1) / POIFSConstants.BIG_BLOCK_SIZE; if (firstBlockIndex == lastBlockIndex) { Array.Copy(blocks[firstBlockIndex]._data, firstBlockOffSet, buffer, 0, buffer.Length); } else { int buffer_offset = 0; Array.Copy(blocks[firstBlockIndex]._data, firstBlockOffSet, buffer, buffer_offset, POIFSConstants.BIG_BLOCK_SIZE - firstBlockOffSet); buffer_offset += POIFSConstants.BIG_BLOCK_SIZE - firstBlockOffSet; for (int j = firstBlockIndex + 1; j < lastBlockIndex; j++) { Array.Copy(blocks[j]._data, 0, buffer, buffer_offset, POIFSConstants.BIG_BLOCK_SIZE); buffer_offset += POIFSConstants.BIG_BLOCK_SIZE; } Array.Copy(blocks[lastBlockIndex]._data, 0, buffer, buffer_offset, buffer.Length - buffer_offset); } }
/// <summary> /// convert a single long array into an array of DocumentBlock /// instances /// </summary> /// <param name="bigBlockSize">the poifs bigBlockSize</param> /// <param name="array">the byte array to be converted</param> /// <param name="size">the intended size of the array (which may be smaller)</param> /// <returns>an array of DocumentBlock instances, filled from the /// input array</returns> public static DocumentBlock[] Convert(POIFSBigBlockSize bigBlockSize, byte[] array, int size) { DocumentBlock[] rval = new DocumentBlock[(size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE]; int offset = 0; for (int k = 0; k < rval.Length; k++) { rval[k] = new DocumentBlock(bigBlockSize); if (offset < array.Length) { int length = Math.Min(POIFSConstants.BIG_BLOCK_SIZE, array.Length - offset); Array.Copy(array, offset, rval[k]._data, 0, length); if (length != POIFSConstants.BIG_BLOCK_SIZE) { for (int j = (length > 0) ? (length - 1) : length; j < POIFSConstants.BIG_BLOCK_SIZE; j++) { rval[k]._data[j] = _default_value; } } } else { for (int j = 0; j < rval[k]._data.Length; j++) { rval[k]._data[j] = _default_value; } } offset += POIFSConstants.BIG_BLOCK_SIZE; } return rval; }
internal BigBlockStore(POIFSBigBlockSize bigBlockSize, DocumentBlock[] blocks) { this.bigBlockSize = bigBlockSize; bigBlocks = (DocumentBlock[])blocks.Clone(); path = null; name = null; size = -1; writer = null; }
public POIFSDocument(string name, POIFSBigBlockSize bigBlockSize, Stream stream) { List<DocumentBlock> blocks = new List<DocumentBlock>(); _size = 0; _bigBigBlockSize = bigBlockSize; while (true) { DocumentBlock block = new DocumentBlock(stream, bigBlockSize); int blockSize = block.Size; if (blockSize > 0) { blocks.Add(block); _size += blockSize; } if (block.PartiallyRead) break; } DocumentBlock[] bigBlocks = blocks.ToArray(); _big_store = new BigBlockStore(bigBlockSize, bigBlocks); _property = new DocumentProperty(name, _size); _property.Document = this; if (_property.ShouldUseSmallBlocks) { _small_store = new SmallBlockStore(bigBlockSize, SmallDocumentBlock.Convert(bigBlockSize, bigBlocks, _size)); _big_store = new BigBlockStore(bigBlockSize, new DocumentBlock[0]); } else { _small_store = new SmallBlockStore(bigBlockSize, EMPTY_SMALL_BLOCK_ARRAY); } }
public void TestRead() { MemoryStream stream = new MemoryStream(testData); ArrayList documents = new ArrayList(); while (true) { DocumentBlock block = new DocumentBlock(stream, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS); documents.Add(block); if (block.PartiallyRead) { break; } } SmallDocumentBlock[] blocks = SmallDocumentBlock.Convert(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, (BlockWritable[])documents.ToArray(typeof(DocumentBlock)), testDataSize); for (int j = 1; j <= testDataSize; j += 38) { byte[] buffer = new byte[j]; int offset = 0; for (int k = 0; k < (testDataSize / j); k++) { SmallDocumentBlock.Read(blocks, buffer, offset); for (int n = 0; n < buffer.Length; n++) { Assert.AreEqual(testData[(k * j) + n], buffer[n], "checking byte " + (k * j) + n); } offset += j; } } }
public void TestRead() { DocumentBlock[] blocks = new DocumentBlock[4]; MemoryStream input = new MemoryStream(_testdata); for (int j = 0; j < 4; j++) { blocks[j] = new DocumentBlock(input, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS); } for (int j = 1; j <= 2000; j += 17) { byte[] buffer = new byte[j]; int offset = 0; for (int k = 0; k < (2000 / j); k++) { DocumentBlock.Read(blocks, buffer, offset); for (int n = 0; n < buffer.Length; n++) { Assert.AreEqual(_testdata[(k * j) + n], buffer[n] , "checking byte " + (k * j) + n); } offset += j; } } }
private void verifyOutput(DocumentBlock block, byte[] input) { Assert.AreEqual(input.Length, block.Size); if (input.Length < 512) { Assert.IsTrue(block.PartiallyRead); } else { Assert.IsTrue(!block.PartiallyRead); } MemoryStream output = new MemoryStream(512); block.WriteBlocks(output); byte[] copy = output.ToArray(); int j = 0; for (; j < input.Length; j++) { Assert.AreEqual(input[j], copy[j]); } for (; j < 512; j++) { Assert.AreEqual((byte)0xFF, copy[j]); } }