public void TestExtract() { byte[] data = new byte[512]; int offset = 0; for (int j = 0; j < 8; j++) { for (int k = 0; k < 64; k++) { data[offset++] = (byte)(k + j); } } RawDataBlock[] blocks = { new RawDataBlock(new MemoryStream(data)) }; IList output = SmallDocumentBlock.Extract(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, (ListManagedBlock[])blocks); IEnumerator iter = output.GetEnumerator(); offset = 0; while (iter.MoveNext()) { byte[] out_data = ((SmallDocumentBlock)iter.Current).Data; Assert.AreEqual(64, out_data.Length, "testing block at offset " + offset); for (int j = 0; j < out_data.Length; j++) { Assert.AreEqual(data[offset], out_data[j], "testing byte at offset " + offset); offset++; } } }
public void TestFill() { for (int j = 0; j <= 8; j++) { ArrayList foo = new ArrayList(); for (int k = 0; k < j; k++) { foo.Add(new Object()); } int result = SmallDocumentBlock.Fill(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, foo); Assert.AreEqual((j + 7) / 8, result, "correct big block count: "); Assert.AreEqual(8 * result, foo.Count, "correct small block count: "); for (int m = j; m < foo.Count; m++) { BlockWritable block = (BlockWritable)foo[m]; MemoryStream stream = new MemoryStream(); block.WriteBlocks(stream); byte[] output = stream.ToArray(); Assert.AreEqual(64, output.Length, "correct output size (block[ " + m + " ]): "); for (int n = 0; n < 64; n++) { Assert.AreEqual((byte)0xff, output[n], "correct value (block[ " + m + " ][ " + n + " ]): "); } } } }
public void TestConvert2() { for (int j = 0; j < 320; j++) { byte[] array = new byte[j]; for (int k = 0; k < j; k++) { array[k] = (byte)255; //Tony Qu changed } SmallDocumentBlock[] blocks = SmallDocumentBlock.Convert(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, array, 319); Assert.AreEqual(5, blocks.Length); MemoryStream stream = new MemoryStream(); for (int k = 0; k < blocks.Length; k++) { blocks[k].WriteBlocks(stream); } stream.Close(); byte[] output = stream.ToArray(); for (int k = 0; k < array.Length; k++) { Assert.AreEqual(array[k], output[k], k.ToString()); } for (int k = array.Length; k < 320; k++) { Assert.AreEqual((byte)0xFF, output[k], k.ToString()); } } }
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 TestExtract() { byte[] data = new byte[512]; int offset = 0; for (int j = 0; j < 8; j++) { for (int k = 0; k < 64; k++) { data[offset++] = (byte)(k + j); } } RawDataBlock[] blocks = { new RawDataBlock(new MemoryStream(data)) }; IList <SmallDocumentBlock> output = SmallDocumentBlock.Extract(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, (ListManagedBlock[])blocks); offset = 0; foreach (SmallDocumentBlock block in output) { byte[] out_data = block.Data; Assert.AreEqual(64, out_data.Length, "testing block at offset " + offset); for (int j = 0; j < out_data.Length; j++) { Assert.AreEqual(data[offset], out_data[j], "testing byte at offset " + offset); offset++; } } }
public void TestConstructor() { byte[] data = new byte[2560]; for (int j = 0; j < 2560; j++) { data[j] = (byte)j; } MemoryStream stream = new MemoryStream(data); RawDataBlock[] blocks = new RawDataBlock[5]; for (int j = 0; j < 5; j++) { blocks[j] = new RawDataBlock(stream); } SmallDocumentBlockList sdbl = new SmallDocumentBlockList(SmallDocumentBlock.Extract(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blocks)); // proof we added the blocks for (int j = 0; j < 40; j++) { sdbl.Remove(j); } try { sdbl.Remove(41); Assert.Fail("there should have been an Earth-shattering ka-boom!"); } catch (IOException) { // it better have thrown one!! } }
/// <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 TestCalcSize() { for (int j = 0; j < 10; j++) { Assert.AreEqual(j * 64, SmallDocumentBlock.CalcSize(j), "testing " + j); } }
private static SmallDocumentBlock[] ConvertRawBlocksToSmallBlocks(ListManagedBlock[] blocks) { if (blocks is SmallDocumentBlock[]) { return((SmallDocumentBlock[])blocks); } SmallDocumentBlock[] result = new SmallDocumentBlock[blocks.Length]; System.Array.Copy(blocks, 0, result, 0, blocks.Length); return(result); }
/// <summary> /// read data from the internal stores /// </summary> /// <param name="buffer">the buffer to write to</param> /// <param name="offset">the offset into our storage to read from</param> public virtual void Read(byte[] buffer, int offset) { if (this._property.ShouldUseSmallBlocks) { SmallDocumentBlock.Read(this._small_store.Blocks, buffer, offset); } else { DocumentBlock.Read(this._big_store.Blocks, buffer, offset); } }
public void TestConvert1() { MemoryStream stream = new MemoryStream(_testdata); ArrayList documents = new ArrayList(); while (true) { DocumentBlock block = new DocumentBlock(stream); documents.Add(block); if (block.PartiallyRead) { break; } } SmallDocumentBlock[] results = SmallDocumentBlock .Convert((BlockWritable[])documents .ToArray(typeof(DocumentBlock)), _testdata_size); Assert.AreEqual((_testdata_size + 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 < _testdata_size; 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 DataInputBlock GetDataInputBlock(int offset) { if (offset >= _size) { if (offset > _size) { throw new Exception("Request for Offset " + offset + " doc size is " + _size); } return(null); } if (_property.ShouldUseSmallBlocks) { return(SmallDocumentBlock.GetDataInputBlock(_small_store.Blocks, offset)); } return(DocumentBlock.GetDataInputBlock(_big_store.Blocks, offset)); }
public void TestConvert1() { MemoryStream stream = new MemoryStream(testData); List <DocumentBlock> documents = new List <DocumentBlock>(); 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, documents.ToArray(), 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 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); } }