コード例 #1
0
ファイル: TestXWPFBugs.cs プロジェクト: Reinakumiko/npoi
        public void Test53475()
        {
            try
            {
                Biff8EncryptionKey.CurrentUserPassword = (/*setter*/"solrcell");
                FileStream file = POIDataSamples.GetDocumentInstance().GetFile("bug53475-password-is-solrcell.docx");
                NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file,null, true, true);
/*
                // Check the encryption details
                EncryptionInfo info = new EncryptionInfo(filesystem);
                Assert.AreEqual(128, info.Header.KeySize);
                Assert.AreEqual(EncryptionHeader.ALGORITHM_AES_128, info.Header.Algorithm);
                Assert.AreEqual(EncryptionHeader.HASH_SHA1, info.Header.HashAlgorithm);

                // Check it can be decoded
                Decryptor d = Decryptor.GetInstance(info);
                Assert.IsTrue("Unable to Process: document is encrypted", d.VerifyPassword("solrcell"));

                // Check we can read the word document in that
                InputStream dataStream = d.GetDataStream(filesystem);
                OPCPackage opc = OPCPackage.Open(dataStream);
                XWPFDocument doc = new XWPFDocument(opc);
                XWPFWordExtractor ex = new XWPFWordExtractor(doc);
                String text = ex.Text;
                Assert.IsNotNull(text);
                Assert.AreEqual("This is password protected Word document.", text.Trim());
                ex.Close();
 */
                filesystem.Close();
            }
            finally
            {
                Biff8EncryptionKey.CurrentUserPassword = (/*setter*/null);
            }
        }
コード例 #2
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadTinyStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            
            // 98 is actually the last block in a two block stream...
            NPOIFSStream stream = new NPOIFSStream(fs, 98);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();
            Assert.AreEqual(true, i.MoveNext());
            ByteBuffer b = i.Current;
            Assert.AreEqual(false, i.MoveNext());

            // Check the contents
            Assert.AreEqual((byte)0x81, b[0]);
            Assert.AreEqual((byte)0x00, b[1]);
            Assert.AreEqual((byte)0x00, b[2]);
            Assert.AreEqual((byte)0x00, b[3]);
            Assert.AreEqual((byte)0x82, b[4]);
            Assert.AreEqual((byte)0x00, b[5]);
            Assert.AreEqual((byte)0x00, b[6]);
            Assert.AreEqual((byte)0x00, b[7]);

            fs.Close();
        }
コード例 #3
0
ファイル: TestOle10Native.cs プロジェクト: Reinakumiko/npoi
        public void TestFiles()
        {
            FileStream[] files = {
            // bug 51891
            POIDataSamples.GetPOIFSInstance().GetFile("multimedia.doc"),
            // tika bug 1072
            POIDataSamples.GetPOIFSInstance().GetFile("20-Force-on-a-current-S00.doc"),
            // other files Containing ole10native records ...
            POIDataSamples.GetDocumentInstance().GetFile("Bug53380_3.doc"),
            POIDataSamples.GetDocumentInstance().GetFile("Bug47731.doc")
        };

            foreach (FileStream f in files)
            {
                NPOIFSFileSystem fs = new NPOIFSFileSystem(f,null, true, true);
                List<Entry> entries = new List<Entry>();
                FindOle10(entries, fs.Root, "/", "");

                foreach (Entry e in entries)
                {
                    MemoryStream bosExp = new MemoryStream();
                    Stream is1 = ((DirectoryNode)e.Parent).CreateDocumentInputStream(e);
                    IOUtils.Copy(is1, bosExp);
                    is1.Close();

                    Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)e.Parent);

                    MemoryStream bosAct = new MemoryStream();
                    ole.WriteOut(bosAct);

                    //assertThat(bosExp.ToByteArray(), EqualTo(bosAct.ToByteArray()));
                    Assert.IsTrue(Arrays.Equals(bosExp.ToArray(), bosAct.ToArray()));
                }

                fs.Close();
            }
        }
コード例 #4
0
ファイル: TestHSSFWorkbook.cs プロジェクト: mdjasim/npoi
        public void TestWriteWorkbookFromNPOIFS()
        {
            //throw new NotImplementedException("class NPOIFSFileSystem is not implemented");
            Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("WithEmbeddedObjects.xls");
            NPOIFSFileSystem fs = new NPOIFSFileSystem(is1);

            // Start as NPOIFS
            HSSFWorkbook wb = new HSSFWorkbook(fs.Root, true);
            Assert.AreEqual(3, wb.NumberOfSheets);
            Assert.AreEqual("Root xls", wb.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue);

            // Will switch to POIFS
            wb = HSSFTestDataSamples.WriteOutAndReadBack(wb);
            Assert.AreEqual(3, wb.NumberOfSheets);
            Assert.AreEqual("Root xls", wb.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue);

            fs.Close();
        }
コード例 #5
0
        public void CreateWriteRead()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem();
            DocumentEntry miniDoc;
            DocumentEntry normDoc;

            // Initially has a BAT but not SBAT
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2));

            // Check that the SBAT is empty
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);

            // Check that no properties table has been written yet
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.PropertyTable.StartBlock);

            // Write and read it
            fs = WriteOutAndReadBack(fs);

            // Property table entries have been added to the blocks 
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(3));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);
            Assert.AreEqual(2, fs.PropertyTable.StartBlock);

            // Put everything within a new directory
            DirectoryEntry testDir = fs.CreateDirectory("Test Directory");

            // Add a new Normal Stream (Normal Streams minimum 4096 bytes)
            byte[] main4096 = new byte[4096];
            main4096[0] = unchecked((byte)-10);
            main4096[4095] = unchecked((byte)-11);
            testDir.CreateDocument("Normal4096", new MemoryStream(main4096));

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));

            Assert.AreEqual(4, fs.GetNextBlock(3));
            Assert.AreEqual(5, fs.GetNextBlock(4));
            Assert.AreEqual(6, fs.GetNextBlock(5));
            Assert.AreEqual(7, fs.GetNextBlock(6));
            Assert.AreEqual(8, fs.GetNextBlock(7));
            Assert.AreEqual(9, fs.GetNextBlock(8));
            Assert.AreEqual(10, fs.GetNextBlock(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(11));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);


            // Add a bigger Normal Stream
            byte[] main5124 = new byte[5124];
            main5124[0] = unchecked((byte)-22);
            main5124[5123] = unchecked((byte)-33);
            testDir.CreateDocument("Normal5124", new MemoryStream(main5124));

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
                Assert.AreEqual(4, fs.GetNextBlock(3));
                Assert.AreEqual(5, fs.GetNextBlock(4));
                Assert.AreEqual(6, fs.GetNextBlock(5));
                Assert.AreEqual(7, fs.GetNextBlock(6));
                Assert.AreEqual(8, fs.GetNextBlock(7));
                Assert.AreEqual(9, fs.GetNextBlock(8));
                Assert.AreEqual(10, fs.GetNextBlock(9));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));

                Assert.AreEqual(12, fs.GetNextBlock(11));
                Assert.AreEqual(13, fs.GetNextBlock(12));
                Assert.AreEqual(14, fs.GetNextBlock(13));
                Assert.AreEqual(15, fs.GetNextBlock(14));
                Assert.AreEqual(16, fs.GetNextBlock(15));
                Assert.AreEqual(17, fs.GetNextBlock(16));
                Assert.AreEqual(18, fs.GetNextBlock(17));
                Assert.AreEqual(19, fs.GetNextBlock(18));
                Assert.AreEqual(20, fs.GetNextBlock(19));
                Assert.AreEqual(21, fs.GetNextBlock(20));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21));
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(22));

            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock);


            // Now Add a mini stream
            byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
            testDir.CreateDocument("Mini", new MemoryStream(mini));

            // Mini stream will Get one block for fat + one block for data
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));

                Assert.AreEqual(4, fs.GetNextBlock(3));
                Assert.AreEqual(5, fs.GetNextBlock(4));
                Assert.AreEqual(6, fs.GetNextBlock(5));
                Assert.AreEqual(7, fs.GetNextBlock(6));
                Assert.AreEqual(8, fs.GetNextBlock(7));
                Assert.AreEqual(9, fs.GetNextBlock(8));
                Assert.AreEqual(10, fs.GetNextBlock(9));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));

                Assert.AreEqual(12, fs.GetNextBlock(11));
                Assert.AreEqual(13, fs.GetNextBlock(12));
                Assert.AreEqual(14, fs.GetNextBlock(13));
                Assert.AreEqual(15, fs.GetNextBlock(14));
                Assert.AreEqual(16, fs.GetNextBlock(15));
                Assert.AreEqual(17, fs.GetNextBlock(16));
                Assert.AreEqual(18, fs.GetNextBlock(17));
                Assert.AreEqual(19, fs.GetNextBlock(18));
                Assert.AreEqual(20, fs.GetNextBlock(19));
                Assert.AreEqual(21, fs.GetNextBlock(20));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22));
                Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23));
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(24));

            // Check the mini stream location was set
            // (22 is mini fat, 23 is first mini stream block)
            Assert.AreEqual(23, fs.Root.Property.StartBlock);

            // Write and read back
            fs = WriteOutAndReadBack(fs);
            HeaderBlock header = WriteOutAndReadHeader(fs);

            // Check the header has the right points in it
            Assert.AreEqual(1, header.BATCount);
            Assert.AreEqual(0, header.BATArray[0]);
            Assert.AreEqual(2, header.PropertyStart);
            Assert.AreEqual(1, header.SBATCount);
            Assert.AreEqual(22, header.SBATStart);
            Assert.AreEqual(23, fs.PropertyTable.Root.StartBlock);

            // Block use should be almost the same, except the properties
            //  stream will have grown out to cover 2 blocks
            // Check the block use is all unChanged
            // Check it's all unChanged
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(24, fs.GetNextBlock(2)); // Properties now extends over 2 blocks

            Assert.AreEqual(4, fs.GetNextBlock(3));
            Assert.AreEqual(5, fs.GetNextBlock(4));
            Assert.AreEqual(6, fs.GetNextBlock(5));
            Assert.AreEqual(7, fs.GetNextBlock(6));
            Assert.AreEqual(8, fs.GetNextBlock(7));
            Assert.AreEqual(9, fs.GetNextBlock(8));
            Assert.AreEqual(10, fs.GetNextBlock(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));// End of normal4096

            Assert.AreEqual(12, fs.GetNextBlock(11));
            Assert.AreEqual(13, fs.GetNextBlock(12));
            Assert.AreEqual(14, fs.GetNextBlock(13));
            Assert.AreEqual(15, fs.GetNextBlock(14));
            Assert.AreEqual(16, fs.GetNextBlock(15));
            Assert.AreEqual(17, fs.GetNextBlock(16));
            Assert.AreEqual(18, fs.GetNextBlock(17));
            Assert.AreEqual(19, fs.GetNextBlock(18));
            Assert.AreEqual(20, fs.GetNextBlock(19));
            Assert.AreEqual(21, fs.GetNextBlock(20));

            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21)); // End of normal5124 

            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22)); // Mini Stream FAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23)); // Mini Stream data
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(24)); // Properties #2
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(25));

            // Check some data
            Assert.AreEqual(1, fs.Root.EntryCount);
            testDir = (DirectoryEntry)fs.Root.GetEntry("Test Directory");
            Assert.AreEqual(3, testDir.EntryCount);

            miniDoc = (DocumentEntry)testDir.GetEntry("Mini");
            assertContentsMatches(mini, miniDoc);

            normDoc = (DocumentEntry)testDir.GetEntry("Normal4096");
            assertContentsMatches(main4096, normDoc);

            normDoc = (DocumentEntry)testDir.GetEntry("Normal5124");
            assertContentsMatches(main5124, normDoc);

            // Delete a couple of streams
            miniDoc.Delete();
            normDoc.Delete();


            // Check - will have un-used sectors now
            fs = WriteOutAndReadBack(fs);

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); // Props back in 1 block

            Assert.AreEqual(4, fs.GetNextBlock(3));
            Assert.AreEqual(5, fs.GetNextBlock(4));
            Assert.AreEqual(6, fs.GetNextBlock(5));
            Assert.AreEqual(7, fs.GetNextBlock(6));
            Assert.AreEqual(8, fs.GetNextBlock(7));
            Assert.AreEqual(9, fs.GetNextBlock(8));
            Assert.AreEqual(10, fs.GetNextBlock(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10)); // End of normal4096

            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(11));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(12));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(13));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(14));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(15));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(16));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(17));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(18));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(19));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(20));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(21));

            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22)); // Mini Stream FAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23)); // Mini Stream data
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(24)); // Properties gone
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(25));
      

            // All done
            fs.Close();
        }
