]> source.dussan.org Git - poi.git/commitdiff
#56791 More updates from OPOIFS to NPOIFS
authorNick Burch <nick@apache.org>
Mon, 11 May 2015 18:15:03 +0000 (18:15 +0000)
committerNick Burch <nick@apache.org>
Mon, 11 May 2015 18:15:03 +0000 (18:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1678787 13f79535-47bb-0310-9956-ffa450edef68

16 files changed:
src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java
src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java
src/java/org/apache/poi/poifs/filesystem/DocumentNode.java
src/java/org/apache/poi/poifs/filesystem/ODocumentInputStream.java
src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java [new file with mode: 0644]
src/java/org/apache/poi/poifs/filesystem/OPOIFSFileSystem.java
src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java [deleted file]
src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
src/java/org/apache/poi/poifs/property/DocumentProperty.java
src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java
src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java
src/testcases/org/apache/poi/poifs/storage/TestSmallBlockTableWriter.java

index 567527523e7cb0beddef4850145dd295c5b0682c..a288709185a83e3465bd2db86234951237881be6 100644 (file)
@@ -24,7 +24,7 @@ import java.io.*;
 import java.util.*;
 
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
-import org.apache.poi.poifs.filesystem.POIFSDocument;
+import org.apache.poi.poifs.filesystem.OPOIFSDocument;
 import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
 import org.apache.poi.poifs.property.DirectoryProperty;
 import org.apache.poi.poifs.property.Property;
@@ -240,19 +240,19 @@ public class POIFSReader
 
                 if (listeners.hasNext())
                 {
-                    int           size     = property.getSize();
-                    POIFSDocument document = null;
+                    int            size     = property.getSize();
+                    OPOIFSDocument document = null;
 
                     if (property.shouldUseSmallBlocks())
                     {
                         document =
-                            new POIFSDocument(name, small_blocks
+                            new OPOIFSDocument(name, small_blocks
                                 .fetchBlocks(startBlock, -1), size);
                     }
                     else
                     {
                         document =
-                            new POIFSDocument(name, big_blocks
+                            new OPOIFSDocument(name, big_blocks
                                 .fetchBlocks(startBlock, -1), size);
                     }
                     while (listeners.hasNext())
index c1d4a25b033b1f3b7fff7d9ba1768779225d99d2..e8c6638bf6d9fbd96617ef71a3972be7cc200531 100644 (file)
@@ -217,7 +217,7 @@ public class DirectoryNode
      *
      * @exception IOException
      */
-    DocumentEntry createDocument(final POIFSDocument document)
+    DocumentEntry createDocument(final OPOIFSDocument document)
         throws IOException
     {
         DocumentProperty property = document.getDocumentProperty();
@@ -418,7 +418,7 @@ public class DirectoryNode
         if(_nfilesystem != null) {
            return createDocument(new NPOIFSDocument(name, _nfilesystem, stream));
         } else {
-           return createDocument(new POIFSDocument(name, stream));
+           return createDocument(new OPOIFSDocument(name, stream));
         }
     }
 
@@ -441,7 +441,7 @@ public class DirectoryNode
         if(_nfilesystem != null) {
             return createDocument(new NPOIFSDocument(name, size, _nfilesystem, writer));
          } else {
-            return createDocument(new POIFSDocument(name, size, _path, writer));
+            return createDocument(new OPOIFSDocument(name, size, _path, writer));
          }
     }
 
index 6c6b6100c0e02ba3089bd610575fc2a06f995fc3..35d0c23655398c99e1f480901014b76f2db512e0 100644 (file)
@@ -72,7 +72,7 @@ public class DocumentInputStream extends InputStream implements LittleEndianInpu
         * 
         * @param document the Document to be read
         */
-       public DocumentInputStream(POIFSDocument document) {
+       public DocumentInputStream(OPOIFSDocument document) {
           delegate = new ODocumentInputStream(document);
        }
 
index 9caafb76688a99bdb7c435a1333a3985d6814f96..7a5bf690098612bc31a244d96ec9613c27cc6e07 100644 (file)
@@ -27,18 +27,15 @@ import org.apache.poi.poifs.dev.POIFSViewable;
 import org.apache.poi.poifs.property.DocumentProperty;
 
 /**
- * Simple implementation of DocumentEntry
- *
- * @author Marc Johnson (mjohnson at apache dot org)
+ * Simple implementation of DocumentEntry for OPOIFS
  */
-
 public class DocumentNode
     extends EntryNode
     implements DocumentEntry, POIFSViewable
 {
 
     // underlying POIFSDocument instance
-    private POIFSDocument _document;
+    private OPOIFSDocument _document;
 
     /**
      * create a DocumentNode. This method is not public by design; it
@@ -59,8 +56,7 @@ public class DocumentNode
      *
      * @return the internal POIFSDocument
      */
-
-    POIFSDocument getDocument()
+    OPOIFSDocument getDocument()
     {
         return _document;
     }
index a1bc5da95f78550e2dd74c79f2190ebba2a4e2a4..858e2d9d9b2514372f071f6bf4ff5edc6f889112 100644 (file)
@@ -23,9 +23,7 @@ import org.apache.poi.poifs.storage.DataInputBlock;
 
 /**
  * This class provides methods to read a DocumentEntry managed by a
- * {@link POIFSFileSystem} instance.
- *
- * @author Marc Johnson (mjohnson at apache dot org)
+ * {@link OPOIFSFileSystem} instance.
  */
 public final class ODocumentInputStream extends DocumentInputStream {
        /** current offset into the Document */
@@ -41,7 +39,7 @@ public final class ODocumentInputStream extends DocumentInputStream {
        private boolean _closed;
 
        /** the actual Document */
-       private POIFSDocument _document;
+       private OPOIFSDocument _document;
 
        /** the data block containing the current stream pointer */
        private DataInputBlock _currentBlock;
@@ -59,7 +57,7 @@ public final class ODocumentInputStream extends DocumentInputStream {
                        throw new IOException("Cannot open internal document storage");
                }
                DocumentNode documentNode = (DocumentNode)document;
-               if(documentNode.getDocument() == null) {
+               if (documentNode.getDocument() == null) {
          throw new IOException("Cannot open internal document storage");
                }
                      
@@ -76,7 +74,7 @@ public final class ODocumentInputStream extends DocumentInputStream {
         * 
         * @param document the Document to be read
         */
-       public ODocumentInputStream(POIFSDocument document) {
+       public ODocumentInputStream(OPOIFSDocument document) {
                _current_offset = 0;
                _marked_offset = 0;
                _document_size = document.getSize();
diff --git a/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java
new file mode 100644 (file)
index 0000000..3862b29
--- /dev/null
@@ -0,0 +1,567 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.poifs.filesystem;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.poi.poifs.common.POIFSBigBlockSize;
+import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.dev.POIFSViewable;
+import org.apache.poi.poifs.property.DocumentProperty;
+import org.apache.poi.poifs.property.Property;
+import org.apache.poi.poifs.storage.BlockWritable;
+import org.apache.poi.poifs.storage.DataInputBlock;
+import org.apache.poi.poifs.storage.DocumentBlock;
+import org.apache.poi.poifs.storage.ListManagedBlock;
+import org.apache.poi.poifs.storage.RawDataBlock;
+import org.apache.poi.poifs.storage.SmallDocumentBlock;
+import org.apache.poi.util.HexDump;
+
+/**
+ * This class manages a document in a old-style 
+ *  OPOIFS filesystem.
+ */
+public final class OPOIFSDocument implements BATManaged, BlockWritable, POIFSViewable  {
+       private static final DocumentBlock[] EMPTY_BIG_BLOCK_ARRAY = { };
+       private static final SmallDocumentBlock[] EMPTY_SMALL_BLOCK_ARRAY = { };
+       private DocumentProperty _property;
+       private int _size;
+       
+   private final POIFSBigBlockSize _bigBigBlockSize;
+
+       // one of these stores will be valid
+       private SmallBlockStore  _small_store;
+       private BigBlockStore    _big_store;
+       
+               /**
+        * Constructor from large blocks
+        *
+        * @param name the name of the POIFSDocument
+        * @param blocks the big blocks making up the POIFSDocument
+        * @param length the actual length of the POIFSDocument
+        */
+       public OPOIFSDocument(String name, RawDataBlock[] blocks, int length) throws IOException {
+               _size = length;
+               if(blocks.length == 0) {
+                  _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
+               } else {
+                  _bigBigBlockSize = (blocks[0].getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ?
+                        POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS : 
+                        POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS
+                  );
+               }
+               
+               _big_store = new BigBlockStore(_bigBigBlockSize, convertRawBlocksToBigBlocks(blocks));
+               _property = new DocumentProperty(name, _size);
+               _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
+               _property.setDocument(this);
+       }
+
+       // TODO - awkward typing going on here
+       private static DocumentBlock[] convertRawBlocksToBigBlocks(ListManagedBlock[] blocks) throws IOException {
+               DocumentBlock[] result = new DocumentBlock[blocks.length];
+               for (int i = 0; i < result.length; i++) {
+                       result[i] = new DocumentBlock((RawDataBlock)blocks[i]);
+               }
+               return result;
+       }
+       private static SmallDocumentBlock[] convertRawBlocksToSmallBlocks(ListManagedBlock[] blocks) {
+               if (blocks instanceof SmallDocumentBlock[]) {
+                       return (SmallDocumentBlock[]) blocks;
+               }
+               SmallDocumentBlock[] result = new SmallDocumentBlock[blocks.length];
+               System.arraycopy(blocks, 0, result, 0, blocks.length);
+               return result;
+       }
+
+       /**
+        * Constructor from small blocks
+        *
+        * @param name the name of the POIFSDocument
+        * @param blocks the small blocks making up the POIFSDocument
+        * @param length the actual length of the POIFSDocument
+        */
+       public OPOIFSDocument(String name, SmallDocumentBlock[] blocks, int length) {
+               _size = length;
+               
+               if(blocks.length == 0) {
+                  _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
+               } else {
+                  _bigBigBlockSize = blocks[0].getBigBlockSize();
+               }
+
+               _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
+               _property = new DocumentProperty(name, _size);
+               _small_store = new SmallBlockStore(_bigBigBlockSize, blocks);
+               _property.setDocument(this);
+       }
+
+       /**
+        * Constructor from small blocks
+        *
+        * @param name the name of the POIFSDocument
+        * @param blocks the small blocks making up the POIFSDocument
+        * @param length the actual length of the POIFSDocument
+        */
+       public OPOIFSDocument(String name, POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks, int length) throws IOException {
+               _size = length;
+               _bigBigBlockSize = bigBlockSize;
+               _property = new DocumentProperty(name, _size);
+               _property.setDocument(this);
+               if (Property.isSmall(_size)) {
+                       _big_store = new BigBlockStore(bigBlockSize,EMPTY_BIG_BLOCK_ARRAY);
+                       _small_store = new SmallBlockStore(bigBlockSize,convertRawBlocksToSmallBlocks(blocks));
+               } else {
+                       _big_store = new BigBlockStore(bigBlockSize,convertRawBlocksToBigBlocks(blocks));
+                       _small_store = new SmallBlockStore(bigBlockSize,EMPTY_SMALL_BLOCK_ARRAY);
+               }
+       }
+       public OPOIFSDocument(String name, ListManagedBlock[] blocks, int length) throws IOException {
+          this(name, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blocks, length);
+       }
+
+       /**
+        * Constructor
+        *
+        * @param name the name of the POIFSDocument
+        * @param stream the InputStream we read data from
+        */
+       public OPOIFSDocument(String name, POIFSBigBlockSize bigBlockSize, InputStream stream) throws IOException {
+               List<DocumentBlock> blocks = new ArrayList<DocumentBlock>();
+
+               _size = 0;
+               _bigBigBlockSize = bigBlockSize;
+               while (true) {
+                       DocumentBlock block = new DocumentBlock(stream, bigBlockSize);
+                       int blockSize = block.size();
+
+                       if (blockSize > 0) {
+                               blocks.add(block);
+                               _size += blockSize;
+                       }
+                       if (block.partiallyRead()) {
+                               break;
+                       }
+               }
+               DocumentBlock[] bigBlocks = blocks.toArray(new DocumentBlock[blocks.size()]);
+
+               _big_store = new BigBlockStore(bigBlockSize,bigBlocks);
+               _property = new DocumentProperty(name, _size);
+               _property.setDocument(this);
+               if (_property.shouldUseSmallBlocks()) {
+                       _small_store = new SmallBlockStore(bigBlockSize,SmallDocumentBlock.convert(bigBlockSize,bigBlocks, _size));
+                       _big_store = new BigBlockStore(bigBlockSize,new DocumentBlock[0]);
+               } else {
+                       _small_store = new SmallBlockStore(bigBlockSize,EMPTY_SMALL_BLOCK_ARRAY);
+               }
+       }
+       public OPOIFSDocument(String name, InputStream stream) throws IOException {
+          this(name, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, stream);
+       }
+
+       /**
+        * Constructor
+        *
+        * @param name the name of the POIFSDocument
+        * @param size the length of the POIFSDocument
+        * @param path the path of the POIFSDocument
+        * @param writer the writer who will eventually write the document contents
+        */
+       public OPOIFSDocument(String name, int size, POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, POIFSWriterListener writer) {
+               _size = size;
+               _bigBigBlockSize = bigBlockSize;
+               _property = new DocumentProperty(name, _size);
+               _property.setDocument(this);
+               if (_property.shouldUseSmallBlocks()) {
+                       _small_store = new SmallBlockStore(_bigBigBlockSize, path, name, size, writer);
+                       _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
+               } else {
+                       _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
+                       _big_store = new BigBlockStore(_bigBigBlockSize, path, name, size, writer);
+               }
+       }
+       public OPOIFSDocument(String name, int size, POIFSDocumentPath path, POIFSWriterListener writer) {
+          this(name, size, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, path, writer);
+       }
+
+       /**
+        * @return array of SmallDocumentBlocks; may be empty, cannot be null
+        */
+       public BlockWritable[] getSmallBlocks() {
+               return _small_store.getBlocks();
+       }
+
+       /**
+        * @return size of the document
+        */
+       public int getSize() {
+               return _size;
+       }
+
+       /**
+        * read data from the internal stores
+        *
+        * @param buffer the buffer to write to
+        * @param offset the offset into our storage to read from
+        * This method is currently (Oct 2008) only used by test code. Perhaps it can be deleted
+        */
+       void read(byte[] buffer, int offset) {
+               int len = buffer.length;
+
+               DataInputBlock currentBlock = getDataInputBlock(offset);
+
+               int blockAvailable = currentBlock.available();
+               if (blockAvailable > len) {
+                       currentBlock.readFully(buffer, 0, len);
+                       return;
+               }
+               // else read big amount in chunks
+               int remaining = len;
+               int writePos = 0;
+               int currentOffset = offset;
+               while (remaining > 0) {
+                       boolean blockIsExpiring = remaining >= blockAvailable;
+                       int reqSize;
+                       if (blockIsExpiring) {
+                               reqSize = blockAvailable;
+                       } else {
+                               reqSize = remaining;
+                       }
+                       currentBlock.readFully(buffer, writePos, reqSize);
+                       remaining-=reqSize;
+                       writePos+=reqSize;
+                       currentOffset += reqSize;
+                       if (blockIsExpiring) {
+                               if (currentOffset == _size) {
+                                       if (remaining > 0) {
+                                               throw new IllegalStateException("reached end of document stream unexpectedly");
+                                       }
+                                       currentBlock = null;
+                                       break;
+                               }
+                               currentBlock = getDataInputBlock(currentOffset);
+                               blockAvailable = currentBlock.available();
+                       }
+               }
+       }
+
+       /**
+        * @return <code>null</code> if <tt>offset</tt> points to the end of the document stream
+        */
+       DataInputBlock getDataInputBlock(int offset) {
+               if (offset >= _size) {
+                       if (offset > _size) {
+                               throw new RuntimeException("Request for Offset " + offset + " doc size is " + _size);
+                       }
+                       return null;
+               }
+               if (_property.shouldUseSmallBlocks()) {
+                       return SmallDocumentBlock.getDataInputBlock(_small_store.getBlocks(), offset);
+               }
+               return DocumentBlock.getDataInputBlock(_big_store.getBlocks(), offset);
+       }
+
+       /**
+        * @return the instance's DocumentProperty
+        */
+
+       DocumentProperty getDocumentProperty() {
+               return _property;
+       }
+
+       /* ********** START implementation of BlockWritable ********** */
+
+       /**
+        * Write the storage to an OutputStream
+        *
+        * @param stream the OutputStream to which the stored data should be written
+        */
+       public void writeBlocks(OutputStream stream) throws IOException {
+               _big_store.writeBlocks(stream);
+       }
+
+       /* **********  END  implementation of BlockWritable ********** */
+       /* ********** START implementation of BATManaged ********** */
+
+       /**
+        * Return the number of BigBlock's this instance uses
+        *
+        * @return count of BigBlock instances
+        */
+       public int countBlocks() {
+               return _big_store.countBlocks();
+       }
+
+       /**
+        * Set the start block for this instance
+        *
+        * @param index index into the array of blocks making up the filesystem
+        */
+       public void setStartBlock(int index) {
+               _property.setStartBlock(index);
+       }
+
+       /* **********  END  implementation of BATManaged ********** */
+       /* ********** START begin implementation of POIFSViewable ********** */
+
+       /**
+        * Get an array of objects, some of which may implement POIFSViewable
+        *
+        * @return an array of Object; may not be null, but may be empty
+        */
+       public Object[] getViewableArray() {
+               Object[] results = new Object[1];
+               String result;
+
+               try {
+                       ByteArrayOutputStream output = new ByteArrayOutputStream();
+                       BlockWritable[] blocks = null;
+
+                       if (_big_store.isValid()) {
+                               blocks = _big_store.getBlocks();
+                       } else if (_small_store.isValid()) {
+                               blocks = _small_store.getBlocks();
+                       }
+                       if (blocks != null) {
+                               for (int k = 0; k < blocks.length; k++) {
+                                       blocks[k].writeBlocks(output);
+                               }
+                               byte[] data = output.toByteArray();
+
+                               if (data.length > _property.getSize()) {
+                                       byte[] tmp = new byte[_property.getSize()];
+
+                                       System.arraycopy(data, 0, tmp, 0, tmp.length);
+                                       data = tmp;
+                               }
+                               output = new ByteArrayOutputStream();
+                               HexDump.dump(data, 0, output, 0);
+                               result = output.toString();
+                       } else {
+                               result = "<NO DATA>";
+                       }
+               } catch (IOException e) {
+                       result = e.getMessage();
+               }
+               results[0] = result;
+               return results;
+       }
+
+       /**
+        * Get an Iterator of objects, some of which may implement POIFSViewable
+        *
+        * @return an Iterator; may not be null, but may have an empty back end
+        *               store
+        */
+       public Iterator<Object> getViewableIterator() {
+               return Collections.emptyList().iterator();
+       }
+
+       /**
+        * Give viewers a hint as to whether to call getViewableArray or
+        * getViewableIterator
+        *
+        * @return <code>true</code> if a viewer should call getViewableArray,
+        *               <code>false</code> if a viewer should call getViewableIterator
+        */
+       public boolean preferArray() {
+               return true;
+       }
+
+       /**
+        * Provides a short description of the object, to be used when a
+        * POIFSViewable object has not provided its contents.
+        *
+        * @return short description
+        */
+       public String getShortDescription() {
+               StringBuffer buffer = new StringBuffer();
+
+               buffer.append("Document: \"").append(_property.getName()).append("\"");
+               buffer.append(" size = ").append(getSize());
+               return buffer.toString();
+       }
+
+       /* **********  END  begin implementation of POIFSViewable ********** */
+       private static final class SmallBlockStore {
+               private SmallDocumentBlock[] _smallBlocks;
+               private final POIFSDocumentPath _path;
+               private final String _name;
+               private final int _size;
+               private final POIFSWriterListener _writer;
+               private final POIFSBigBlockSize _bigBlockSize;
+
+               /**
+                * Constructor
+                *
+                * @param blocks blocks to construct the store from
+                */
+               SmallBlockStore(POIFSBigBlockSize bigBlockSize, SmallDocumentBlock[] blocks) {
+                  _bigBlockSize = bigBlockSize;
+                       _smallBlocks = blocks.clone();
+                       this._path = null;
+                       this._name = null;
+                       this._size = -1;
+                       this._writer = null;
+               }
+
+               /**
+                * Constructor for a small block store that will be written later
+                *
+                * @param path path of the document
+                * @param name name of the document
+                * @param size length of the document
+                * @param writer the object that will eventually write the document
+                */
+               SmallBlockStore(POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, 
+                               String name, int size, POIFSWriterListener writer) {
+                  _bigBlockSize = bigBlockSize;
+                       _smallBlocks = new SmallDocumentBlock[0];
+                       this._path = path;
+                       this._name = name;
+                       this._size = size;
+                       this._writer = writer;
+               }
+
+               /**
+                * @return <code>true</code> if this store is a valid source of data
+                */
+               boolean isValid() {
+                       return _smallBlocks.length > 0 || _writer != null;
+               }
+
+               /**
+                * @return the SmallDocumentBlocks
+                */
+               SmallDocumentBlock[] getBlocks() {
+                       if (isValid() && _writer != null) {
+                               ByteArrayOutputStream stream = new ByteArrayOutputStream(_size);
+                               DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
+
+                               _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
+                               _smallBlocks = SmallDocumentBlock.convert(_bigBlockSize, stream.toByteArray(), _size);
+                       }
+                       return _smallBlocks;
+               }
+       } // end private class SmallBlockStore
+
+       private static final class BigBlockStore {
+               private DocumentBlock[] bigBlocks;
+               private final POIFSDocumentPath _path;
+               private final String _name;
+               private final int _size;
+               private final POIFSWriterListener _writer;
+      private final POIFSBigBlockSize _bigBlockSize;
+
+               /**
+                * Constructor
+                *
+                * @param blocks the blocks making up the store
+                */
+               BigBlockStore(POIFSBigBlockSize bigBlockSize, DocumentBlock[] blocks) {
+                  _bigBlockSize = bigBlockSize;
+                       bigBlocks = blocks.clone();
+                       _path = null;
+                       _name = null;
+                       _size = -1;
+                       _writer = null;
+               }
+
+               /**
+                * Constructor for a big block store that will be written later
+                *
+                * @param path path of the document
+                * @param name name of the document
+                * @param size length of the document
+                * @param writer the object that will eventually write the document
+                */
+               BigBlockStore(POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, 
+                             String name, int size, POIFSWriterListener writer) {
+                  _bigBlockSize = bigBlockSize;
+                       bigBlocks = new DocumentBlock[0];
+                       _path = path;
+                       _name = name;
+                       _size = size;
+                       _writer = writer;
+               }
+
+               /**
+                * @return <code>true</code> if this store is a valid source of data
+                */
+               boolean isValid() {
+                       return bigBlocks.length > 0 || _writer != null;
+               }
+
+               /**
+                * @return the DocumentBlocks
+                */
+               DocumentBlock[] getBlocks() {
+                       if (isValid() && _writer != null) {
+                               ByteArrayOutputStream stream = new ByteArrayOutputStream(_size);
+                               DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
+
+                               _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
+                               bigBlocks = DocumentBlock.convert(_bigBlockSize, stream.toByteArray(), _size);
+                       }
+                       return bigBlocks;
+               }
+
+               /**
+                * write the blocks to a stream
+                *
+                * @param stream the stream to which the data is to be written
+                */
+               void writeBlocks(OutputStream stream) throws IOException {
+                       if (isValid()) {
+                               if (_writer != null) {
+                                       DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
+
+                                       _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
+                                       dstream.writeFiller(countBlocks() * _bigBlockSize.getBigBlockSize(),
+                                                       DocumentBlock.getFillByte());
+                               } else {
+                                       for (int k = 0; k < bigBlocks.length; k++) {
+                                               bigBlocks[k].writeBlocks(stream);
+                                       }
+                               }
+                       }
+               }
+
+               /**
+                * @return number of big blocks making up this document
+                */
+               int countBlocks() {
+
+                       if (isValid()) {
+                               if (_writer == null) {
+                                       return bigBlocks.length;
+                               }
+                               return (_size + _bigBlockSize.getBigBlockSize() - 1)
+                                                       / _bigBlockSize.getBigBlockSize();
+                       }
+                       return 0;
+               }
+       } // end private class BigBlockStore
+}
index b95c8991965218e7e5908568628829a1e8f0e0f1..d03c57942ee12edfa501c14472b564b03045327b 100644 (file)
@@ -445,7 +445,7 @@ public class OPOIFSFileSystem
      * @param document the POIFSDocument being added
      */
 
-    void addDocument(final POIFSDocument document)
+    void addDocument(final OPOIFSDocument document)
     {
         _documents.add(document);
         _property_table.addProperty(document.getDocumentProperty());
@@ -508,21 +508,21 @@ public class OPOIFSFileSystem
             {
                 int           startBlock = property.getStartBlock();
                 int           size       = property.getSize();
-                POIFSDocument document   = null;
+                OPOIFSDocument document  = null;
 
                 if (property.shouldUseSmallBlocks())
                 {
                     document =
-                        new POIFSDocument(name,
-                                          small_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
-                                          size);
+                        new OPOIFSDocument(name,
+                                           small_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
+                                           size);
                 }
                 else
                 {
                     document =
-                        new POIFSDocument(name,
-                                          big_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
-                                          size);
+                        new OPOIFSDocument(name,
+                                           big_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
+                                           size);
                 }
                 parent.createDocument(document);
             }
diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java
deleted file mode 100644 (file)
index c6b35fd..0000000
+++ /dev/null
@@ -1,568 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.poifs.filesystem;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.poi.poifs.common.POIFSBigBlockSize;
-import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.dev.POIFSViewable;
-import org.apache.poi.poifs.property.DocumentProperty;
-import org.apache.poi.poifs.property.Property;
-import org.apache.poi.poifs.storage.BlockWritable;
-import org.apache.poi.poifs.storage.DataInputBlock;
-import org.apache.poi.poifs.storage.DocumentBlock;
-import org.apache.poi.poifs.storage.ListManagedBlock;
-import org.apache.poi.poifs.storage.RawDataBlock;
-import org.apache.poi.poifs.storage.SmallDocumentBlock;
-import org.apache.poi.util.HexDump;
-
-/**
- * This class manages a document in the POIFS filesystem.
- *
- * @author Marc Johnson (mjohnson at apache dot org)
- */
-public final class POIFSDocument implements BATManaged, BlockWritable, POIFSViewable  {
-       private static final DocumentBlock[] EMPTY_BIG_BLOCK_ARRAY = { };
-       private static final SmallDocumentBlock[] EMPTY_SMALL_BLOCK_ARRAY = { };
-       private DocumentProperty _property;
-       private int _size;
-       
-   private final POIFSBigBlockSize _bigBigBlockSize;
-
-       // one of these stores will be valid
-       private SmallBlockStore  _small_store;
-       private BigBlockStore    _big_store;
-       
-               /**
-        * Constructor from large blocks
-        *
-        * @param name the name of the POIFSDocument
-        * @param blocks the big blocks making up the POIFSDocument
-        * @param length the actual length of the POIFSDocument
-        */
-       public POIFSDocument(String name, RawDataBlock[] blocks, int length) throws IOException {
-               _size = length;
-               if(blocks.length == 0) {
-                  _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
-               } else {
-                  _bigBigBlockSize = (blocks[0].getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ?
-                        POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS : 
-                        POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS
-                  );
-               }
-               
-               _big_store = new BigBlockStore(_bigBigBlockSize, convertRawBlocksToBigBlocks(blocks));
-               _property = new DocumentProperty(name, _size);
-               _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
-               _property.setDocument(this);
-       }
-
-       // TODO - awkward typing going on here
-       private static DocumentBlock[] convertRawBlocksToBigBlocks(ListManagedBlock[] blocks) throws IOException {
-               DocumentBlock[] result = new DocumentBlock[blocks.length];
-               for (int i = 0; i < result.length; i++) {
-                       result[i] = new DocumentBlock((RawDataBlock)blocks[i]);
-               }
-               return result;
-       }
-       private static SmallDocumentBlock[] convertRawBlocksToSmallBlocks(ListManagedBlock[] blocks) {
-               if (blocks instanceof SmallDocumentBlock[]) {
-                       return (SmallDocumentBlock[]) blocks;
-               }
-               SmallDocumentBlock[] result = new SmallDocumentBlock[blocks.length];
-               System.arraycopy(blocks, 0, result, 0, blocks.length);
-               return result;
-       }
-
-       /**
-        * Constructor from small blocks
-        *
-        * @param name the name of the POIFSDocument
-        * @param blocks the small blocks making up the POIFSDocument
-        * @param length the actual length of the POIFSDocument
-        */
-       public POIFSDocument(String name, SmallDocumentBlock[] blocks, int length) {
-               _size = length;
-               
-               if(blocks.length == 0) {
-                  _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
-               } else {
-                  _bigBigBlockSize = blocks[0].getBigBlockSize();
-               }
-
-               _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
-               _property = new DocumentProperty(name, _size);
-               _small_store = new SmallBlockStore(_bigBigBlockSize, blocks);
-               _property.setDocument(this);
-       }
-
-       /**
-        * Constructor from small blocks
-        *
-        * @param name the name of the POIFSDocument
-        * @param blocks the small blocks making up the POIFSDocument
-        * @param length the actual length of the POIFSDocument
-        */
-       public POIFSDocument(String name, POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks, int length) throws IOException {
-               _size = length;
-               _bigBigBlockSize = bigBlockSize;
-               _property = new DocumentProperty(name, _size);
-               _property.setDocument(this);
-               if (Property.isSmall(_size)) {
-                       _big_store = new BigBlockStore(bigBlockSize,EMPTY_BIG_BLOCK_ARRAY);
-                       _small_store = new SmallBlockStore(bigBlockSize,convertRawBlocksToSmallBlocks(blocks));
-               } else {
-                       _big_store = new BigBlockStore(bigBlockSize,convertRawBlocksToBigBlocks(blocks));
-                       _small_store = new SmallBlockStore(bigBlockSize,EMPTY_SMALL_BLOCK_ARRAY);
-               }
-       }
-       public POIFSDocument(String name, ListManagedBlock[] blocks, int length) throws IOException {
-          this(name, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blocks, length);
-       }
-
-       /**
-        * Constructor
-        *
-        * @param name the name of the POIFSDocument
-        * @param stream the InputStream we read data from
-        */
-       public POIFSDocument(String name, POIFSBigBlockSize bigBlockSize, InputStream stream) throws IOException {
-               List<DocumentBlock> blocks = new ArrayList<DocumentBlock>();
-
-               _size = 0;
-               _bigBigBlockSize = bigBlockSize;
-               while (true) {
-                       DocumentBlock block = new DocumentBlock(stream, bigBlockSize);
-                       int blockSize = block.size();
-
-                       if (blockSize > 0) {
-                               blocks.add(block);
-                               _size += blockSize;
-                       }
-                       if (block.partiallyRead()) {
-                               break;
-                       }
-               }
-               DocumentBlock[] bigBlocks = blocks.toArray(new DocumentBlock[blocks.size()]);
-
-               _big_store = new BigBlockStore(bigBlockSize,bigBlocks);
-               _property = new DocumentProperty(name, _size);
-               _property.setDocument(this);
-               if (_property.shouldUseSmallBlocks()) {
-                       _small_store = new SmallBlockStore(bigBlockSize,SmallDocumentBlock.convert(bigBlockSize,bigBlocks, _size));
-                       _big_store = new BigBlockStore(bigBlockSize,new DocumentBlock[0]);
-               } else {
-                       _small_store = new SmallBlockStore(bigBlockSize,EMPTY_SMALL_BLOCK_ARRAY);
-               }
-       }
-       public POIFSDocument(String name, InputStream stream) throws IOException {
-          this(name, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, stream);
-       }
-
-       /**
-        * Constructor
-        *
-        * @param name the name of the POIFSDocument
-        * @param size the length of the POIFSDocument
-        * @param path the path of the POIFSDocument
-        * @param writer the writer who will eventually write the document contents
-        */
-       public POIFSDocument(String name, int size, POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, POIFSWriterListener writer) {
-               _size = size;
-               _bigBigBlockSize = bigBlockSize;
-               _property = new DocumentProperty(name, _size);
-               _property.setDocument(this);
-               if (_property.shouldUseSmallBlocks()) {
-                       _small_store = new SmallBlockStore(_bigBigBlockSize, path, name, size, writer);
-                       _big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
-               } else {
-                       _small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
-                       _big_store = new BigBlockStore(_bigBigBlockSize, path, name, size, writer);
-               }
-       }
-       public POIFSDocument(String name, int size, POIFSDocumentPath path, POIFSWriterListener writer) {
-          this(name, size, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, path, writer);
-       }
-
-       /**
-        * @return array of SmallDocumentBlocks; may be empty, cannot be null
-        */
-       public BlockWritable[] getSmallBlocks() {
-               return _small_store.getBlocks();
-       }
-
-       /**
-        * @return size of the document
-        */
-       public int getSize() {
-               return _size;
-       }
-
-       /**
-        * read data from the internal stores
-        *
-        * @param buffer the buffer to write to
-        * @param offset the offset into our storage to read from
-        * This method is currently (Oct 2008) only used by test code. Perhaps it can be deleted
-        */
-       void read(byte[] buffer, int offset) {
-               int len = buffer.length;
-
-               DataInputBlock currentBlock = getDataInputBlock(offset);
-
-               int blockAvailable = currentBlock.available();
-               if (blockAvailable > len) {
-                       currentBlock.readFully(buffer, 0, len);
-                       return;
-               }
-               // else read big amount in chunks
-               int remaining = len;
-               int writePos = 0;
-               int currentOffset = offset;
-               while (remaining > 0) {
-                       boolean blockIsExpiring = remaining >= blockAvailable;
-                       int reqSize;
-                       if (blockIsExpiring) {
-                               reqSize = blockAvailable;
-                       } else {
-                               reqSize = remaining;
-                       }
-                       currentBlock.readFully(buffer, writePos, reqSize);
-                       remaining-=reqSize;
-                       writePos+=reqSize;
-                       currentOffset += reqSize;
-                       if (blockIsExpiring) {
-                               if (currentOffset == _size) {
-                                       if (remaining > 0) {
-                                               throw new IllegalStateException("reached end of document stream unexpectedly");
-                                       }
-                                       currentBlock = null;
-                                       break;
-                               }
-                               currentBlock = getDataInputBlock(currentOffset);
-                               blockAvailable = currentBlock.available();
-                       }
-               }
-       }
-
-       /**
-        * @return <code>null</code> if <tt>offset</tt> points to the end of the document stream
-        */
-       DataInputBlock getDataInputBlock(int offset) {
-               if (offset >= _size) {
-                       if (offset > _size) {
-                               throw new RuntimeException("Request for Offset " + offset + " doc size is " + _size);
-                       }
-                       return null;
-               }
-               if (_property.shouldUseSmallBlocks()) {
-                       return SmallDocumentBlock.getDataInputBlock(_small_store.getBlocks(), offset);
-               }
-               return DocumentBlock.getDataInputBlock(_big_store.getBlocks(), offset);
-       }
-
-       /**
-        * @return the instance's DocumentProperty
-        */
-
-       DocumentProperty getDocumentProperty() {
-               return _property;
-       }
-
-       /* ********** START implementation of BlockWritable ********** */
-
-       /**
-        * Write the storage to an OutputStream
-        *
-        * @param stream the OutputStream to which the stored data should be written
-        */
-       public void writeBlocks(OutputStream stream) throws IOException {
-               _big_store.writeBlocks(stream);
-       }
-
-       /* **********  END  implementation of BlockWritable ********** */
-       /* ********** START implementation of BATManaged ********** */
-
-       /**
-        * Return the number of BigBlock's this instance uses
-        *
-        * @return count of BigBlock instances
-        */
-       public int countBlocks() {
-               return _big_store.countBlocks();
-       }
-
-       /**
-        * Set the start block for this instance
-        *
-        * @param index index into the array of blocks making up the filesystem
-        */
-       public void setStartBlock(int index) {
-               _property.setStartBlock(index);
-       }
-
-       /* **********  END  implementation of BATManaged ********** */
-       /* ********** START begin implementation of POIFSViewable ********** */
-
-       /**
-        * Get an array of objects, some of which may implement POIFSViewable
-        *
-        * @return an array of Object; may not be null, but may be empty
-        */
-       public Object[] getViewableArray() {
-               Object[] results = new Object[1];
-               String result;
-
-               try {
-                       ByteArrayOutputStream output = new ByteArrayOutputStream();
-                       BlockWritable[] blocks = null;
-
-                       if (_big_store.isValid()) {
-                               blocks = _big_store.getBlocks();
-                       } else if (_small_store.isValid()) {
-                               blocks = _small_store.getBlocks();
-                       }
-                       if (blocks != null) {
-                               for (int k = 0; k < blocks.length; k++) {
-                                       blocks[k].writeBlocks(output);
-                               }
-                               byte[] data = output.toByteArray();
-
-                               if (data.length > _property.getSize()) {
-                                       byte[] tmp = new byte[_property.getSize()];
-
-                                       System.arraycopy(data, 0, tmp, 0, tmp.length);
-                                       data = tmp;
-                               }
-                               output = new ByteArrayOutputStream();
-                               HexDump.dump(data, 0, output, 0);
-                               result = output.toString();
-                       } else {
-                               result = "<NO DATA>";
-                       }
-               } catch (IOException e) {
-                       result = e.getMessage();
-               }
-               results[0] = result;
-               return results;
-       }
-
-       /**
-        * Get an Iterator of objects, some of which may implement POIFSViewable
-        *
-        * @return an Iterator; may not be null, but may have an empty back end
-        *               store
-        */
-       public Iterator<Object> getViewableIterator() {
-               return Collections.emptyList().iterator();
-       }
-
-       /**
-        * Give viewers a hint as to whether to call getViewableArray or
-        * getViewableIterator
-        *
-        * @return <code>true</code> if a viewer should call getViewableArray,
-        *               <code>false</code> if a viewer should call getViewableIterator
-        */
-       public boolean preferArray() {
-               return true;
-       }
-
-       /**
-        * Provides a short description of the object, to be used when a
-        * POIFSViewable object has not provided its contents.
-        *
-        * @return short description
-        */
-       public String getShortDescription() {
-               StringBuffer buffer = new StringBuffer();
-
-               buffer.append("Document: \"").append(_property.getName()).append("\"");
-               buffer.append(" size = ").append(getSize());
-               return buffer.toString();
-       }
-
-       /* **********  END  begin implementation of POIFSViewable ********** */
-       private static final class SmallBlockStore {
-               private SmallDocumentBlock[] _smallBlocks;
-               private final POIFSDocumentPath _path;
-               private final String _name;
-               private final int _size;
-               private final POIFSWriterListener _writer;
-               private final POIFSBigBlockSize _bigBlockSize;
-
-               /**
-                * Constructor
-                *
-                * @param blocks blocks to construct the store from
-                */
-               SmallBlockStore(POIFSBigBlockSize bigBlockSize, SmallDocumentBlock[] blocks) {
-                  _bigBlockSize = bigBlockSize;
-                       _smallBlocks = blocks.clone();
-                       this._path = null;
-                       this._name = null;
-                       this._size = -1;
-                       this._writer = null;
-               }
-
-               /**
-                * Constructor for a small block store that will be written later
-                *
-                * @param path path of the document
-                * @param name name of the document
-                * @param size length of the document
-                * @param writer the object that will eventually write the document
-                */
-               SmallBlockStore(POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, 
-                               String name, int size, POIFSWriterListener writer) {
-                  _bigBlockSize = bigBlockSize;
-                       _smallBlocks = new SmallDocumentBlock[0];
-                       this._path = path;
-                       this._name = name;
-                       this._size = size;
-                       this._writer = writer;
-               }
-
-               /**
-                * @return <code>true</code> if this store is a valid source of data
-                */
-               boolean isValid() {
-                       return _smallBlocks.length > 0 || _writer != null;
-               }
-
-               /**
-                * @return the SmallDocumentBlocks
-                */
-               SmallDocumentBlock[] getBlocks() {
-                       if (isValid() && _writer != null) {
-                               ByteArrayOutputStream stream = new ByteArrayOutputStream(_size);
-                               DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
-
-                               _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
-                               _smallBlocks = SmallDocumentBlock.convert(_bigBlockSize, stream.toByteArray(), _size);
-                       }
-                       return _smallBlocks;
-               }
-       } // end private class SmallBlockStore
-
-       private static final class BigBlockStore {
-               private DocumentBlock[] bigBlocks;
-               private final POIFSDocumentPath _path;
-               private final String _name;
-               private final int _size;
-               private final POIFSWriterListener _writer;
-      private final POIFSBigBlockSize _bigBlockSize;
-
-               /**
-                * Constructor
-                *
-                * @param blocks the blocks making up the store
-                */
-               BigBlockStore(POIFSBigBlockSize bigBlockSize, DocumentBlock[] blocks) {
-                  _bigBlockSize = bigBlockSize;
-                       bigBlocks = blocks.clone();
-                       _path = null;
-                       _name = null;
-                       _size = -1;
-                       _writer = null;
-               }
-
-               /**
-                * Constructor for a big block store that will be written later
-                *
-                * @param path path of the document
-                * @param name name of the document
-                * @param size length of the document
-                * @param writer the object that will eventually write the document
-                */
-               BigBlockStore(POIFSBigBlockSize bigBlockSize, POIFSDocumentPath path, 
-                             String name, int size, POIFSWriterListener writer) {
-                  _bigBlockSize = bigBlockSize;
-                       bigBlocks = new DocumentBlock[0];
-                       _path = path;
-                       _name = name;
-                       _size = size;
-                       _writer = writer;
-               }
-
-               /**
-                * @return <code>true</code> if this store is a valid source of data
-                */
-               boolean isValid() {
-                       return bigBlocks.length > 0 || _writer != null;
-               }
-
-               /**
-                * @return the DocumentBlocks
-                */
-               DocumentBlock[] getBlocks() {
-                       if (isValid() && _writer != null) {
-                               ByteArrayOutputStream stream = new ByteArrayOutputStream(_size);
-                               DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
-
-                               _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
-                               bigBlocks = DocumentBlock.convert(_bigBlockSize, stream.toByteArray(), _size);
-                       }
-                       return bigBlocks;
-               }
-
-               /**
-                * write the blocks to a stream
-                *
-                * @param stream the stream to which the data is to be written
-                */
-               void writeBlocks(OutputStream stream) throws IOException {
-                       if (isValid()) {
-                               if (_writer != null) {
-                                       DocumentOutputStream dstream = new DocumentOutputStream(stream, _size);
-
-                                       _writer.processPOIFSWriterEvent(new POIFSWriterEvent(dstream, _path, _name, _size));
-                                       dstream.writeFiller(countBlocks() * _bigBlockSize.getBigBlockSize(),
-                                                       DocumentBlock.getFillByte());
-                               } else {
-                                       for (int k = 0; k < bigBlocks.length; k++) {
-                                               bigBlocks[k].writeBlocks(stream);
-                                       }
-                               }
-                       }
-               }
-
-               /**
-                * @return number of big blocks making up this document
-                */
-               int countBlocks() {
-
-                       if (isValid()) {
-                               if (_writer == null) {
-                                       return bigBlocks.length;
-                               }
-                               return (_size + _bigBlockSize.getBigBlockSize() - 1)
-                                                       / _bigBlockSize.getBigBlockSize();
-                       }
-                       return 0;
-               }
-       } // end private class BigBlockStore
-}
index c6223cf2421d310db68e11af13c76a5950f09163..bf5ea3c589e3d872ff8950a5620c12c0def2b3db 100644 (file)
@@ -447,7 +447,7 @@ public class POIFSFileSystem
      * @param document the POIFSDocument being added
      */
 
-    void addDocument(final POIFSDocument document)
+    void addDocument(final OPOIFSDocument document)
     {
         _documents.add(document);
         _property_table.addProperty(document.getDocumentProperty());
@@ -510,21 +510,21 @@ public class POIFSFileSystem
             {
                 int           startBlock = property.getStartBlock();
                 int           size       = property.getSize();
-                POIFSDocument document   = null;
+                OPOIFSDocument document  = null;
 
                 if (property.shouldUseSmallBlocks())
                 {
                     document =
-                        new POIFSDocument(name,
-                                          small_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
-                                          size);
+                        new OPOIFSDocument(name,
+                                           small_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
+                                           size);
                 }
                 else
                 {
                     document =
-                        new POIFSDocument(name,
-                                          big_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
-                                          size);
+                        new OPOIFSDocument(name,
+                                           big_blocks.fetchBlocks(startBlock, headerPropertiesStartAt),
+                                           size);
                 }
                 parent.createDocument(document);
             }
index 67c8b03073dc117bd651109e259fa758d0d1734a..3ba3c0889b3faa2b377566b65bcb028c54d06feb 100644 (file)
 
 package org.apache.poi.poifs.property;
 
-import org.apache.poi.poifs.filesystem.POIFSDocument;
+import org.apache.poi.poifs.filesystem.OPOIFSDocument;
 
 /**
  * Trivial extension of Property for POIFSDocuments
- *
- * @author Marc Johnson (mjohnson at apache dot org)
  */
 
 public class DocumentProperty
@@ -32,7 +30,7 @@ public class DocumentProperty
 {
 
     // the POIFSDocument this property is associated with
-    private POIFSDocument _document;
+    private OPOIFSDocument _document;
 
     /**
      * Constructor
@@ -72,7 +70,7 @@ public class DocumentProperty
      * @param doc the associated POIFSDocument
      */
 
-    public void setDocument(POIFSDocument doc)
+    public void setDocument(OPOIFSDocument doc)
     {
         _document = doc;
     }
@@ -83,7 +81,7 @@ public class DocumentProperty
      * @return the associated document
      */
 
-    public POIFSDocument getDocument()
+    public OPOIFSDocument getDocument()
     {
         return _document;
     }
index 2db7bf4c73320030ec1410fb3c501c834ae36816..783518f1533e1587bc6e4792b1e3aa6b7c18ea73 100644 (file)
@@ -22,7 +22,7 @@ package org.apache.poi.poifs.storage;
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.filesystem.BATManaged;
-import org.apache.poi.poifs.filesystem.POIFSDocument;
+import org.apache.poi.poifs.filesystem.OPOIFSDocument;
 import org.apache.poi.poifs.property.RootProperty;
 
 import java.util.*;
@@ -62,7 +62,7 @@ public class SmallBlockTableWriter
 
         while (iter.hasNext())
         {
-            POIFSDocument   doc    = ( POIFSDocument ) iter.next();
+            OPOIFSDocument   doc    = ( OPOIFSDocument ) iter.next();
             BlockWritable[] blocks = doc.getSmallBlocks();
 
             if (blocks.length != 0)
index 0bb1de24b48cef7f69f78040202bf8c759c8fe33..6c2969a78943e2a84e7e61a5840b7e6c25dd3824 100644 (file)
@@ -32,7 +32,6 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
index e216ed9a88d2be0006c072f4183cf2661ffc34c9..21494b39badd50329715030622b8a1f916d8d954 100644 (file)
@@ -28,27 +28,25 @@ import org.apache.poi.poifs.storage.RawDataBlock;
 import org.apache.poi.poifs.storage.SmallDocumentBlock;
 
 /**
- * Class to test POIFSDocument functionality
- *
- * @author Marc Johnson
+ * Class to test OPOIFSDocument functionality
  */
 public final class TestDocument extends TestCase {
 
     /**
      * Integration test -- really about all we can do
      */
-    public void testPOIFSDocument() throws IOException {
+    public void testOPOIFSDocument() throws IOException {
 
         // verify correct number of blocks get created for document
         // that is exact multituple of block size
-        POIFSDocument document;
+        OPOIFSDocument document;
         byte[]        array = new byte[ 4096 ];
 
         for (int j = 0; j < array.length; j++)
         {
             array[ j ] = ( byte ) j;
         }
-        document = new POIFSDocument("foo", new SlowInputStream(new ByteArrayInputStream(array)));
+        document = new OPOIFSDocument("foo", new SlowInputStream(new ByteArrayInputStream(array)));
         checkDocument(document, array);
 
         // verify correct number of blocks get created for document
@@ -58,7 +56,7 @@ public final class TestDocument extends TestCase {
         {
             array[ j ] = ( byte ) j;
         }
-        document = new POIFSDocument("bar", new ByteArrayInputStream(array));
+        document = new OPOIFSDocument("bar", new ByteArrayInputStream(array));
         checkDocument(document, array);
 
         // verify correct number of blocks get created for document
@@ -68,7 +66,7 @@ public final class TestDocument extends TestCase {
         {
             array[ j ] = ( byte ) j;
         }
-        document = new POIFSDocument("_bar", new ByteArrayInputStream(array));
+        document = new OPOIFSDocument("_bar", new ByteArrayInputStream(array));
         checkDocument(document, array);
 
         // verify correct number of blocks get created for document
@@ -78,7 +76,7 @@ public final class TestDocument extends TestCase {
         {
             array[ j ] = ( byte ) j;
         }
-        document = new POIFSDocument("_bar2",
+        document = new OPOIFSDocument("_bar2",
                                      new ByteArrayInputStream(array));
         checkDocument(document, array);
 
@@ -88,7 +86,7 @@ public final class TestDocument extends TestCase {
         {
             array[ j ] = ( byte ) j;
         }
-        document = new POIFSDocument("foobar",
+        document = new OPOIFSDocument("foobar",
                                      new ByteArrayInputStream(array));
         checkDocument(document, array);
         document.setStartBlock(0x12345678);   // what a big file!!
@@ -135,9 +133,9 @@ public final class TestDocument extends TestCase {
         }
     }
 
-    private static POIFSDocument makeCopy(POIFSDocument document, byte[] input, byte[] data)
+    private static OPOIFSDocument makeCopy(OPOIFSDocument document, byte[] input, byte[] data)
             throws IOException {
-        POIFSDocument copy = null;
+        OPOIFSDocument copy = null;
 
         if (input.length >= 4096)
         {
@@ -156,12 +154,12 @@ public final class TestDocument extends TestCase {
                 }
                 blocks[ index++ ] = block;
             }
-            copy = new POIFSDocument("test" + input.length, blocks,
+            copy = new OPOIFSDocument("test" + input.length, blocks,
                                      input.length);
         }
         else
         {
-            copy = new POIFSDocument(
+            copy = new OPOIFSDocument(
                 "test" + input.length,
                 ( SmallDocumentBlock [] ) document.getSmallBlocks(),
                 input.length);
@@ -169,7 +167,7 @@ public final class TestDocument extends TestCase {
         return copy;
     }
 
-    private static void checkDocument(final POIFSDocument document, final byte[] input)
+    private static void checkDocument(final OPOIFSDocument document, final byte[] input)
             throws IOException {
         int big_blocks   = 0;
         int small_blocks = 0;
@@ -195,7 +193,7 @@ public final class TestDocument extends TestCase {
     }
 
     private static byte[] checkValues(int big_blocks, int small_blocks, int total_output,
-            POIFSDocument document, byte[] input) throws IOException {
+            OPOIFSDocument document, byte[] input) throws IOException {
         assertEquals(document, document.getDocumentProperty().getDocument());
         int increment = ( int ) Math.sqrt(input.length);
 
index 6ba10f99c6ae60754bc171544e718e22e85b2b5e..98b2b7a7d7d6ba74722c7cedc100a4b393bc66dc 100644 (file)
@@ -64,8 +64,8 @@ public final class TestDocumentInputStream extends TestCase {
         {
             rawBlocks[ j ] = new RawDataBlock(stream);
         }
-        POIFSDocument document = new POIFSDocument("Workbook", rawBlocks,
-                                                   _workbook_size);
+        OPOIFSDocument document = new OPOIFSDocument("Workbook", rawBlocks,
+                                                     _workbook_size);
 
         _workbook_o = new DocumentNode(
             document.getDocumentProperty(),
index 6101494a07f378e25ea35a7e8331cf189994ebe5..69d167793a81e0960fb0f15970d64c521dea41d9 100644 (file)
@@ -46,7 +46,7 @@ public final class TestDocumentNode extends TestCase {
         {
             rawBlocks[ j ] = new RawDataBlock(stream);
         }
-        POIFSDocument    document  = new POIFSDocument("document", rawBlocks,
+        OPOIFSDocument   document  = new OPOIFSDocument("document", rawBlocks,
                                          2000);
         DocumentProperty property2 = document.getDocumentProperty();
         DirectoryNode    parent    = new DirectoryNode(property1, (POIFSFileSystem)null, null);
index f10576a4aa4692183fc6ff88f9e2e0eea98ca36b..9bb4f6a16632b2eba5ad805f8bb50e0bb6292cc2 100644 (file)
@@ -25,7 +25,7 @@ import java.util.List;
 import junit.framework.TestCase;
 
 import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.filesystem.POIFSDocument;
+import org.apache.poi.poifs.filesystem.OPOIFSDocument;
 import org.apache.poi.poifs.property.PropertyTable;
 import org.apache.poi.poifs.property.RootProperty;
 
@@ -37,43 +37,43 @@ import org.apache.poi.poifs.property.RootProperty;
 public final class TestSmallBlockTableWriter extends TestCase {
 
     public void testWritingConstructor() throws IOException {
-        List<POIFSDocument> documents = new ArrayList<POIFSDocument>();
+        List<OPOIFSDocument> documents = new ArrayList<OPOIFSDocument>();
 
         documents.add(
-            new POIFSDocument(
+            new OPOIFSDocument(
                 "doc340", new ByteArrayInputStream(new byte[ 340 ])));
         documents.add(
-            new POIFSDocument(
+            new OPOIFSDocument(
                 "doc5000", new ByteArrayInputStream(new byte[ 5000 ])));
         documents
-            .add(new POIFSDocument("doc0",
+            .add(new OPOIFSDocument("doc0",
                                    new ByteArrayInputStream(new byte[ 0 ])));
         documents
-            .add(new POIFSDocument("doc1",
+            .add(new OPOIFSDocument("doc1",
                                    new ByteArrayInputStream(new byte[ 1 ])));
         documents
-            .add(new POIFSDocument("doc2",
+            .add(new OPOIFSDocument("doc2",
                                    new ByteArrayInputStream(new byte[ 2 ])));
         documents
-            .add(new POIFSDocument("doc3",
+            .add(new OPOIFSDocument("doc3",
                                    new ByteArrayInputStream(new byte[ 3 ])));
         documents
-            .add(new POIFSDocument("doc4",
+            .add(new OPOIFSDocument("doc4",
                                    new ByteArrayInputStream(new byte[ 4 ])));
         documents
-            .add(new POIFSDocument("doc5",
+            .add(new OPOIFSDocument("doc5",
                                    new ByteArrayInputStream(new byte[ 5 ])));
         documents
-            .add(new POIFSDocument("doc6",
+            .add(new OPOIFSDocument("doc6",
                                    new ByteArrayInputStream(new byte[ 6 ])));
         documents
-            .add(new POIFSDocument("doc7",
+            .add(new OPOIFSDocument("doc7",
                                    new ByteArrayInputStream(new byte[ 7 ])));
         documents
-            .add(new POIFSDocument("doc8",
+            .add(new OPOIFSDocument("doc8",
                                    new ByteArrayInputStream(new byte[ 8 ])));
         documents
-            .add(new POIFSDocument("doc9",
+            .add(new OPOIFSDocument("doc9",
                                    new ByteArrayInputStream(new byte[ 9 ])));
         
         HeaderBlock              header = new HeaderBlock(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS);