public void TestWriteNewStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            // 100 is our first free one
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));


            // Add a single block one
            byte[] data = new byte[512];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            NPOIFSStream stream = new NPOIFSStream(fs);

            stream.UpdateContents(data);

            // Check it was allocated properly
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            // And check the contents
            IEnumerator <ByteBuffer> it = stream.GetBlockIterator();
            int count = 0;

            while (it.MoveNext())
            {
                ByteBuffer b = it.Current;

                data = new byte[512];
                //b.get(data);
                //Array.Copy(b, 0, data, 0, b.Length);
                b.Read(data);
                for (int i = 0; i < data.Length; i++)
                {
                    byte exp = (byte)(i % 256);
                    Assert.AreEqual(exp, data[i]);
                }
                count++;
            }
            Assert.AreEqual(1, count);


            // And a multi block one
            data = new byte[512 * 3];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            stream = new NPOIFSStream(fs);
            stream.UpdateContents(data);

            // Check it was allocated properly
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(102, fs.GetNextBlock(101));
            Assert.AreEqual(103, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            // And check the contents
            it    = stream.GetBlockIterator();
            count = 0;
            while (it.MoveNext())
            {
                ByteBuffer b = it.Current;
                data = new byte[512];
                //b.get(data);
                // Array.Copy(b, 0, data, 0, b.Length);
                b.Read(data);
                for (int i = 0; i < data.Length; i++)
                {
                    byte exp = (byte)(i % 256);
                    Assert.AreEqual(exp, data[i]);
                }
                count++;
            }
            Assert.AreEqual(3, count);

            // Free it
            stream.Free();
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            fs.Close();
        }
Exemple #2
0
        public void TestWriteNewStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            // 100 is our first free one
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));


            // Add a single block one
            byte[] data = new byte[512];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            NPOIFSStream stream = new NPOIFSStream(fs);
            stream.UpdateContents(data);

            // Check it was allocated properly
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            // And check the contents
            IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
            int count = 0;
            while (it.MoveNext())
            {
                ByteBuffer b = it.Current;

                data = new byte[512];
                //b.get(data);
                //Array.Copy(b, 0, data, 0, b.Length);
                b.Read(data);
                for (int i = 0; i < data.Length; i++)
                {
                    byte exp = (byte)(i % 256);
                    Assert.AreEqual(exp, data[i]);
                }
                count++;
            }
            Assert.AreEqual(1, count);


            // And a multi block one
            data = new byte[512 * 3];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            stream = new NPOIFSStream(fs);
            stream.UpdateContents(data);

            // Check it was allocated properly
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(102, fs.GetNextBlock(101));
            Assert.AreEqual(103, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            // And check the contents
            it = stream.GetBlockIterator();
            count = 0;
            while (it.MoveNext())
            {
                ByteBuffer b = it.Current;
                data = new byte[512];
                //b.get(data);
               // Array.Copy(b, 0, data, 0, b.Length);
                b.Read(data);
                for (int i = 0; i < data.Length; i++)
                {
                    byte exp = (byte)(i % 256);
                    Assert.AreEqual(exp, data[i]);
                }
                count++;
            }
            Assert.AreEqual(3, count);

            // Free it
            stream.Free();
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(104));

            fs.Close();
        }