コード例 #6
0
        public void TestGetFreeBlockWithSpare()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));

            Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);

            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(100, fs.GetFreeBlock());

            Assert.AreEqual(100, fs.GetFreeBlock());

            fs.SetNextBlock(100, POIFSConstants.END_OF_CHAIN);
            Assert.AreEqual(101, fs.GetFreeBlock());

            fs.Close();
        }
コード例 #7
0
        public void TestBasicOpen()
        {
            NPOIFSFileSystem fsA, fsB;
            fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB })
            {
                Assert.AreEqual(512, fs.GetBigBlockSize());
            }

            fsA.Close();
            fsB.Close();

            fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi"));
            fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));

            foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB })
            {
                Assert.AreEqual(4096, fs.GetBigBlockSize());
            }
            fsA.Close();
            fsB.Close();
        }
コード例 #8
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestWriteFailsOnLoop()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));

            // Hack the FAT so that it goes 0->1->2->0
            fs.SetNextBlock(0, 1);
            fs.SetNextBlock(1, 2);
            fs.SetNextBlock(2, 0);

            // Try to write a large amount, should fail on the write
            byte[] data = new byte[512 * 4];
            NPOIFSStream stream = new NPOIFSStream(fs, 0);
            try
            {
                stream.UpdateContents(data);
                Assert.Fail("Loop should have been detected but wasn't!");
            }
            catch (Exception) { }

            // Now reset, and try on a small bit
            // Should fail during the freeing set
            fs.SetNextBlock(0, 1);
            fs.SetNextBlock(1, 2);
            fs.SetNextBlock(2, 0);

            data = new byte[512];
            stream = new NPOIFSStream(fs, 0);
            try
            {
                stream.UpdateContents(data);
                Assert.Fail("Loop should have been detected but wasn't!");
            }
            catch (Exception) { }

            fs.Close();
        }
コード例 #9
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestWriteMiniStreams()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore ministore = fs.GetMiniStore();
            NPOIFSStream stream = new NPOIFSStream(ministore, 178);

            // 178 -> 179 -> 180 -> end
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180));


            // Try writing 3 full blocks worth
            byte[] data = new byte[64 * 3];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)i;
            }
            stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            // Check
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180));

            stream = new NPOIFSStream(ministore, 178);
            IEnumerator<ByteBuffer> it = stream.GetBlockIterator();
            it.MoveNext();
            ByteBuffer b178 = it.Current;
            it.MoveNext();
            ByteBuffer b179 = it.Current;
            it.MoveNext();
            ByteBuffer b180 = it.Current;

            Assert.AreEqual(false, it.MoveNext());

            Assert.AreEqual((byte)0x00, b178.Read());
            Assert.AreEqual((byte)0x01, b178.Read());
            Assert.AreEqual((byte)0x40, b179.Read());
            Assert.AreEqual((byte)0x41, b179.Read());
            Assert.AreEqual((byte)0x80, b180.Read());
            Assert.AreEqual((byte)0x81, b180.Read());


            // Try writing just into 3 blocks worth
            data = new byte[64 * 2 + 12];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i + 4);
            }
            stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            // Check
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180));

            stream = new NPOIFSStream(ministore, 178);
            it = stream.GetBlockIterator();
            it.MoveNext();
            b178 = it.Current;
            it.MoveNext();
            b179 = it.Current;
            it.MoveNext();
            b180 = it.Current;
            Assert.AreEqual(false, it.MoveNext());

            Assert.AreEqual((byte)0x04, b178.Read());
            Assert.AreEqual((byte)0x05, b178.Read());
            Assert.AreEqual((byte)0x44, b179.Read());
            Assert.AreEqual((byte)0x45, b179.Read());
            Assert.AreEqual((byte)0x84, b180.Read());
            Assert.AreEqual((byte)0x85, b180.Read());


            // Try writing 1, should truncate
            data = new byte[12];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i + 9);
            }
            stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(178));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(180));

            stream = new NPOIFSStream(ministore, 178);
            it = stream.GetBlockIterator();
            it.MoveNext();
            b178 = it.Current;
            Assert.AreEqual(false, it.MoveNext());

            Assert.AreEqual((byte)0x09, b178[0]);
            Assert.AreEqual((byte)0x0a, b178[1]);

            // Try writing 5, should extend
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(178));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(180));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(181));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(182));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(183));

            data = new byte[64 * 4 + 12];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i + 3);
            }
            stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(181, ministore.GetNextBlock(180));
            Assert.AreEqual(182, ministore.GetNextBlock(181));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(182));

            stream = new NPOIFSStream(ministore, 178);
            it = stream.GetBlockIterator();
            it.MoveNext();
            b178 = it.Current;
            it.MoveNext();
            b179 = it.Current;
            it.MoveNext();
            b180 = it.Current;
            it.MoveNext();
            ByteBuffer b181 = it.Current;
            it.MoveNext();
            ByteBuffer b182 = it.Current;
            Assert.AreEqual(false, it.MoveNext());

            Assert.AreEqual((byte)0x03, b178[0]);
            Assert.AreEqual((byte)0x04, b178[1]);
            Assert.AreEqual((byte)0x43, b179[0]);
            Assert.AreEqual((byte)0x44, b179[1]);
            Assert.AreEqual((byte)0x83, b180[0]);
            Assert.AreEqual((byte)0x84, b180[1]);
            Assert.AreEqual((byte)0xc3, b181[0]);
            Assert.AreEqual((byte)0xc4, b181[1]);
            Assert.AreEqual((byte)0x03, b182[0]);
            Assert.AreEqual((byte)0x04, b182[1]);


            // Write lots, so it needs another big block
            ministore.GetBlockAt(183);
            try
            {
                ministore.GetBlockAt(184);
                Assert.Fail("Block 184 should be off the end of the list");
            }
           // catch (ArgumentOutOfRangeException e)
            catch(Exception)
            {
            }

            data = new byte[64 * 6 + 12];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i + 1);
            }
            stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            // Should have added 2 more blocks to the chain
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(181, ministore.GetNextBlock(180));
            Assert.AreEqual(182, ministore.GetNextBlock(181));
            Assert.AreEqual(183, ministore.GetNextBlock(182));
            Assert.AreEqual(184, ministore.GetNextBlock(183));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(184));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(185));

            // Block 184 should exist
            ministore.GetBlockAt(183);
            ministore.GetBlockAt(184);
            ministore.GetBlockAt(185);

            // Check contents
            stream = new NPOIFSStream(ministore, 178);
            it = stream.GetBlockIterator();
            it.MoveNext();
            b178 = it.Current;
            it.MoveNext();
            b179 = it.Current;
            it.MoveNext();
            b180 = it.Current;
            it.MoveNext();
            b181 = it.Current;
            it.MoveNext();
            b182 = it.Current;
            it.MoveNext();
            ByteBuffer b183 = it.Current;
            it.MoveNext();
            ByteBuffer b184 = it.Current;
            Assert.AreEqual(false, it.MoveNext());

            Assert.AreEqual((byte)0x01, b178[0]);
            Assert.AreEqual((byte)0x02, b178[1]);
            Assert.AreEqual((byte)0x41, b179[0]);
            Assert.AreEqual((byte)0x42, b179[1]);
            Assert.AreEqual((byte)0x81, b180[0]);
            Assert.AreEqual((byte)0x82, b180[1]);
            Assert.AreEqual((byte)0xc1, b181[0]);
            Assert.AreEqual((byte)0xc2, b181[1]);
            Assert.AreEqual((byte)0x01, b182[0]);
            Assert.AreEqual((byte)0x02, b182[1]);
            Assert.AreEqual((byte)0x41, b183[0]);
            Assert.AreEqual((byte)0x42, b183[1]);
            Assert.AreEqual((byte)0x81, b184[0]);
            Assert.AreEqual((byte)0x82, b184[1]);

            fs.Close();
        }
