Esempio n. 1
0
        /// <summary>
        /// Create an array of PropertyBlocks from an array of Property
        /// instances, creating empty Property instances to make up any
        /// shortfall
        /// </summary>
        /// <param name="bigBlockSize"></param>
        /// <param name="properties">the Property instances to be converted into PropertyBlocks, in a java List</param>
        /// <returns>the array of newly created PropertyBlock instances</returns>
        public static BlockWritable[] CreatePropertyBlockArray(POIFSBigBlockSize bigBlockSize,
                                                               List <Property> properties)
        {
            int _properties_per_block = bigBlockSize.GetPropertiesPerBlock();

            int blockCount = (properties.Count + _properties_per_block - 1) / _properties_per_block;

            Property[] toBeWritten = new Property[blockCount * _properties_per_block];

            System.Array.Copy(properties.ToArray(), 0, toBeWritten, 0, properties.Count);

            for (int i = properties.Count; i < toBeWritten.Length; i++)
            {
                toBeWritten[i] = new AnonymousProperty();
            }

            BlockWritable[] rvalue = new BlockWritable[blockCount];

            for (int i = 0; i < blockCount; i++)
            {
                rvalue[i] = new PropertyBlock(bigBlockSize, toBeWritten, i * _properties_per_block);
            }

            return(rvalue);
        }
Esempio n. 2
0
        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
                                        + " ]): ");
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Create an array of PropertyBlocks from an array of Property
        /// instances, creating empty Property instances to make up any
        /// shortfall
        /// </summary>
        /// <param name="properties">the Property instances to be converted into PropertyBlocks, in a java List</param>
        /// <returns>the array of newly created PropertyBlock instances</returns>
        public static BlockWritable [] CreatePropertyBlockArray(
            IList properties)
        {
            int block_count =
                (properties.Count + _properties_per_block - 1)
                / _properties_per_block;

            Property[] to_be_written =
                new Property[block_count * _properties_per_block];

            Property[] array = new Property[properties.Count];
            properties.CopyTo(array, 0);

            System.Array.Copy(array, 0,
                              to_be_written, 0, properties.Count);
            for (int j = properties.Count; j < to_be_written.Length; j++)
            {
                // create an instance of an anonymous inner class that
                // extends Property
                to_be_written[j] = new AnonymousProperty();
            }
            BlockWritable[] rvalue = new BlockWritable[block_count];

            for (int j = 0; j < block_count; j++)
            {
                rvalue[j] = new PropertyBlock(to_be_written,
                                              j * _properties_per_block);
            }
            return(rvalue);
        }
Esempio n. 4
0
        /// <summary>
        /// Writes the file system.
        /// </summary>
        /// <param name="stream">the OutputStream to which the filesystem will be
        /// written</param>
        public void WriteFileSystem(Stream stream)
        {
            // Get the property table Ready
            _property_table.PreWrite();

            // Create the small block store, and the SBAT
            SmallBlockTableWriter sbtw =
                new SmallBlockTableWriter(bigBlockSize, _documents, _property_table.Root);

            // Create the block allocation table
            BlockAllocationTableWriter bat =
                new BlockAllocationTableWriter(bigBlockSize);

            // Create a list of BATManaged objects: the documents plus the
            // property table and the small block table
            List <object> bm_objects = new List <object>();

            bm_objects.AddRange(_documents);
            bm_objects.Add(_property_table);
            bm_objects.Add(sbtw);
            bm_objects.Add(sbtw.SBAT);

            // walk the list, allocating space for each and assigning each
            // a starting block number
            IEnumerator iter = bm_objects.GetEnumerator();

            while (iter.MoveNext())
            {
                BATManaged bmo         = ( BATManaged )iter.Current;
                int        block_count = bmo.CountBlocks;

                if (block_count != 0)
                {
                    bmo.StartBlock = bat.AllocateSpace(block_count);
                }
                else
                {
                    // Either the BATManaged object is empty or its data
                    // is composed of SmallBlocks; in either case,
                    // allocating space in the BAT is inappropriate
                }
            }

            // allocate space for the block allocation table and take its
            // starting block
            int batStartBlock = bat.CreateBlocks();

            // Get the extended block allocation table blocks
            HeaderBlockWriter header_block_Writer = new HeaderBlockWriter(bigBlockSize);

            BATBlock[] xbat_blocks =
                header_block_Writer.SetBATBlocks(bat.CountBlocks,
                                                 batStartBlock);

            // Set the property table start block
            header_block_Writer.PropertyStart = _property_table.StartBlock;

            // Set the small block allocation table start block
            header_block_Writer.SBATStart = sbtw.SBAT.StartBlock;

            // Set the small block allocation table block count
            header_block_Writer.SBATBlockCount = sbtw.SBATBlockCount;

            // the header is now properly initialized. Make a list of
            // Writers (the header block, followed by the documents, the
            // property table, the small block store, the small block
            // allocation table, the block allocation table, and the
            // extended block allocation table blocks)
            List <object> Writers = new List <object>();

            Writers.Add(header_block_Writer);
            Writers.AddRange(_documents);
            Writers.Add(_property_table);
            Writers.Add(sbtw);
            Writers.Add(sbtw.SBAT);
            Writers.Add(bat);
            for (int j = 0; j < xbat_blocks.Length; j++)
            {
                Writers.Add(xbat_blocks[j]);
            }

            // now, Write everything out
            iter = Writers.GetEnumerator();
            while (iter.MoveNext())
            {
                BlockWritable Writer = ( BlockWritable )iter.Current;

                Writer.WriteBlocks(stream);
            }

            Writers = null;
            iter    = null;
        }
