Beispiel #1
0
        /**
         * Set the contents of a document, creating if needed,
         *  otherwise updating. Returns the created / updated DocumentEntry
         *
         * @param name the name of the new or existing DocumentEntry
         * @param stream the InputStream from which to populate the DocumentEntry
         *
         * @return the new or updated DocumentEntry
         *
         * @exception IOException
         */
        public DocumentEntry CreateOrUpdateDocument(String name,
                                                    Stream stream)

        {
            if (!HasEntry(name))
            {
                return(CreateDocument(name, stream));
            }
            else
            {
                DocumentNode existing = (DocumentNode)GetEntry(name);
                if (_nFilesSystem != null)
                {
                    NPOIFSDocument nDoc = new NPOIFSDocument(existing);
                    nDoc.ReplaceContents(stream);
                    return(existing);
                }
                else
                {
                    // Do it the hard way for Old POIFS...
                    DeleteEntry(existing);
                    return(CreateDocument(name, stream));
                }
            }
        }
Beispiel #2
0
        public void Close()
        {
            // Do we have a pending buffer for the mini stream?
            if (_buffer != null)
            {
                // It's not much data, so ask NPOIFSDocument to do it for us
                _document.ReplaceContents(new MemoryStream(_buffer.ToArray()));
            }
            else
            {
                // We've been writing to the stream as we've gone along
                // Update the details on the property now
                _stream_output.Close();
                _property.UpdateSize(_document_size);
                _property.StartBlock = (_stream.GetStartBlock());
            }

            // No more!
            _closed = true;
        }
        public void ReadWriteRead()
        {
            SummaryInformation sinf = null;
            DocumentSummaryInformation dinf = null;
            DirectoryEntry root = null, testDir = null;
            NPOIFSFileSystem[] testFS = get512and4kFileAndInput();
            for (int i=0;i<testFS.Length;i++)
            {
                NPOIFSFileSystem fs = testFS[i];
                // Check we can find the entries we expect
                root = fs.Root;
                Assert.AreEqual(5, root.EntryCount);
                
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);

                // Check they're still there
                root = fs.Root;
                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check the contents of them - parse the summary block and check
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);


                // Add a test mini stream
                testDir = root.CreateDirectory("Testing 123");
                testDir.CreateDirectory("Testing 456");
                testDir.CreateDirectory("Testing 789");
                byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
                testDir.CreateDocument("Mini", new MemoryStream(mini));


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(6, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check old and new are there
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);

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


                // Write out and read once more, just to be sure
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(6, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);
                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));

                byte[] main4096 = new byte[4096];
                main4096[0] = unchecked((byte)-10);
                main4096[4095] = unchecked((byte)-11);
                testDir.CreateDocument("Normal4096", new MemoryStream(main4096));

                root.GetEntry("Tags").Delete();


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);

                // Check it's all there
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check old and new are there
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));


                // Delete a directory, and add one more
                testDir.GetEntry("Testing 456").Delete();
                testDir.CreateDirectory("Testing ABC");


                // Save
                fs = WriteOutAndReadBack(fs);

                // Check
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(4, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));


                // Add another mini stream
                byte[] mini2 = new byte[] { unchecked((byte)-42), 0, unchecked((byte)-1), 
                    unchecked((byte)-2), unchecked((byte)-3), unchecked((byte)-4), unchecked((byte)-42) };
                testDir.CreateDocument("Mini2", new MemoryStream(mini2));

                // Save, load, check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));
                assertContentsMatches(mini2, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));


                // Delete a mini stream, add one more
                testDir.GetEntry("Mini").Delete();

                byte[] mini3 = new byte[] { 42, 0, 42, 0, 42, 0, 42 };
                testDir.CreateDocument("Mini3", new MemoryStream(mini3));


                // Save, load, check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini3"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini2, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(mini3, (DocumentEntry)testDir.GetEntry("Mini3"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));

                // Change some existing streams
                NPOIFSDocument mini2Doc = new NPOIFSDocument((DocumentNode)testDir.GetEntry("Mini2"));
                mini2Doc.ReplaceContents(new MemoryStream(mini));

                byte[] main4106 = new byte[4106];
                main4106[0] = 41;
                main4106[4105] = 42;
                NPOIFSDocument mainDoc = new NPOIFSDocument((DocumentNode)testDir.GetEntry("Normal4096"));
                mainDoc.ReplaceContents(new MemoryStream(main4106));


                // Re-check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini3"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(mini3, (DocumentEntry)testDir.GetEntry("Mini3"));
                assertContentsMatches(main4106, (DocumentEntry)testDir.GetEntry("Normal4096"));
           


                fs.Close();
            }
        }