コード例 #10
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadStream4096()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi"));

            // 0 -> 1 -> 2 -> end
            NPOIFSStream stream = new NPOIFSStream(fs, 0);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();

            Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b0 = i.Current;
            Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b1 = i.Current;
            Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b2 = i.Current;
            Assert.AreEqual(false, i.MoveNext());

            // Check the contents of the 1st block
            Assert.AreEqual((byte)0x9E, b0[0]);
            Assert.AreEqual((byte)0x75, b0[1]);
            Assert.AreEqual((byte)0x97, b0[2]);
            Assert.AreEqual((byte)0xF6, b0[3]);
            Assert.AreEqual((byte)0xFF, b0[4]);
            Assert.AreEqual((byte)0x21, b0[5]);
            Assert.AreEqual((byte)0xD2, b0[6]);
            Assert.AreEqual((byte)0x11, b0[7]);

            // Check the contents of the 2nd block
            Assert.AreEqual((byte)0x00, b1[0]);
            Assert.AreEqual((byte)0x00, b1[1]);
            Assert.AreEqual((byte)0x03, b1[2]);
            Assert.AreEqual((byte)0x00, b1[3]);
            Assert.AreEqual((byte)0x00, b1[4]);
            Assert.AreEqual((byte)0x00, b1[5]);
            Assert.AreEqual((byte)0x00, b1[6]);
            Assert.AreEqual((byte)0x00, b1[7]);

            // Check the contents of the 3rd block
            Assert.AreEqual((byte)0x6D, b2[0]);
            Assert.AreEqual((byte)0x00, b2[1]);
            Assert.AreEqual((byte)0x00, b2[2]);
            Assert.AreEqual((byte)0x00, b2[3]);
            Assert.AreEqual((byte)0x03, b2[4]);
            Assert.AreEqual((byte)0x00, b2[5]);
            Assert.AreEqual((byte)0x46, b2[6]);
            Assert.AreEqual((byte)0x00, b2[7]);

            fs.Close();
        }
コード例 #11
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadLongerStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));

            ByteBuffer b0 = null;
            ByteBuffer b1 = null;
            ByteBuffer b22 = null;

            // The stream at 0 has 23 blocks in it
            NPOIFSStream stream = new NPOIFSStream(fs, 0);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();

            int count = 0;
            while (i.MoveNext())
            {
                ByteBuffer b = i.Current;
                if (count == 0)
                {
                    b0 = b;
                }
                if (count == 1)
                {
                    b1 = b;
                }
                if (count == 22)
                {
                    b22 = b;
                }

                count++;
            }
            Assert.AreEqual(23, count);

            // Check the contents
            //  1st block is at 0
            Assert.AreEqual((byte)0x9e, b0[0]);
            Assert.AreEqual((byte)0x75, b0[1]);
            Assert.AreEqual((byte)0x97, b0[2]);
            Assert.AreEqual((byte)0xf6, b0[3]);

            //  2nd block is at 1
            Assert.AreEqual((byte)0x86, b1[0]);
            Assert.AreEqual((byte)0x09, b1[1]);
            Assert.AreEqual((byte)0x22, b1[2]);
            Assert.AreEqual((byte)0xfb, b1[3]);

            //  last block is at 89
            Assert.AreEqual((byte)0xfe, b22[0]);
            Assert.AreEqual((byte)0xff, b22[1]);
            Assert.AreEqual((byte)0x00, b22[2]);
            Assert.AreEqual((byte)0x00, b22[3]);
            Assert.AreEqual((byte)0x05, b22[4]);
            Assert.AreEqual((byte)0x01, b22[5]);
            Assert.AreEqual((byte)0x02, b22[6]);
            Assert.AreEqual((byte)0x00, b22[7]);

            fs.Close();
        }