Esempio n. 5
0
        private void verifyCorrect(BlockWritable[] blocks, byte[] testblock)
        {
            MemoryStream stream = new MemoryStream(512
                                               * blocks.Length);

            for (int j = 0; j < blocks.Length; j++)
            {
                blocks[j].WriteBlocks(stream);
            }
            byte[] output = stream.ToArray();

            Assert.AreEqual(testblock.Length, output.Length);
            for (int j = 0; j < testblock.Length; j++)
            {
                Assert.AreEqual(testblock[j],
                             output[j], "mismatch at offset " + j);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Read data from an array of SmallDocumentBlocks
        /// </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(BlockWritable [] blocks,
                                byte [] buffer, int offset)
        {
            int firstBlockIndex  = offset / _block_size;
            int firstBlockOffSet = offset % _block_size;
            int lastBlockIndex   = (offset + buffer.Length - 1) / _block_size;

            if (firstBlockIndex == lastBlockIndex)
            {
                Array.Copy(
                    (( SmallDocumentBlock ) blocks[ firstBlockIndex ])._data,
                    firstBlockOffSet, buffer, 0, buffer.Length);
            }
            else
            {
                int buffer_offset = 0;

                Array.Copy(
                    (( SmallDocumentBlock ) blocks[ firstBlockIndex ])._data,
                    firstBlockOffSet, buffer, buffer_offset,
                    _block_size - firstBlockOffSet);
                buffer_offset += _block_size - firstBlockOffSet;
                for (int j = firstBlockIndex + 1; j < lastBlockIndex; j++)
                {
                    Array.Copy((( SmallDocumentBlock ) blocks[ j ])._data,
                                     0, buffer, buffer_offset, _block_size);
                    buffer_offset += _block_size;
                }
                Array.Copy(
                    (( SmallDocumentBlock ) blocks[ lastBlockIndex ])._data, 0,
                    buffer, buffer_offset, buffer.Length - buffer_offset);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Factory for creating SmallDocumentBlocks from DocumentBlocks
        /// </summary>
        /// <param name="store">the original DocumentBlocks</param>
        /// <param name="size">the total document size</param>
        /// <returns>an array of new SmallDocumentBlocks instances</returns>
        public static SmallDocumentBlock[] Convert(BlockWritable [] store,
                                                    int size)
        {
            MemoryStream stream = new MemoryStream();

            for (int j = 0; j < store.Length; j++)
            {
                store[ j ].WriteBlocks(stream);
            }
            byte[]               data = stream.ToArray();
            SmallDocumentBlock[] rval =
                new SmallDocumentBlock[ ConvertToBlockCount(size) ];

            for (int index = 0; index < rval.Length; index++)
            {
                rval[ index ] = new SmallDocumentBlock(data, index);
            }
            return rval;
        }
Esempio n. 8
0
        /// <summary>
        /// Create an array of PropertyBlocks from an array of Property
        /// instances, creating empty Property instances to make up any
        /// shortfall
        /// </summary>
        /// <param name="bigBlockSize"></param>
        /// <param name="properties">the Property instances to be converted into PropertyBlocks, in a java List</param>
        /// <returns>the array of newly created PropertyBlock instances</returns>
        public static BlockWritable [] CreatePropertyBlockArray( POIFSBigBlockSize bigBlockSize,
                                        List<Property> properties)
            {
            int _properties_per_block = bigBlockSize.GetPropertiesPerBlock();

            int blockCount = (properties.Count + _properties_per_block - 1) / _properties_per_block;

            Property[] toBeWritten = new Property[blockCount * _properties_per_block];

            System.Array.Copy(properties.ToArray(), 0, toBeWritten, 0, properties.Count);

            for (int i = properties.Count; i < toBeWritten.Length; i++)
            {
                toBeWritten[i] = new AnonymousProperty();
            }

            BlockWritable[] rvalue = new BlockWritable[blockCount];

            for (int i = 0; i < blockCount; i++)
                rvalue[i] = new PropertyBlock(bigBlockSize, toBeWritten, i * _properties_per_block);

            return rvalue;
        }
Esempio n. 9
0
        /// <summary>
        /// Create an array of PropertyBlocks from an array of Property
        /// instances, creating empty Property instances to make up any
        /// shortfall
        /// </summary>
        /// <param name="properties">the Property instances to be converted into PropertyBlocks, in a java List</param>
        /// <returns>the array of newly created PropertyBlock instances</returns>
        public static BlockWritable [] CreatePropertyBlockArray(
                IList properties)
        {
            int        block_count   =
                (properties.Count + _properties_per_block - 1)
                / _properties_per_block;
            Property[] to_be_written =
                new Property[ block_count * _properties_per_block ];

            Property[] array = new Property[properties.Count];
            properties.CopyTo(array, 0);

            System.Array.Copy(array, 0,
                             to_be_written, 0, properties.Count);
            for (int j = properties.Count; j < to_be_written.Length; j++)
            {

                // create an instance of an anonymous inner class that
                // extends Property
                to_be_written[j] = new AnonymousProperty();
            }
            BlockWritable[] rvalue = new BlockWritable[ block_count ];

            for (int j = 0; j < block_count; j++)
            {
                rvalue[ j ] = new PropertyBlock(to_be_written,
                                                j * _properties_per_block);
            }
            return rvalue;
        }