コード例 #12
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestWriteThenReplace()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem();

            // Starts empty
            BATBlock bat = fs.GetBATBlockAndIndex(0).Block;
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(2));

            // Write something that uses a main stream
            byte[] main4106 = new byte[4106];
            main4106[0] = unchecked((byte)-10);
            main4106[4105] = unchecked((byte)-11);
            DocumentEntry normal = fs.Root.CreateDocument(
                    "Normal", new MemoryStream(main4106));

            // Should have used 9 blocks
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(3, bat.GetValueAt(2));
            Assert.AreEqual(4, bat.GetValueAt(3));
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(10, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(11));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4106, normal.Size);
            Assert.AreEqual(4106, ((DocumentNode)normal).Property.Size);


            // Replace with one still big enough for a main stream, but one block smaller
            byte[] main4096 = new byte[4096];
            main4096[0] = unchecked((byte)-10);
            main4096[4095] = unchecked((byte)-11);

            NDocumentOutputStream nout = new NDocumentOutputStream(normal);
            nout.Write(main4096);
            nout.Close();

            // Will have dropped to 8
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(3, bat.GetValueAt(2));
            Assert.AreEqual(4, bat.GetValueAt(3));
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(10));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(11));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4096, normal.Size);
            Assert.AreEqual(4096, ((DocumentNode)normal).Property.Size);


            // Write and check
            fs = TestNPOIFSFileSystem.WriteOutAndReadBack(fs);
            bat = fs.GetBATBlockAndIndex(0).Block;

            // Will have properties, but otherwise the same
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(3, bat.GetValueAt(2));
            Assert.AreEqual(4, bat.GetValueAt(3));
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(9)); // End of Normal
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10)); // Props
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(11));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4096, normal.Size);
            Assert.AreEqual(4096, ((DocumentNode)normal).Property.Size);


            // Make longer, take 1 block After the properties too
            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            nout = new NDocumentOutputStream(normal);
            nout.Write(main4106);
            nout.Close();

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(3, bat.GetValueAt(2));
            Assert.AreEqual(4, bat.GetValueAt(3));
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(11, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10)); // Props
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(11)); // Normal
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(12));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4106, normal.Size);
            Assert.AreEqual(4106, ((DocumentNode)normal).Property.Size);


            // Make it small, will trigger the SBAT stream and free lots up
            byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            nout = new NDocumentOutputStream(normal);
            nout.Write(mini);
            nout.Close();

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(2)); // SBAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(3)); // Mini Stream
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(4));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(5));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(6));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(7));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(8));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10)); // Props
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(11));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(12));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(7, normal.Size);
            Assert.AreEqual(7, ((DocumentNode)normal).Property.Size);


            // Finally back to big again
            nout = new NDocumentOutputStream(normal);
            nout.Write(main4096);
            nout.Close();

            // Will keep the mini stream, now empty
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(2)); // SBAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(3)); // Mini Stream
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(11, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10)); // Props
            Assert.AreEqual(12, bat.GetValueAt(11));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(12));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(13));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4096, normal.Size);
            Assert.AreEqual(4096, ((DocumentNode)normal).Property.Size);


            // Save, re-load, re-check
            fs = TestNPOIFSFileSystem.WriteOutAndReadBack(fs);
            bat = fs.GetBATBlockAndIndex(0).Block;

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(2)); // SBAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(3)); // Mini Stream
            Assert.AreEqual(5, bat.GetValueAt(4));
            Assert.AreEqual(6, bat.GetValueAt(5));
            Assert.AreEqual(7, bat.GetValueAt(6));
            Assert.AreEqual(8, bat.GetValueAt(7));
            Assert.AreEqual(9, bat.GetValueAt(8));
            Assert.AreEqual(11, bat.GetValueAt(9));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(10)); // Props
            Assert.AreEqual(12, bat.GetValueAt(11));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(12));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(13));

            normal = (DocumentEntry)fs.Root.GetEntry("Normal");
            Assert.AreEqual(4096, normal.Size);
            Assert.AreEqual(4096, ((DocumentNode)normal).Property.Size);

            fs.Close();
        }
コード例 #13
0
ファイル: TestHSSFWorkbook.cs プロジェクト: age-soft/npoi
        public void WriteWorkbookFromNPOIFS()
        {
            Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("WithEmbeddedObjects.xls");
            try
            {
                NPOIFSFileSystem fs = new NPOIFSFileSystem(is1);
                try
                {
                    // Start as NPOIFS
                    HSSFWorkbook wb = new HSSFWorkbook(fs.Root, true);
                    Assert.AreEqual(3, wb.NumberOfSheets);
                    Assert.AreEqual("Root xls", wb.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue);

                    // Will switch to POIFS
                    wb = HSSFTestDataSamples.WriteOutAndReadBack(wb);
                    Assert.AreEqual(3, wb.NumberOfSheets);
                    Assert.AreEqual("Root xls", wb.GetSheetAt(0).GetRow(0).GetCell(0).StringCellValue);
                }
                finally
                {
                    fs.Close();
                }
            }
            finally
            {
                is1.Close();
            }
        }
コード例 #14
0
        public void TestCreateBlockIfNeeded()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore ministore = fs.GetMiniStore();

            // 178 -> 179 -> 180, 181+ is free
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180));
            for (int i = 181; i < 256; i++)
            {
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i));
            }

            // However, the ministore data only covers blocks to 183
            for (int i = 0; i <= 183; i++)
            {
                ministore.GetBlockAt(i);
            }
            try
            {
                ministore.GetBlockAt(184);
                Assert.Fail("No block at 184");
            }
            catch (IndexOutOfRangeException) { }

            // The ministore itself is made up of 23 big blocks
            IEnumerator<ByteBuffer> it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator();
            int count = 0;

            while (it.MoveNext())
            {
                count++;
                //it.MoveNext();
            }
            Assert.AreEqual(23, count);

            // Ask it to get block 184 with creating, it will do
            ministore.CreateBlockIfNeeded(184);

            // The ministore should be one big block bigger now
            it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator();
            count = 0;
            while (it.MoveNext())
            {
                count++;
                //it.MoveNext();
            }
            Assert.AreEqual(24, count);

            // The mini block block counts now run to 191
            for (int i = 0; i <= 191; i++)
            {
                ministore.GetBlockAt(i);
            }
            try
            {
                ministore.GetBlockAt(192);
                Assert.Fail("No block at 192");
            }
            catch (IndexOutOfRangeException) { }


            // Now try writing through to 192, check that the SBAT and blocks are there
            byte[] data = new byte[15 * 64];
            NPOIFSStream stream = new NPOIFSStream(ministore, 178);
            stream.UpdateContents(data);

            // Check now
            Assert.AreEqual(179, ministore.GetNextBlock(178));
            Assert.AreEqual(180, ministore.GetNextBlock(179));
            Assert.AreEqual(181, ministore.GetNextBlock(180));
            Assert.AreEqual(182, ministore.GetNextBlock(181));
            Assert.AreEqual(183, ministore.GetNextBlock(182));
            Assert.AreEqual(184, ministore.GetNextBlock(183));
            Assert.AreEqual(185, ministore.GetNextBlock(184));
            Assert.AreEqual(186, ministore.GetNextBlock(185));
            Assert.AreEqual(187, ministore.GetNextBlock(186));
            Assert.AreEqual(188, ministore.GetNextBlock(187));
            Assert.AreEqual(189, ministore.GetNextBlock(188));
            Assert.AreEqual(190, ministore.GetNextBlock(189));
            Assert.AreEqual(191, ministore.GetNextBlock(190));
            Assert.AreEqual(192, ministore.GetNextBlock(191));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(192));
            for (int i = 193; i < 256; i++)
            {
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i));
            }

            fs.Close();
        }
コード例 #15
0
        public void TestGetFreeBlockWithNonSpare()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore ministore = fs.GetMiniStore();

            // We've spare ones from 181 to 255
            for (int i = 181; i < 256; i++)
            {
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i));
            }

            // Check our SBAT free stuff is correct
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);

            // Allocate all the spare ones
            for (int i = 181; i < 256; i++)
            {
                ministore.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
            }

            // SBAT are now full, but there's only the two
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors);
                Assert.Fail("Should only be two SBATs");
            }
            catch (ArgumentOutOfRangeException) { }

            // Now ask for a free one, will need to extend the SBAT chain
            Assert.AreEqual(256, ministore.GetFreeBlock());

            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors);
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(254)); // 2nd SBAT 
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(255)); // 2nd SBAT
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(256)); // 3rd SBAT
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(257)); // 3rd SBAT

            fs.Close();
        }
コード例 #16
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestWriteStream4096()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi"));

            // 0 -> 1 -> 2 -> end
            Assert.AreEqual(1, fs.GetNextBlock(0));
            Assert.AreEqual(2, fs.GetNextBlock(1));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2));
            Assert.AreEqual(4, fs.GetNextBlock(3));

            // First free one is at 15
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(14));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(15));


            // Write a 5 block file 
            byte[] data = new byte[4096 * 5];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }
            NPOIFSStream stream = new NPOIFSStream(fs, 0);
            stream.UpdateContents(data);


            // Check it
            Assert.AreEqual(1, fs.GetNextBlock(0));
            Assert.AreEqual(2, fs.GetNextBlock(1));
            Assert.AreEqual(15, fs.GetNextBlock(2)); // Jumps
            Assert.AreEqual(4, fs.GetNextBlock(3));  // Next stream
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(14));
            Assert.AreEqual(16, fs.GetNextBlock(15)); // Continues
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(16)); // Ends
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(17)); // Free

            // Check the contents too
            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(5, count);

            fs.Close();
        }
コード例 #17
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadFailsOnLoop()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));

            // Hack the FAT so that it goes 0->1->2->0
            fs.SetNextBlock(0, 1);
            fs.SetNextBlock(1, 2);
            fs.SetNextBlock(2, 0);

            // Now try to read
            NPOIFSStream stream = new NPOIFSStream(fs, 0);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();
            //1st read works
            Assert.AreEqual(true, i.MoveNext());

            // 1st read works
          //  i.MoveNext();
            // 2nd read works
            Assert.AreEqual(true, i.MoveNext());

            
          // i.MoveNext();
          //  Assert.AreEqual(true, i.MoveNext());

            // 3rd read works
            //i.MoveNext();
            Assert.AreEqual(true, i.MoveNext());

            // 4th read blows up as it loops back to 0
            try
            {
                i.MoveNext();
                Assert.Fail("Loop should have been detected but wasn't!");
            }
            catch (Exception)
            {
                // Good, it was detected
            }
            //Assert.AreEqual(true, i.MoveNext());

            fs.Close();
        }
コード例 #18
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadShortStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));

            // 97 -> 98 -> end
            NPOIFSStream stream = new NPOIFSStream(fs, 97);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();

            Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b97 = i.Current;
            Assert.AreEqual(true, i.MoveNext());

            //i.MoveNext();
            ByteBuffer b98 = i.Current;
            Assert.AreEqual(false, i.MoveNext());

            // Check the contents of the 1st block
            Assert.AreEqual((byte)0x01, b97[0]);
            Assert.AreEqual((byte)0x00, b97[1]);
            Assert.AreEqual((byte)0x00, b97[2]);
            Assert.AreEqual((byte)0x00, b97[3]);
            Assert.AreEqual((byte)0x02, b97[4]);
            Assert.AreEqual((byte)0x00, b97[5]);
            Assert.AreEqual((byte)0x00, b97[6]);
            Assert.AreEqual((byte)0x00, b97[7]);

            // Check the contents of the 2nd block
            Assert.AreEqual((byte)0x81, b98[0]);
            Assert.AreEqual((byte)0x00, b98[1]);
            Assert.AreEqual((byte)0x00, b98[2]);
            Assert.AreEqual((byte)0x00, b98[3]);
            Assert.AreEqual((byte)0x82, b98[4]);
            Assert.AreEqual((byte)0x00, b98[5]);
            Assert.AreEqual((byte)0x00, b98[6]);
            Assert.AreEqual((byte)0x00, b98[7]);

            fs.Close();
        }
コード例 #19
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadMiniStreams()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore ministore = fs.GetMiniStore();

            // 178 -> 179 -> 180 -> end
            NPOIFSStream stream = new NPOIFSStream(ministore, 178);
            IEnumerator<ByteBuffer> i = stream.GetBlockIterator();
            Assert.AreEqual(true, i.MoveNext());
          //  Assert.AreEqual(true, i.MoveNext());
          //  Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b178 = i.Current;
            Assert.AreEqual(true, i.MoveNext());
           // Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b179 = i.Current;
            Assert.AreEqual(true, i.MoveNext());

           // i.MoveNext();
            ByteBuffer b180 = i.Current;
            Assert.AreEqual(false, i.MoveNext());
            Assert.AreEqual(false, i.MoveNext());
           // Assert.AreEqual(false, i.MoveNext());

            // Check the contents of the 1st block
            Assert.AreEqual((byte)0xfe, b178[0]);
            Assert.AreEqual((byte)0xff, b178[1]);
            Assert.AreEqual((byte)0x00, b178[2]);
            Assert.AreEqual((byte)0x00, b178[3]);
            Assert.AreEqual((byte)0x05, b178[4]);
            Assert.AreEqual((byte)0x01, b178[5]);
            Assert.AreEqual((byte)0x02, b178[6]);
            Assert.AreEqual((byte)0x00, b178[7]);

            // And the 2nd
            Assert.AreEqual((byte)0x6c, b179[0]);
            Assert.AreEqual((byte)0x00, b179[1]);
            Assert.AreEqual((byte)0x00, b179[2]);
            Assert.AreEqual((byte)0x00, b179[3]);
            Assert.AreEqual((byte)0x28, b179[4]);
            Assert.AreEqual((byte)0x00, b179[5]);
            Assert.AreEqual((byte)0x00, b179[6]);
            Assert.AreEqual((byte)0x00, b179[7]);

            // And the 3rd
            Assert.AreEqual((byte)0x30, b180[0]);
            Assert.AreEqual((byte)0x00, b180[1]);
            Assert.AreEqual((byte)0x00, b180[2]);
            Assert.AreEqual((byte)0x00, b180[3]);
            Assert.AreEqual((byte)0x00, b180[4]);
            Assert.AreEqual((byte)0x00, b180[5]);
            Assert.AreEqual((byte)0x00, b180[6]);
            Assert.AreEqual((byte)0x80, b180[7]);

            fs.Close();
        }
コード例 #20
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReadWriteNewStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem();
            NPOIFSStream stream = new NPOIFSStream(fs);

            // Check our filesystem has a BAT and the Properties
            Assert.AreEqual(2, fs.GetFreeBlock());
            BATBlock bat = fs.GetBATBlockAndIndex(0).Block;
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(2));

            // Check the stream as-is
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, stream.GetStartBlock());
            try
            {
                stream.GetBlockIterator();
                Assert.Fail("Shouldn't be able to get an iterator before writing");
            }
            catch (Exception) { }

            // Write in two blocks
            byte[] data = new byte[512 + 20];
            for (int i = 0; i < 512; i++)
            {
                data[i] = (byte)(i % 256);
            }
            for (int i = 512; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256 + 100);
            }
            stream.UpdateContents(data);

            // Check now
            Assert.AreEqual(4, fs.GetFreeBlock());
            bat = fs.GetBATBlockAndIndex(0).Block;
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1));
            Assert.AreEqual(3, bat.GetValueAt(2));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(3));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(4));


            IEnumerator<ByteBuffer> it = stream.GetBlockIterator();

            Assert.AreEqual(true, it.MoveNext());
            ByteBuffer b = it.Current;

            byte[] read = new byte[512];
            //b.get(read);
           // Array.Copy(b, 0, read, 0, b.Length);
            b.Read(read);
            for (int i = 0; i < read.Length; i++)
            {
                //Assert.AreEqual("Wrong value at " + i, data[i], read[i]);
                Assert.AreEqual(data[i], read[i], "Wrong value at " + i);
            }

            Assert.AreEqual(true, it.MoveNext());
            b = it.Current;

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

            Assert.AreEqual(false, it.MoveNext());

            fs.Close();
        }
コード例 #21
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReplaceStream()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            byte[] data = new byte[512];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            // 98 is actually the last block in a two block stream...
            NPOIFSStream stream = new NPOIFSStream(fs, 98);
            stream.UpdateContents(data);

            // Check the reading of blocks
            IEnumerator<ByteBuffer> it = stream.GetBlockIterator();

            Assert.AreEqual(true, it.MoveNext());

          //  it.MoveNext();
            ByteBuffer b = it.Current;
            Assert.AreEqual(false, it.MoveNext());

            // Now check the contents
            data = new byte[512];
            b.Read(data);
            for (int i = 0; i < data.Length; i++)
            {
                byte exp = (byte)(i % 256);
                Assert.AreEqual(exp, data[i]);
            }

            fs.Close();
        }
コード例 #22
0
        public void TestReadMultipleTreeLevels()
        {
            POIDataSamples _samples = POIDataSamples.GetPublisherInstance();
            FileStream sample = _samples.GetFile("Sample.pub");

            DocumentInputStream stream;

            NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample);

            try
            {
                sample = _samples.GetFile("Sample.pub");
                POIFSFileSystem opoifs = new POIFSFileSystem(sample);

                // Ensure we have what we expect on the root
                Assert.AreEqual(npoifs, npoifs.Root.NFileSystem);
                Assert.AreEqual(null, npoifs.Root.FileSystem);
                Assert.AreEqual(opoifs, opoifs.Root.FileSystem);
                Assert.AreEqual(null, opoifs.Root.NFileSystem);

                // Check inside
                foreach (DirectoryNode root in new DirectoryNode[] { opoifs.Root, npoifs.Root })
                {
                    // Top Level
                    Entry top = root.GetEntry("Contents");
                    Assert.AreEqual(true, top.IsDocumentEntry);
                    stream = root.CreateDocumentInputStream(top);
                    stream.Read();

                    // One Level Down
                    DirectoryNode escher = (DirectoryNode)root.GetEntry("Escher");
                    Entry one = escher.GetEntry("EscherStm");
                    Assert.AreEqual(true, one.IsDocumentEntry);
                    stream = escher.CreateDocumentInputStream(one);
                    stream.Read();

                    // Two Levels Down
                    DirectoryNode quill = (DirectoryNode)root.GetEntry("Quill");
                    DirectoryNode quillSub = (DirectoryNode)quill.GetEntry("QuillSub");
                    Entry two = quillSub.GetEntry("CONTENTS");
                    Assert.AreEqual(true, two.IsDocumentEntry);
                    stream = quillSub.CreateDocumentInputStream(two);
                    stream.Read();
                }
            }
            finally
            {
                npoifs.Close();
            }
        }
コード例 #23
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReplaceStreamWithLess()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            byte[] data = new byte[512];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }

            // 97 -> 98 -> end
            Assert.AreEqual(98, fs.GetNextBlock(97));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98));

            // Create a 2 block stream, will become a 1 block one
            NPOIFSStream stream = new NPOIFSStream(fs, 97);
            stream.UpdateContents(data);

            // 97 should now be the end, and 98 free
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(97));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(98));

            // Check the reading of blocks
            IEnumerator<ByteBuffer> it = stream.GetBlockIterator();

            Assert.AreEqual(true, it.MoveNext());
            ByteBuffer b = it.Current;

            Assert.AreEqual(false, it.MoveNext());

            // Now check the contents
            data = new byte[512];
           // b.get(data);
            //for (int i = 0; i < b.Length; i++)
            //    data[i] = b[i];
            //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]);
            }

            fs.Close();
        }
コード例 #24
0
        public void NPOIFSReadCopyWritePOIFSRead()
        {
            FileStream testFile = POIDataSamples.GetSpreadSheetInstance().GetFile("Simple.xls");
            NPOIFSFileSystem src = new NPOIFSFileSystem(testFile);
            byte[] wbDataExp = IOUtils.ToByteArray(src.CreateDocumentInputStream("Workbook"));

            NPOIFSFileSystem nfs = new NPOIFSFileSystem();
            EntryUtils.CopyNodes(src.Root, nfs.Root);
            src.Close();

            MemoryStream bos = new MemoryStream();
            nfs.WriteFileSystem(bos);
            nfs.Close();

            POIFSFileSystem pfs = new POIFSFileSystem(new MemoryStream(bos.ToArray()));
            byte[] wbDataAct = IOUtils.ToByteArray(pfs.CreateDocumentInputStream("Workbook"));

            Assert.That(wbDataExp, new EqualConstraint(wbDataAct));
        }
コード例 #25
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestReplaceStreamWithMore()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

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

            // 97 -> 98 -> end
            Assert.AreEqual(98, fs.GetNextBlock(97));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98));

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

            // Create a 2 block stream, will become a 3 block one
            NPOIFSStream stream = new NPOIFSStream(fs, 97);
            stream.UpdateContents(data);

            // 97 -> 98 -> 100 -> end
            Assert.AreEqual(98, fs.GetNextBlock(97));
            Assert.AreEqual(100, fs.GetNextBlock(98));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(100));

            // Check the reading of blocks
            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(3, count);

            fs.Close();
        }
コード例 #26
0
        public void TestGetFreeBlockWithNoneSpare()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            int free;

            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            assertBATCount(fs, 1, 0);
            for (int i = 100; i < 128; i++)
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(i));

            Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);

            for (int i = 100; i < 128; i++)
                fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);

            Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);

            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
                Assert.Fail("Should only be one BAT");
            }
            //catch (IndexOutOfRangeException)
            catch (ArgumentOutOfRangeException)
            {
            }
            assertBATCount(fs, 1, 0);

            // Now ask for a free one, will need to extend the file

            Assert.AreEqual(129, fs.GetFreeBlock());

            Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(129));

            // We now have 2 BATs, but no XBATs
            assertBATCount(fs, 2, 0);

            // Fill up to hold 109 BAT blocks
            for (int i = 0; i < 109; i++)
            {
                fs.GetFreeBlock();
                int startOffset = i * 128;
                while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
                {
                    free = fs.GetFreeBlock();
                    fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
                }
            }

            Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128).Block.HasFreeSectors);
                Assert.Fail("Should only be 109 BATs");
            }
            // catch (IndexOutOfRangeException)
            catch (ArgumentOutOfRangeException)
            {
            }

            // We now have 109 BATs, but no XBATs
            assertBATCount(fs, 109, 0);


            // Ask for it to be written out, and check the header
            HeaderBlock header = WriteOutAndReadHeader(fs);
            Assert.AreEqual(109, header.BATCount);
            Assert.AreEqual(0, header.XBATCount);

            free = fs.GetFreeBlock();
            Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors);
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(110 * 128 - 1).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(110 * 128).Block.HasFreeSectors);
                Assert.Fail("Should only be 110 BATs");
            }
            //catch (IndexOutOfRangeException)
            catch (ArgumentOutOfRangeException)
            {
            }

            assertBATCount(fs, 110, 1);

            header = WriteOutAndReadHeader(fs);
            Assert.AreEqual(110, header.BATCount);
            Assert.AreEqual(1, header.XBATCount);

            for (int i = 109; i < 109 + 127; i++)
            {
                fs.GetFreeBlock();
                int startOffset = i * 128;
                while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors)
                {
                    free = fs.GetFreeBlock();
                    fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN);
                }

                assertBATCount(fs, i + 1, 1);
            }

            // Should now have 109+127 = 236 BATs
            Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128).Block.HasFreeSectors);
                Assert.Fail("Should only be 236 BATs");
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            assertBATCount(fs, 236, 1);

            // Ask for another, will get our 2nd XBAT
            free = fs.GetFreeBlock();
            Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors);
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(237 * 128 - 1).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(237 * 128).Block.HasFreeSectors);
                Assert.Fail("Should only be 237 BATs");
            }
            // catch (IndexOutOfRangeException) { }
            catch (ArgumentOutOfRangeException)
            {
            }

            // Check the counts now
            assertBATCount(fs, 237, 2);

            // Check the header
            header = WriteOutAndReadHeader(fs);


            // Now, write it out, and read it back in again fully
            fs = WriteOutAndReadBack(fs);

            
            // Check that it is seen correctly
            assertBATCount(fs, 237, 2);

            Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors);
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(237 * 128 - 1).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, fs.GetBATBlockAndIndex(237 * 128).Block.HasFreeSectors);
                Assert.Fail("Should only be 237 BATs");
            }
            catch (ArgumentOutOfRangeException) { }

            fs.Close();
        }
コード例 #27
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        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();
        }
コード例 #28
0
 public static NPOIFSFileSystem WriteOutAndReadBack(NPOIFSFileSystem original)
 {
     MemoryStream baos = new MemoryStream();
     original.WriteFileSystem(baos);
     original.Close();
     return new NPOIFSFileSystem(new ByteArrayInputStream(baos.ToArray()));
 }
コード例 #29
0
ファイル: TestNPOIFSStream.cs プロジェクト: Reinakumiko/npoi
        public void TestWriteNewStreamExtraFATs()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));

            // Allocate almost all the blocks
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(127));
            for (int i = 100; i < 127; i++)
            {
                fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
            }
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(127));
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);


            // Write a 3 block stream
            byte[] data = new byte[512 * 3];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)(i % 256);
            }
            NPOIFSStream stream = new NPOIFSStream(fs);
            stream.UpdateContents(data);

            // Check we got another BAT
            Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors);

            // the BAT will be in the first spot of the new block
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(126));
            Assert.AreEqual(129, fs.GetNextBlock(127));
            Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128));
            Assert.AreEqual(130, fs.GetNextBlock(129));
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(130));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(131));

            fs.Close();
        }
コード例 #30
0
ファイル: DocumentFactoryHelper.cs プロジェクト: zzy092/npoi
 public override void Close()
 {
     fs.Close();
     base.Close();
 }
コード例 #31
0
        public void TestGetFreeBlockWithSpare()
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            NPOIFSMiniStore ministore = fs.GetMiniStore();

            // Our 2nd SBAT block has spares
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);

            // First free one at 181
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(181));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(182));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(183));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(184));

            // Ask, will get 181
            Assert.AreEqual(181, ministore.GetFreeBlock());

            // Ask again, will still get 181 as not written to
            Assert.AreEqual(181, ministore.GetFreeBlock());

            // Allocate it, then ask again
            ministore.SetNextBlock(181, POIFSConstants.END_OF_CHAIN);
            Assert.AreEqual(182, ministore.GetFreeBlock());

            fs.Close();
        }