You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ki...@apache.org on 2018/08/31 00:25:51 UTC

svn commit: r1839709 [2/7] - in /poi: site/src/documentation/content/xdocs/components/poifs/ trunk/src/examples/src/org/apache/poi/hpsf/examples/ trunk/src/java/org/apache/poi/ trunk/src/java/org/apache/poi/extractor/ trunk/src/java/org/apache/poi/hpsf...

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java Fri Aug 31 00:25:50 2018
@@ -22,18 +22,23 @@ import static org.apache.poi.util.Little
 import static org.apache.poi.util.LittleEndianConsts.SHORT_SIZE;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.util.Iterator;
 
 import org.apache.poi.poifs.property.DocumentProperty;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
 
 /**
  * This class provides methods to read a DocumentEntry managed by a
- * {@link NPOIFSFileSystem} instance.
+ * {@link POIFSFileSystem} instance.
  */
-public final class NDocumentInputStream extends DocumentInputStream {
+public final class DocumentInputStream extends InputStream implements LittleEndianInput {
+    /** returned by read operations if we're at end of document */
+    private static final int EOF = -1;
+
     /** current offset into the Document */
     private int _current_offset;
     /** current block count */
@@ -51,7 +56,7 @@ public final class NDocumentInputStream
     private boolean _closed;
 
     /** the actual Document */
-    private final NPOIFSDocument _document;
+    private final POIFSDocument _document;
 
     private Iterator<ByteBuffer> _data;
     private ByteBuffer _buffer;
@@ -64,7 +69,7 @@ public final class NDocumentInputStream
      * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has
      *                been deleted?)
      */
-    public NDocumentInputStream(DocumentEntry document) throws IOException {
+    public DocumentInputStream(DocumentEntry document) throws IOException {
         if (!(document instanceof DocumentNode)) {
             throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
         }
@@ -80,7 +85,7 @@ public final class NDocumentInputStream
 
         DocumentNode doc = (DocumentNode)document;
         DocumentProperty property = (DocumentProperty)doc.getProperty();
-        _document = new NPOIFSDocument(
+        _document = new POIFSDocument(
                 property, 
                 ((DirectoryNode)doc.getParent()).getNFileSystem()
         );
@@ -92,7 +97,7 @@ public final class NDocumentInputStream
      * 
      * @param document the Document to be read
      */
-    public NDocumentInputStream(NPOIFSDocument document) {
+    public DocumentInputStream(POIFSDocument document) {
         _current_offset = 0;
         _current_block_count = 0;
         _marked_offset = 0;
@@ -125,6 +130,16 @@ public final class NDocumentInputStream
         _closed = true;
     }
 
+    /**
+     * Tests if this input stream supports the mark and reset methods.
+     *
+     * @return {@code true} always
+     */
+    @Override
+    public boolean markSupported() {
+        return true;
+    }
+
     @Override
     public void mark(int ignoredReadlimit) {
         _marked_offset = _current_offset;
@@ -149,6 +164,11 @@ public final class NDocumentInputStream
     }
 
     @Override
+    public int read(byte[] b) throws IOException {
+        return read(b, 0, b.length);
+    }
+
+    @Override
     public int read(byte[] b, int off, int len) throws IOException {
         dieIfClosed();
         if (b == null) {
@@ -254,7 +274,12 @@ public final class NDocumentInputStream
 		}
 	}
 
-   @Override
+    @Override
+    public void readFully(byte[] buf) {
+        readFully(buf, 0, buf.length);
+    }
+
+    @Override
 	public void readFully(byte[] buf, int off, int len) {
         if (len < 0) {
            throw new RuntimeException("Can't read negative number of bytes");
@@ -276,7 +301,13 @@ public final class NDocumentInputStream
 		}
 	}
 
-   @Override
+    @Override
+    public void readPlain(byte[] buf, int off, int len) {
+        readFully(buf, off, len);
+    }
+
+
+    @Override
    public byte readByte() {
       return (byte) readUByte();
    }
@@ -310,7 +341,12 @@ public final class NDocumentInputStream
       return LittleEndian.getInt(data);
 	}
 
-   @Override
+    public long readUInt() {
+        int i = readInt();
+        return i & 0xFFFFFFFFL;
+    }
+
+    @Override
 	public int readUShort() {
 		checkAvaliable(SHORT_SIZE);
       byte[] data = new byte[SHORT_SIZE];

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentNode.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentNode.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentNode.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentNode.java Fri Aug 31 00:25:50 2018
@@ -35,7 +35,7 @@ public class DocumentNode
 {
 
     // underlying POIFSDocument instance
-    private NPOIFSDocument _document;
+    private POIFSDocument _document;
 
     /**
      * create a DocumentNode. This method is not public by design; it
@@ -56,7 +56,7 @@ public class DocumentNode
      *
      * @return the internal POIFSDocument
      */
-    NPOIFSDocument getDocument()
+    POIFSDocument getDocument()
     {
         return _document;
     }

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentOutputStream.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentOutputStream.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentOutputStream.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentOutputStream.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentOutputStream.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentOutputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/DocumentOutputStream.java Fri Aug 31 00:25:50 2018
@@ -27,17 +27,17 @@ import org.apache.poi.poifs.property.Doc
 
 /**
  * This class provides methods to write a DocumentEntry managed by a
- * {@link NPOIFSFileSystem} instance.
+ * {@link POIFSFileSystem} instance.
  */
-public final class NDocumentOutputStream extends OutputStream {
-	/** the Document's size */
-	private int _document_size;
+public final class DocumentOutputStream extends OutputStream {
+	/** the Document's size, i.e. the size of the big block data - mini block data is cached and not counted */
+	private int _document_size = 0;
 
-	/** have we been closed? */
-	private boolean _closed;
+    /** have we been closed? */
+	private boolean _closed = false;
 
 	/** the actual Document */
-	private NPOIFSDocument _document;
+	private POIFSDocument _document;
 	/** and its Property */
 	private DocumentProperty _property;
 	
@@ -46,53 +46,68 @@ public final class NDocumentOutputStream
 	        new ByteArrayOutputStream(POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE);
 	
 	/** our main block stream, when we're into normal blocks */
-	private NPOIFSStream _stream;
+	private POIFSStream _stream;
 	private OutputStream _stream_output;
-	
+
+    /** a write limit or -1 if unlimited */
+    private final long _limit;
+
+
 	/**
 	 * Create an OutputStream from the specified DocumentEntry.
 	 * The specified entry will be emptied.
 	 * 
 	 * @param document the DocumentEntry to be written
 	 */
-	public NDocumentOutputStream(DocumentEntry document) throws IOException {
-		if (!(document instanceof DocumentNode)) {
-			throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
-		}
-		_document_size = 0;
-		_closed = false;
-		
-		_property = (DocumentProperty)((DocumentNode)document).getProperty();
-		
-		_document = new NPOIFSDocument((DocumentNode)document);
-		_document.free();
+	public DocumentOutputStream(DocumentEntry document) throws IOException {
+	    this(document, -1);
 	}
-	
-	/**
+
+    /**
 	 * Create an OutputStream to create the specified new Entry
 	 * 
 	 * @param parent Where to create the Entry
 	 * @param name Name of the new entry
 	 */
-	public NDocumentOutputStream(DirectoryEntry parent, String name) throws IOException {
+	public DocumentOutputStream(DirectoryEntry parent, String name) throws IOException {
+	    this(createDocument(parent, name), -1);
+	}
+
+    /**
+     * Create a DocumentOutputStream
+     *
+     * @param document the DocumentEntry to which the data is actually written
+     * @param limit the maximum number of bytes that can be written
+     */
+    DocumentOutputStream(DocumentEntry document, long limit) throws IOException {
+        this(getDocument(document), limit);
+    }
+
+    DocumentOutputStream(POIFSDocument document, long limit) throws IOException {
+        _document = document;
+        _document.free();
+
+        _property = document.getDocumentProperty();
+
+        _limit   = limit;
+    }
+
+    private static POIFSDocument getDocument(DocumentEntry document) throws IOException {
+        if (!(document instanceof DocumentNode)) {
+            throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
+        }
+        return new POIFSDocument((DocumentNode)document);
+    }
+
+    private static DocumentEntry createDocument(DirectoryEntry parent, String name) throws IOException {
         if (!(parent instanceof DirectoryNode)) {
             throw new IOException("Cannot open internal directory storage, " + parent + " not a Directory Node");
         }
-        _document_size = 0;
-        _closed = false;
 
         // Have an empty one created for now
-        DocumentEntry doc = parent.createDocument(name, new ByteArrayInputStream(new byte[0]));
-        _property = (DocumentProperty)((DocumentNode)doc).getProperty();
-        _document = new NPOIFSDocument((DocumentNode)doc);
-	}
-	
-    private void dieIfClosed() throws IOException {
-        if (_closed) {
-            throw new IOException("cannot perform requested operation on a closed stream");
-        }
+        return parent.createDocument(name, new ByteArrayInputStream(new byte[0]));
     }
-    
+
     private void checkBufferSize() throws IOException {
         // Have we gone over the mini stream limit yet?
         if (_buffer.size() > POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
@@ -106,36 +121,24 @@ public final class NDocumentOutputStream
     }
 
     public void write(int b) throws IOException {
-        dieIfClosed();
-        
-        if (_buffer != null) {
-            _buffer.write(b);
-            checkBufferSize();
-        } else {
-            write(new byte[] { (byte)b });
-        }
+        write(new byte[] { (byte)b }, 0, 1);
     }
 
-    public void write(byte[] b) throws IOException {
-        dieIfClosed();
-        
-        if (_buffer != null) {
-            _buffer.write(b);
-            checkBufferSize();
-        } else {
-            write(b, 0, b.length);
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        if (_closed) {
+            throw new IOException("cannot perform requested operation on a closed stream");
+        }
+        if (_limit > -1 && (size() + len) > _limit) {
+            throw new IOException("tried to write too much data");
         }
-    }
 
-    public void write(byte[] b, int off, int len) throws IOException {
-        dieIfClosed();
-        
         if (_buffer != null) {
             _buffer.write(b, off, len);
             checkBufferSize();
         } else {
             if (_stream == null) {
-                _stream = new NPOIFSStream(_document.getFileSystem());
+                _stream = new POIFSStream(_document.getFileSystem());
                 _stream_output = _stream.getOutputStream();
             }
             _stream_output.write(b, off, len);
@@ -146,7 +149,7 @@ public final class NDocumentOutputStream
     public void close() throws IOException {
         // 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
+            // It's not much data, so ask POIFSDocument to do it for us
             _document.replaceContents(new ByteArrayInputStream(_buffer.toByteArray()));
         }
         else {
@@ -160,4 +163,11 @@ public final class NDocumentOutputStream
         // No more!
         _closed = true;
     }
-}
+
+    /**
+     * @return the amount of written bytes
+     */
+    public long size() {
+	    return _document_size + (_buffer == null ? 0 : _buffer.size());
+    }
+}
\ No newline at end of file

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java Fri Aug 31 00:25:50 2018
@@ -81,7 +81,7 @@ public final class EntryUtils {
      * @param target
      *            is the target POIFS to copy to
      */
-    public static void copyNodes( NPOIFSFileSystem source, NPOIFSFileSystem target )
+    public static void copyNodes(POIFSFileSystem source, POIFSFileSystem target )
     throws IOException {
         copyNodes( source.getRoot(), target.getRoot() );
     }
@@ -96,7 +96,7 @@ public final class EntryUtils {
      * @param target is the target POIFS to copy to
      * @param excepts is a list of Entry Names to be excluded from the copy
      */
-    public static void copyNodes( NPOIFSFileSystem source, NPOIFSFileSystem target, List<String> excepts )
+    public static void copyNodes(POIFSFileSystem source, POIFSFileSystem target, List<String> excepts )
     throws IOException {
         copyNodes(
               new FilteringDirectoryNode(source.getRoot(), excepts),

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSDocument.java Fri Aug 31 00:25:50 2018
@@ -36,23 +36,23 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * This class manages a document in the NIO POIFS filesystem.
- * This is the {@link NPOIFSFileSystem} version.
+ * This is the {@link POIFSFileSystem} version.
  */
-public final class NPOIFSDocument implements POIFSViewable, Iterable<ByteBuffer> {
+public final class POIFSDocument implements POIFSViewable, Iterable<ByteBuffer> {
 
     //arbitrarily selected; may need to increase
     private static final int MAX_RECORD_LENGTH = 100_000;
 
     private DocumentProperty _property;
 
-   private NPOIFSFileSystem _filesystem;
-   private NPOIFSStream _stream;
+   private POIFSFileSystem _filesystem;
+   private POIFSStream _stream;
    private int _block_size;
 	
    /**
     * Constructor for an existing Document 
     */
-   public NPOIFSDocument(DocumentNode document) {
+   public POIFSDocument(DocumentNode document) {
        this((DocumentProperty)document.getProperty(), 
             ((DirectoryNode)document.getParent()).getNFileSystem());
    }
@@ -60,15 +60,15 @@ public final class NPOIFSDocument implem
    /**
     * Constructor for an existing Document 
     */
-   public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem) {
+   public POIFSDocument(DocumentProperty property, POIFSFileSystem filesystem) {
       this._property = property;
       this._filesystem = filesystem;
 
       if(property.getSize() < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
-         _stream = new NPOIFSStream(_filesystem.getMiniStore(), property.getStartBlock());
+         _stream = new POIFSStream(_filesystem.getMiniStore(), property.getStartBlock());
          _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
       } else {
-         _stream = new NPOIFSStream(_filesystem, property.getStartBlock());
+         _stream = new POIFSStream(_filesystem, property.getStartBlock());
          _block_size = _filesystem.getBlockStoreBlockSize();
       }
    }
@@ -79,7 +79,7 @@ public final class NPOIFSDocument implem
     * @param name the name of the POIFSDocument
     * @param stream the InputStream we read data from
     */
-   public NPOIFSDocument(String name, NPOIFSFileSystem filesystem, InputStream stream) 
+   public POIFSDocument(String name, POIFSFileSystem filesystem, InputStream stream)
       throws IOException 
    {
       this._filesystem = filesystem;
@@ -93,31 +93,29 @@ public final class NPOIFSDocument implem
       _property.setDocument(this);
    }
    
-   public NPOIFSDocument(String name, int size, NPOIFSFileSystem filesystem, POIFSWriterListener writer) 
+   public POIFSDocument(String name, final int size, POIFSFileSystem filesystem, POIFSWriterListener writer)
       throws IOException 
    {
        this._filesystem = filesystem;
 
        if (size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
-           _stream = new NPOIFSStream(filesystem.getMiniStore());
+           _stream = new POIFSStream(filesystem.getMiniStore());
            _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
        } else {
-           _stream = new NPOIFSStream(filesystem);
+           _stream = new POIFSStream(filesystem);
            _block_size = _filesystem.getBlockStoreBlockSize();
        }
        
-       OutputStream innerOs = _stream.getOutputStream();
-       DocumentOutputStream os = new DocumentOutputStream(innerOs, size);
-       POIFSDocumentPath path = new POIFSDocumentPath(name.split("\\\\"));
-       String docName = path.getComponent(path.length()-1);
-       POIFSWriterEvent event = new POIFSWriterEvent(os, path, docName, size);
-       writer.processPOIFSWriterEvent(event);
-       innerOs.close();
-
-       // And build the property for it
        this._property = new DocumentProperty(name, size);
        _property.setStartBlock(_stream.getStartBlock());
        _property.setDocument(this);
+
+       try (DocumentOutputStream os = new DocumentOutputStream(this, size)) {
+           POIFSDocumentPath path = new POIFSDocumentPath(name.split("\\\\"));
+           String docName = path.getComponent(path.length() - 1);
+           POIFSWriterEvent event = new POIFSWriterEvent(os, path, docName, size);
+           writer.processPOIFSWriterEvent(event);
+       }
    }
    
    /**
@@ -131,10 +129,10 @@ public final class NPOIFSDocument implem
        // Do we need to store as a mini stream or a full one?
        long streamBlockSize = IOUtils.skipFully(bis, bigBlockSize);
        if (streamBlockSize < bigBlockSize) {
-          _stream = new NPOIFSStream(_filesystem.getMiniStore());
+          _stream = new POIFSStream(_filesystem.getMiniStore());
           _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
        } else {
-          _stream = new NPOIFSStream(_filesystem);
+          _stream = new POIFSStream(_filesystem);
           _block_size = _filesystem.getBlockStoreBlockSize();
        }
 
@@ -167,7 +165,7 @@ public final class NPOIFSDocument implem
        _property.setStartBlock(POIFSConstants.END_OF_CHAIN);
    }
    
-   NPOIFSFileSystem getFileSystem()
+   POIFSFileSystem getFileSystem()
    {
        return _filesystem;
    }

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java Fri Aug 31 00:25:50 2018
@@ -19,6 +19,7 @@
 
 package org.apache.poi.poifs.filesystem;
 
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
@@ -44,14 +45,10 @@ import org.apache.poi.poifs.nio.DataSour
 import org.apache.poi.poifs.nio.FileBackedDataSource;
 import org.apache.poi.poifs.property.DirectoryProperty;
 import org.apache.poi.poifs.property.DocumentProperty;
-import org.apache.poi.poifs.property.NPropertyTable;
+import org.apache.poi.poifs.property.PropertyTable;
 import org.apache.poi.poifs.storage.BATBlock;
 import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex;
-import org.apache.poi.poifs.storage.BlockAllocationTableReader;
-import org.apache.poi.poifs.storage.BlockAllocationTableWriter;
 import org.apache.poi.poifs.storage.HeaderBlock;
-import org.apache.poi.poifs.storage.HeaderBlockWriter;
-import org.apache.poi.util.CloseIgnoringInputStream;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
@@ -63,23 +60,29 @@ import org.apache.poi.util.POILogger;
  * <p>This is the new NIO version, which uses less memory</p>
  */
 
-public class NPOIFSFileSystem extends BlockStore
+public class POIFSFileSystem extends BlockStore
     implements POIFSViewable, Closeable
 {
     //arbitrarily selected; may need to increase
     private static final int MAX_RECORD_LENGTH = 100_000;
 
-    private static final POILogger LOG = POILogFactory.getLogger(NPOIFSFileSystem.class);
+    private static final POILogger LOG = POILogFactory.getLogger(POIFSFileSystem.class);
 
     /**
-     * Convenience method for clients that want to avoid the auto-close behaviour of the constructor.
+     * Maximum number size (in blocks) of the allocation table as supported by
+     * POI.<p>
+     *
+     * This constant has been chosen to help POI identify corrupted data in the
+     * header block (rather than crash immediately with {@link OutOfMemoryError}
+     * ). It's not clear if the compound document format actually specifies any
+     * upper limits. For files with 512 byte blocks, having an allocation table
+     * of 65,335 blocks would correspond to a total file size of 4GB. Needless
+     * to say, POI probably cannot handle files anywhere near that size.
      */
-    public static InputStream createNonClosingInputStream(InputStream is) {
-       return new CloseIgnoringInputStream(is);
-    }
-   
-    private NPOIFSMiniStore _mini_store;
-    private NPropertyTable  _property_table;
+    private static final int MAX_BLOCK_COUNT = 65535;
+
+    private POIFSMiniStore _mini_store;
+    private PropertyTable _property_table;
     private List<BATBlock>  _xbat_blocks;
     private List<BATBlock>  _bat_blocks;
     private HeaderBlock     _header;
@@ -94,11 +97,11 @@ public class NPOIFSFileSystem extends Bl
     private POIFSBigBlockSize bigBlockSize = 
        POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
 
-    private NPOIFSFileSystem(boolean newFS)
+    private POIFSFileSystem(boolean newFS)
     {
         _header         = new HeaderBlock(bigBlockSize);
-        _property_table = new NPropertyTable(_header);
-        _mini_store     = new NPOIFSMiniStore(this, _property_table.getRoot(), new ArrayList<>(), _header);
+        _property_table = new PropertyTable(_header);
+        _mini_store     = new POIFSMiniStore(this, _property_table.getRoot(), new ArrayList<>(), _header);
         _xbat_blocks    = new ArrayList<>();
         _bat_blocks     = new ArrayList<>();
         _root           = null;
@@ -114,7 +117,7 @@ public class NPOIFSFileSystem extends Bl
     /**
      * Constructor, intended for writing
      */
-    public NPOIFSFileSystem()
+    public POIFSFileSystem()
     {
        this(true);
        
@@ -144,7 +147,7 @@ public class NPOIFSFileSystem extends Bl
      *
      * @exception IOException on errors reading, or on invalid data
      */
-    public NPOIFSFileSystem(File file)
+    public POIFSFileSystem(File file)
          throws IOException
     {
        this(file, true);
@@ -163,7 +166,7 @@ public class NPOIFSFileSystem extends Bl
      *
      * @exception IOException on errors reading, or on invalid data
      */
-    public NPOIFSFileSystem(File file, boolean readOnly)
+    public POIFSFileSystem(File file, boolean readOnly)
          throws IOException
     {
        this(null, file, readOnly, true);
@@ -182,7 +185,7 @@ public class NPOIFSFileSystem extends Bl
      *
      * @exception IOException on errors reading, or on invalid data
      */
-    public NPOIFSFileSystem(FileChannel channel)
+    public POIFSFileSystem(FileChannel channel)
          throws IOException
     {
        this(channel, true);
@@ -201,13 +204,13 @@ public class NPOIFSFileSystem extends Bl
      *
      * @exception IOException on errors reading, or on invalid data
      */
-    public NPOIFSFileSystem(FileChannel channel, boolean readOnly)
+    public POIFSFileSystem(FileChannel channel, boolean readOnly)
          throws IOException
     {
        this(channel, null, readOnly, false);
     }
     
-    private NPOIFSFileSystem(FileChannel channel, File srcFile, boolean readOnly, boolean closeChannelOnError)
+    private POIFSFileSystem(FileChannel channel, File srcFile, boolean readOnly, boolean closeChannelOnError)
          throws IOException
     {
        this(false);
@@ -240,7 +243,6 @@ public class NPOIFSFileSystem extends Bl
            //  still sticking to the iterator contract
            if (closeChannelOnError && channel != null) {
                channel.close();
-               channel = null;
            }
            throw e;
        }
@@ -275,53 +277,48 @@ public class NPOIFSFileSystem extends Bl
      * @exception IOException on errors reading, or on invalid data
      */
 
-    public NPOIFSFileSystem(InputStream stream)
+    public POIFSFileSystem(InputStream stream)
         throws IOException
     {
         this(false);
-        
-        ReadableByteChannel channel = null;
+
         boolean success = false;
-        
-        try {
-           // Turn our InputStream into something NIO based
-           channel = Channels.newChannel(stream);
-           
-           // Get the header
-           ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
-           IOUtils.readFully(channel, headerBuffer);
-           
-           // Have the header processed
-           _header = new HeaderBlock(headerBuffer);
-           
-           // Sanity check the block count
-           BlockAllocationTableReader.sanityCheckBlockCount(_header.getBATCount());
-   
-           // We need to buffer the whole file into memory when
-           //  working with an InputStream.
-           // The max possible size is when each BAT block entry is used
-           long maxSize = BATBlock.calculateMaximumSize(_header); 
-           if (maxSize > Integer.MAX_VALUE) {
-               throw new IllegalArgumentException("Unable read a >2gb file via an InputStream");
-           }
-           ByteBuffer data = ByteBuffer.allocate((int)maxSize);
-           
-           // Copy in the header
-           headerBuffer.position(0);
-           data.put(headerBuffer);
-           data.position(headerBuffer.capacity());
-           
-           // Now read the rest of the stream
-           IOUtils.readFully(channel, data);
-           success = true;
-           
-           // Turn it into a DataSource
-           _data = new ByteArrayBackedDataSource(data.array(), data.position());
+        try (ReadableByteChannel channel = Channels.newChannel(stream)) {
+            // Turn our InputStream into something NIO based
+
+            // Get the header
+            ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
+            IOUtils.readFully(channel, headerBuffer);
+
+            // Have the header processed
+            _header = new HeaderBlock(headerBuffer);
+
+            // Sanity check the block count
+            sanityCheckBlockCount(_header.getBATCount());
+
+            // We need to buffer the whole file into memory when
+            //  working with an InputStream.
+            // The max possible size is when each BAT block entry is used
+            long maxSize = BATBlock.calculateMaximumSize(_header);
+            if (maxSize > Integer.MAX_VALUE) {
+                throw new IllegalArgumentException("Unable read a >2gb file via an InputStream");
+            }
+            ByteBuffer data = ByteBuffer.allocate((int) maxSize);
+
+            // Copy in the header
+            headerBuffer.position(0);
+            data.put(headerBuffer);
+            data.position(headerBuffer.capacity());
+
+            // Now read the rest of the stream
+            IOUtils.readFully(channel, data);
+            success = true;
+
+            // Turn it into a DataSource
+            _data = new ByteArrayBackedDataSource(data.array(), data.position());
         } finally {
-           // As per the constructor contract, always close the stream
-           if(channel != null)
-              channel.close();
-           closeInputStream(stream, success);
+            // As per the constructor contract, always close the stream
+            closeInputStream(stream, success);
         }
         
         // Now process the various entries
@@ -388,12 +385,12 @@ public class NPOIFSFileSystem extends Bl
        
        // We're now able to load steams
        // Use this to read in the properties
-       _property_table = new NPropertyTable(_header, this);
+       _property_table = new PropertyTable(_header, this);
        
        // Finally read the Small Stream FAT (SBAT) blocks
        BATBlock sfat;
        List<BATBlock> sbats = new ArrayList<>();
-       _mini_store     = new NPOIFSMiniStore(this, _property_table.getRoot(), sbats, _header);
+       _mini_store     = new POIFSMiniStore(this, _property_table.getRoot(), sbats, _header);
        nextAt = _header.getSBATStart();
        for(int i=0; i<_header.getSBATCount() && nextAt != POIFSConstants.END_OF_CHAIN; i++) {
           loopDetector.claim(nextAt);
@@ -589,7 +586,7 @@ public class NPOIFSFileSystem extends Bl
      * For unit testing only! Returns the underlying
      *  properties table
      */
-    NPropertyTable _get_property_table() {
+    PropertyTable _get_property_table() {
       return _property_table;
     }
     
@@ -597,7 +594,7 @@ public class NPOIFSFileSystem extends Bl
      * Returns the MiniStore, which performs a similar low
      *  level function to this, except for the small blocks.
      */
-    public NPOIFSMiniStore getMiniStore() {
+    POIFSMiniStore getMiniStore() {
        return _mini_store;
     }
 
@@ -606,7 +603,7 @@ public class NPOIFSFileSystem extends Bl
      *
      * @param document the POIFSDocument being added
      */
-    void addDocument(final NPOIFSDocument document)
+    void addDocument(final POIFSDocument document)
     {
         _property_table.addProperty(document.getDocumentProperty());
     }
@@ -650,12 +647,10 @@ public class NPOIFSFileSystem extends Bl
      *
      * @return the new DocumentEntry
      *
-     * @exception IOException
+     * @exception IOException if the writer exceeds the given size
      */
-    public DocumentEntry createDocument(final String name, final int size,
-                                        final POIFSWriterListener writer)
-        throws IOException
-    {
+    public DocumentEntry createDocument(final String name, final int size, final POIFSWriterListener writer)
+    throws IOException {
         return getRoot().createDocument(name, size, writer);
     }
 
@@ -687,11 +682,9 @@ public class NPOIFSFileSystem extends Bl
      *
      * @exception IOException on error populating the POIFSDocument
      */
-
-    public DocumentEntry createOrUpdateDocument(final InputStream stream,
-                                                final String name)
-        throws IOException
-    {
+    @SuppressWarnings("UnusedReturnValue")
+    public DocumentEntry createOrUpdateDocument(final InputStream stream, final String name)
+    throws IOException {
         return getRoot().createOrUpdateDocument(name, stream);
     }
     
@@ -702,12 +695,7 @@ public class NPOIFSFileSystem extends Bl
      *  is supported.
      */
     public boolean isInPlaceWriteable() {
-        if(_data instanceof FileBackedDataSource) {
-            if ( ((FileBackedDataSource)_data).isWriteable() ) {
-                return true;
-            }
-        }
-        return false;
+        return (_data instanceof FileBackedDataSource) && ((FileBackedDataSource) _data).isWriteable();
     }
     
     /**
@@ -718,9 +706,7 @@ public class NPOIFSFileSystem extends Bl
      * @exception IOException thrown on errors writing to the stream
      */
     public void writeFilesystem() throws IOException {
-       if(_data instanceof FileBackedDataSource) {
-          // Good, correct type
-       } else {
+       if (!(_data instanceof FileBackedDataSource)) {
           throw new IllegalArgumentException(
                 "POIFS opened from an inputstream, so writeFilesystem() may " +
                 "not be called. Use writeFilesystem(OutputStream) instead"
@@ -761,24 +747,28 @@ public class NPOIFSFileSystem extends Bl
         _mini_store.syncWithDataSource();
         
         // Properties
-        NPOIFSStream propStream = new NPOIFSStream(this, _header.getPropertyStart());
+        POIFSStream propStream = new POIFSStream(this, _header.getPropertyStart());
         _property_table.preWrite();
         _property_table.write(propStream);
         // _header.setPropertyStart has been updated on write ...
         
-       // HeaderBlock
-       HeaderBlockWriter hbw = new HeaderBlockWriter(_header);
-       hbw.writeBlock( getBlockAt(-1) );
+        // HeaderBlock
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(
+                _header.getBigBlockSize().getBigBlockSize()
+        );
+        _header.writeData(baos);
+        getBlockAt(-1).put(baos.toByteArray());
+
        
        // BATs
        for(BATBlock bat : _bat_blocks) {
           ByteBuffer block = getBlockAt(bat.getOurBlockIndex());
-          BlockAllocationTableWriter.writeBlock(bat, block);
+          bat.writeData(block);
        }
        // XBats
        for(BATBlock bat : _xbat_blocks) {
            ByteBuffer block = getBlockAt(bat.getOurBlockIndex());
-           BlockAllocationTableWriter.writeBlock(bat, block);
+           bat.writeData(block);
         }
     }
     
@@ -796,8 +786,6 @@ public class NPOIFSFileSystem extends Bl
      *
      * @param args names of the files; arg[ 0 ] is the input file,
      *             arg[ 1 ] is the output file
-     *
-     * @exception IOException
      */
     public static void main(String args[]) throws IOException {
         if (args.length != 2) {
@@ -808,7 +796,7 @@ public class NPOIFSFileSystem extends Bl
 
         try (FileInputStream istream = new FileInputStream(args[0])) {
             try (FileOutputStream ostream = new FileOutputStream(args[1])) {
-                try (NPOIFSFileSystem fs = new NPOIFSFileSystem(istream)) {
+                try (POIFSFileSystem fs = new POIFSFileSystem(istream)) {
                     fs.writeFilesystem(ostream);
                 }
             }
@@ -850,7 +838,7 @@ public class NPOIFSFileSystem extends Bl
     void remove(EntryNode entry) throws IOException {
         // If it's a document, free the blocks
         if (entry instanceof DocumentEntry) {
-            NPOIFSDocument doc = new NPOIFSDocument((DocumentProperty)entry.getProperty(), this);
+            POIFSDocument doc = new POIFSDocument((DocumentProperty)entry.getProperty(), this);
             doc.free();
         }
         
@@ -925,17 +913,37 @@ public class NPOIFSFileSystem extends Bl
     /**
      * @return The Big Block size, normally 512 bytes, sometimes 4096 bytes
      */
+    @SuppressWarnings("WeakerAccess")
     public POIFSBigBlockSize getBigBlockSizeDetails() {
       return bigBlockSize;
     }
 
+    /**
+     * Creates a new {@link POIFSFileSystem} in a new {@link File}.
+     * Use {@link #POIFSFileSystem(File)} to open an existing File,
+     *  this should only be used to create a new empty filesystem.
+     *
+     * @param file The file to create and open
+     * @return The created and opened {@link POIFSFileSystem}
+     */
+    public static POIFSFileSystem create(File file) throws IOException {
+        // Create a new empty POIFS in the file
+        try (POIFSFileSystem tmp = new POIFSFileSystem();
+             OutputStream out = new FileOutputStream(file)) {
+            tmp.writeFilesystem(out);
+        }
+
+        // Open it up again backed by the file
+        return new POIFSFileSystem(file, false);
+    }
+
     @Override
     protected int getBlockStoreBlockSize() {
        return getBigBlockSize();
     }
 
     @Internal
-    public NPropertyTable getPropertyTable() {
+    public PropertyTable getPropertyTable() {
         return _property_table;
     }
 
@@ -943,5 +951,22 @@ public class NPOIFSFileSystem extends Bl
     public HeaderBlock getHeaderBlock() {
         return _header;
     }
+
+
+    private static void sanityCheckBlockCount(int block_count) throws IOException {
+        if (block_count <= 0) {
+            throw new IOException(
+                    "Illegal block count; minimum count is 1, got " +
+                            block_count + " instead"
+            );
+        }
+        if (block_count > MAX_BLOCK_COUNT) {
+            throw new IOException(
+                    "Block count " + block_count +
+                            " is too high. POI maximum is " + MAX_BLOCK_COUNT + "."
+            );
+        }
+    }
+
 }
 

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSMiniStore.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSMiniStore.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSMiniStore.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSMiniStore.java Fri Aug 31 00:25:50 2018
@@ -28,36 +28,35 @@ import org.apache.poi.poifs.common.POIFS
 import org.apache.poi.poifs.property.RootProperty;
 import org.apache.poi.poifs.storage.BATBlock;
 import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex;
-import org.apache.poi.poifs.storage.BlockAllocationTableWriter;
 import org.apache.poi.poifs.storage.HeaderBlock;
 
 /**
  * This class handles the MiniStream (small block store)
- *  in the NIO case for {@link NPOIFSFileSystem}
+ *  in the NIO case for {@link POIFSFileSystem}
  */
-public class NPOIFSMiniStore extends BlockStore
+public class POIFSMiniStore extends BlockStore
 {
-    private NPOIFSFileSystem _filesystem;
-    private NPOIFSStream     _mini_stream;
+    private POIFSFileSystem _filesystem;
+    private POIFSStream _mini_stream;
     private List<BATBlock>   _sbat_blocks;
     private HeaderBlock      _header;
     private RootProperty     _root;
 
-    protected NPOIFSMiniStore(NPOIFSFileSystem filesystem, RootProperty root,
-         List<BATBlock> sbats, HeaderBlock header)
+    POIFSMiniStore(POIFSFileSystem filesystem, RootProperty root,
+                             List<BATBlock> sbats, HeaderBlock header)
     {
        this._filesystem = filesystem;
        this._sbat_blocks = sbats;
        this._header = header;
        this._root = root;
        
-       this._mini_stream = new NPOIFSStream(filesystem, root.getStartBlock());
+       this._mini_stream = new POIFSStream(filesystem, root.getStartBlock());
     }
     
     /**
      * Load the block at the given offset.
      */
-    protected ByteBuffer getBlockAt(final int offset) throws IOException {
+    protected ByteBuffer getBlockAt(final int offset) {
        // Which big block is this?
        int byteOffset = offset * POIFSConstants.SMALL_BLOCK_SIZE;
        int bigBlockNumber = byteOffset / _filesystem.getBigBlockSize();
@@ -109,7 +108,7 @@ public class NPOIFSMiniStore extends Blo
        // If we are the first block to be allocated, initialise the stream
        if (firstInStore) {
            _filesystem._get_property_table().getRoot().setStartBlock(newBigBlock);
-           _mini_stream = new NPOIFSStream(_filesystem, newBigBlock);
+           _mini_stream = new POIFSStream(_filesystem, newBigBlock);
        } else {
            // Tack it onto the end of our chain
            ChainLoopDetector loopDetector = _filesystem.getChainLoopDetector();
@@ -232,7 +231,7 @@ public class NPOIFSMiniStore extends Blo
     }
     
     @Override
-    protected ChainLoopDetector getChainLoopDetector() throws IOException {
+    protected ChainLoopDetector getChainLoopDetector() {
       return new ChainLoopDetector( _root.getSize() );
     }
 
@@ -245,12 +244,12 @@ public class NPOIFSMiniStore extends Blo
      *  the mini-stream size in the properties. Stream size is
      *  based on full blocks used, not the data within the streams
      */
-    protected void syncWithDataSource() throws IOException {
+    void syncWithDataSource() throws IOException {
        int blocksUsed = 0;
        for (BATBlock sbat : _sbat_blocks) {
           ByteBuffer block = _filesystem.getBlockAt(sbat.getOurBlockIndex());
-          BlockAllocationTableWriter.writeBlock(sbat, block);
-          
+          sbat.writeData(block);
+
           if (!sbat.hasFreeSectors()) {
               blocksUsed += _filesystem.getBigBlockSizeDetails().getBATEntriesPerBlock();
           } else {

Copied: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java?p2=poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java&p1=poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java Fri Aug 31 00:25:50 2018
@@ -31,7 +31,7 @@ import org.apache.poi.poifs.storage.Head
 
 /**
  * This handles reading and writing a stream within a
- *  {@link NPOIFSFileSystem}. It can supply an iterator
+ *  {@link POIFSFileSystem}. It can supply an iterator
  *  to read blocks, and way to write out to existing and
  *  new blocks.
  * Most users will want a higher level version of this, 
@@ -44,7 +44,7 @@ import org.apache.poi.poifs.storage.Head
  * TODO Implement a streaming write method, and append
  */
 
-public class NPOIFSStream implements Iterable<ByteBuffer>
+public class POIFSStream implements Iterable<ByteBuffer>
 {
 	private BlockStore blockStore;
 	private int startBlock;
@@ -55,7 +55,7 @@ public class NPOIFSStream implements Ite
 	 *  to know how to get the start block (eg from a 
 	 *  {@link HeaderBlock} or a {@link Property}) 
 	 */
-	public NPOIFSStream(BlockStore blockStore, int startBlock) {
+	public POIFSStream(BlockStore blockStore, int startBlock) {
 	   this.blockStore = blockStore;
 	   this.startBlock = startBlock;
 	}
@@ -64,7 +64,7 @@ public class NPOIFSStream implements Ite
 	 * Constructor for a new stream. A start block won't
 	 *  be allocated until you begin writing to it.
 	 */
-	public NPOIFSStream(BlockStore blockStore) {
+	public POIFSStream(BlockStore blockStore) {
       this.blockStore = blockStore;
 	   this.startBlock = POIFSConstants.END_OF_CHAIN;
 	}
@@ -86,7 +86,7 @@ public class NPOIFSStream implements Ite
       return getBlockIterator();
    }
 	
-   public Iterator<ByteBuffer> getBlockIterator() {
+   Iterator<ByteBuffer> getBlockIterator() {
       if(startBlock == POIFSConstants.END_OF_CHAIN) {
          throw new IllegalStateException(
                "Can't read from a new stream before it has been written to"
@@ -101,7 +101,7 @@ public class NPOIFSStream implements Ite
     * Note - if this is property based, you'll still
     *  need to update the size in the property yourself
     */
-   public void updateContents(byte[] contents) throws IOException {
+   void updateContents(byte[] contents) throws IOException {
        OutputStream os = getOutputStream();
        os.write(contents);
        os.close();
@@ -143,7 +143,7 @@ public class NPOIFSStream implements Ite
       private ChainLoopDetector loopDetector;
       private int nextBlock;
       
-      protected StreamBlockByteBufferIterator(int firstBlock) {
+      StreamBlockByteBufferIterator(int firstBlock) {
          this.nextBlock = firstBlock;
          try {
             this.loopDetector = blockStore.getChainLoopDetector();
@@ -153,10 +153,7 @@ public class NPOIFSStream implements Ite
       }
 
       public boolean hasNext() {
-         if(nextBlock == POIFSConstants.END_OF_CHAIN) {
-            return false;
-         }
-         return true;
+          return nextBlock != POIFSConstants.END_OF_CHAIN;
       }
 
       public ByteBuffer next() {
@@ -187,13 +184,13 @@ public class NPOIFSStream implements Ite
        ChainLoopDetector loopDetector;
        int prevBlock, nextBlock;
 
-       protected StreamBlockByteBuffer() throws IOException {
+       StreamBlockByteBuffer() throws IOException {
            loopDetector = blockStore.getChainLoopDetector();
            prevBlock = POIFSConstants.END_OF_CHAIN;
            nextBlock = startBlock;
        }
 
-       protected void createBlockIfNeeded() throws IOException {
+       void createBlockIfNeeded() throws IOException {
            if (buffer != null && buffer.hasRemaining()) return;
            
            int thisBlock = nextBlock;
@@ -228,12 +225,14 @@ public class NPOIFSStream implements Ite
            // Update pointers
            prevBlock = thisBlock;
        }
-       
+
+       @Override
        public void write(int b) throws IOException {
             oneByte[0] = (byte)(b & 0xFF);
             write(oneByte);
        }
-    
+
+       @Override
         public void write(byte[] b, int off, int len) throws IOException {
             if ((off < 0) || (off > b.length) || (len < 0) ||
                     ((off + len) > b.length) || ((off + len) < 0)) {
@@ -253,7 +252,7 @@ public class NPOIFSStream implements Ite
     
         public void close() throws IOException {
             // If we're overwriting, free any remaining blocks
-            NPOIFSStream toFree = new NPOIFSStream(blockStore, nextBlock);
+            POIFSStream toFree = new POIFSStream(blockStore, nextBlock);
             toFree.free(loopDetector);
             
             // Mark the end of the stream, if we have any data

Modified: poi/trunk/src/java/org/apache/poi/poifs/macros/VBAMacroReader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/macros/VBAMacroReader.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/macros/VBAMacroReader.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/macros/VBAMacroReader.java Fri Aug 31 00:25:50 2018
@@ -32,7 +32,7 @@ import org.apache.poi.poifs.filesystem.D
 import org.apache.poi.poifs.filesystem.DocumentNode;
 import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.poifs.filesystem.FileMagic;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
 import org.apache.poi.poifs.macros.Module.ModuleType;
 import org.apache.poi.util.CodePageUtil;
@@ -59,13 +59,13 @@ public class VBAMacroReader implements C
     protected static final String VBA_PROJECT_OOXML = "vbaProject.bin";
     protected static final String VBA_PROJECT_POIFS = "VBA";
 
-    private NPOIFSFileSystem fs;
+    private POIFSFileSystem fs;
     
     public VBAMacroReader(InputStream rstream) throws IOException {
         InputStream is = FileMagic.prepareToCheckMagic(rstream);
         FileMagic fm = FileMagic.valueOf(is);
         if (fm == FileMagic.OLE2) {
-            fs = new NPOIFSFileSystem(is);
+            fs = new POIFSFileSystem(is);
         } else {
             openOOXML(is);
         }
@@ -73,12 +73,12 @@ public class VBAMacroReader implements C
     
     public VBAMacroReader(File file) throws IOException {
         try {
-            this.fs = new NPOIFSFileSystem(file);
+            this.fs = new POIFSFileSystem(file);
         } catch (OfficeXmlFileException e) {
             openOOXML(new FileInputStream(file));
         }
     }
-    public VBAMacroReader(NPOIFSFileSystem fs) {
+    public VBAMacroReader(POIFSFileSystem fs) {
         this.fs = fs;
     }
     
@@ -89,7 +89,7 @@ public class VBAMacroReader implements C
                 if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) {
                     try {
                         // Make a NPOIFS from the contents, and close the stream
-                        this.fs = new NPOIFSFileSystem(zis);
+                        this.fs = new POIFSFileSystem(zis);
                         return;
                     } catch (IOException e) {
                         // Tidy up

Modified: poi/trunk/src/java/org/apache/poi/poifs/property/DocumentProperty.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/property/DocumentProperty.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/property/DocumentProperty.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/property/DocumentProperty.java Fri Aug 31 00:25:50 2018
@@ -19,14 +19,14 @@
 
 package org.apache.poi.poifs.property;
 
-import org.apache.poi.poifs.filesystem.NPOIFSDocument;
+import org.apache.poi.poifs.filesystem.POIFSDocument;
 
 /**
  * Trivial extension of Property for POIFSDocuments
  */
 public class DocumentProperty extends Property {
     // the POIFSDocument this property is associated with
-    private NPOIFSDocument _document;
+    private POIFSDocument _document;
 
     /**
      * Constructor
@@ -64,7 +64,7 @@ public class DocumentProperty extends Pr
      *
      * @param doc the associated POIFSDocument
      */
-    public void setDocument(NPOIFSDocument doc)
+    public void setDocument(POIFSDocument doc)
     {
         _document = doc;
     }
@@ -74,7 +74,7 @@ public class DocumentProperty extends Pr
      *
      * @return the associated document
      */
-    public NPOIFSDocument getDocument()
+    public POIFSDocument getDocument()
     {
         return _document;
     }

Modified: poi/trunk/src/java/org/apache/poi/poifs/property/PropertyFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/property/PropertyFactory.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/property/PropertyFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/property/PropertyFactory.java Fri Aug 31 00:25:50 2018
@@ -19,12 +19,9 @@
 
 package org.apache.poi.poifs.property;
 
-import java.io.IOException;
-
-import java.util.*;
+import java.util.List;
 
 import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.storage.ListManagedBlock;
 
 /**
  * Factory for turning an array of RawDataBlock instances containing
@@ -38,37 +35,13 @@ import org.apache.poi.poifs.storage.List
  * @author Marc Johnson (mjohnson at apache dot org)
  */
 
-class PropertyFactory {
+final class PropertyFactory {
     // no need for an accessible constructor
     private PropertyFactory()
     {
     }
 
-    /**
-     * Convert raw data blocks to an array of Property's
-     *
-     * @param blocks to be converted
-     *
-     * @return the converted List of Property objects. May contain
-     *         nulls, but will not be null
-     *
-     * @exception IOException if any of the blocks are empty
-     */
-    static List<Property> convertToProperties(ListManagedBlock [] blocks)
-        throws IOException
-    {
-        List<Property> properties = new ArrayList<>();
-
-        for (ListManagedBlock block : blocks) {
-            byte[] data = block.getData();
-            convertToProperties(data, properties);
-        }
-        return properties;
-    }
-    
-    static void convertToProperties(byte[] data, List<Property> properties)
-        throws IOException
-    {
+    static void convertToProperties(byte[] data, List<Property> properties) {
        int property_count = data.length / POIFSConstants.PROPERTY_SIZE;
        int offset         = 0;
 

Copied: poi/trunk/src/java/org/apache/poi/poifs/property/PropertyTable.java (from r1839708, poi/trunk/src/java/org/apache/poi/poifs/property/NPropertyTable.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/property/PropertyTable.java?p2=poi/trunk/src/java/org/apache/poi/poifs/property/PropertyTable.java&p1=poi/trunk/src/java/org/apache/poi/poifs/property/NPropertyTable.java&r1=1839708&r2=1839709&rev=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/property/NPropertyTable.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/property/PropertyTable.java Fri Aug 31 00:25:50 2018
@@ -21,35 +21,41 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
+import java.util.Stack;
 
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
-import org.apache.poi.poifs.filesystem.NPOIFSStream;
+import org.apache.poi.poifs.filesystem.BATManaged;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSStream;
 import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 
 /**
- * This class embodies the Property Table for a {@link NPOIFSFileSystem}; 
- *  this is basically the directory for all of the documents in the
- * filesystem.
+ * This class embodies the Property Table for a {@link POIFSFileSystem};
+ * this is basically the directory for all of the documents in the
+ * filesystem and looks up entries in the filesystem to their
+ * chain of blocks.
  */
-public final class NPropertyTable extends PropertyTableBase {
+public final class PropertyTable implements BATManaged {
     private static final POILogger _logger =
-       POILogFactory.getLogger(NPropertyTable.class);
+       POILogFactory.getLogger(PropertyTable.class);
+
     //arbitrarily selected; may need to increase
     private static final int MAX_RECORD_LENGTH = 100_000;
 
-    private POIFSBigBlockSize _bigBigBlockSize;
+    private final HeaderBlock    _header_block;
+    private final List<Property> _properties = new ArrayList<>();
+    private final POIFSBigBlockSize _bigBigBlockSize;
 
-    public NPropertyTable(HeaderBlock headerBlock)
+    public PropertyTable(HeaderBlock headerBlock)
     {
-        super(headerBlock);
+        _header_block = headerBlock;
         _bigBigBlockSize = headerBlock.getBigBlockSize();
+        addProperty(new RootProperty());
     }
 
     /**
@@ -63,60 +69,104 @@ public final class NPropertyTable extend
      * @exception IOException if anything goes wrong (which should be
      *            a result of the input being NFG)
      */
-    public NPropertyTable(final HeaderBlock headerBlock,
-                          final NPOIFSFileSystem filesystem)
-        throws IOException
-    {
-        super(
+    public PropertyTable(final HeaderBlock headerBlock, final POIFSFileSystem filesystem)
+    throws IOException {
+        this(
               headerBlock,
-              buildProperties(
-                    (new NPOIFSStream(filesystem, headerBlock.getPropertyStart())).iterator(),
-                    headerBlock.getBigBlockSize()
-              )
+              new POIFSStream(filesystem, headerBlock.getPropertyStart())
         );
+    }
+
+    /* only invoked locally and from the junit tests */
+    PropertyTable(final HeaderBlock headerBlock, final Iterable<ByteBuffer> dataSource)
+    throws IOException {
+        _header_block = headerBlock;
         _bigBigBlockSize = headerBlock.getBigBlockSize();
+
+        for (ByteBuffer bb : dataSource) {
+            // Turn it into an array
+            byte[] data;
+            if (bb.hasArray() && bb.arrayOffset() == 0 &&
+                    bb.array().length == _bigBigBlockSize.getBigBlockSize()) {
+                data = bb.array();
+            } else {
+                data = IOUtils.safelyAllocate(_bigBigBlockSize.getBigBlockSize(), MAX_RECORD_LENGTH);
+
+                int toRead = data.length;
+                if (bb.remaining() < _bigBigBlockSize.getBigBlockSize()) {
+                    // Looks to be a truncated block
+                    // This isn't allowed, but some third party created files
+                    //  sometimes do this, and we can normally read anyway
+                    _logger.log(POILogger.WARN, "Short Property Block, ", bb.remaining(),
+                            " bytes instead of the expected " + _bigBigBlockSize.getBigBlockSize());
+                    toRead = bb.remaining();
+                }
+
+                bb.get(data, 0, toRead);
+            }
+
+            PropertyFactory.convertToProperties(data, _properties);
+        }
+
+        populatePropertyTree( (DirectoryProperty)_properties.get(0));
     }
-    
-    private static List<Property> buildProperties(final Iterator<ByteBuffer> dataSource,
-          final POIFSBigBlockSize bigBlockSize) throws IOException
-    {
-       List<Property> properties = new ArrayList<>();
-       while(dataSource.hasNext()) {
-          ByteBuffer bb = dataSource.next();
-          
-          // Turn it into an array
-          byte[] data;
-          if(bb.hasArray() && bb.arrayOffset() == 0 && 
-                bb.array().length == bigBlockSize.getBigBlockSize()) {
-             data = bb.array();
-          } else {
-             data = IOUtils.safelyAllocate(bigBlockSize.getBigBlockSize(), MAX_RECORD_LENGTH);
-             
-             int toRead = data.length;
-             if (bb.remaining() < bigBlockSize.getBigBlockSize()) {
-                // Looks to be a truncated block
-                // This isn't allowed, but some third party created files
-                //  sometimes do this, and we can normally read anyway
-                _logger.log(POILogger.WARN, "Short Property Block, ", bb.remaining(),
-                            " bytes instead of the expected " + bigBlockSize.getBigBlockSize());
-                toRead = bb.remaining();
-             }
-             
-             bb.get(data, 0, toRead);
-          }
-          
-          PropertyFactory.convertToProperties(data, properties);
-       }
-       return properties;
+
+
+    /**
+     * Add a property to the list of properties we manage
+     *
+     * @param property the new Property to manage
+     */
+    public void addProperty(Property property) {
+        _properties.add(property);
+    }
+
+    /**
+     * Remove a property from the list of properties we manage
+     *
+     * @param property the Property to be removed
+     */
+    public void removeProperty(final Property property) {
+        _properties.remove(property);
+    }
+
+    /**
+     * Get the root property
+     *
+     * @return the root property
+     */
+    public RootProperty getRoot() {
+        // it's always the first element in the List
+        return ( RootProperty ) _properties.get(0);
     }
 
     /**
+     * Get the start block for the property table
+     *
+     * @return start block index
+     */
+    public int getStartBlock() {
+        return _header_block.getPropertyStart();
+    }
+
+    /**
+     * Set the start block for this instance
+     *
+     * @param index index into the array of BigBlock instances making
+     *              up the the filesystem
+     */
+    public void setStartBlock(final int index) {
+        _header_block.setPropertyStart(index);
+    }
+
+
+
+    /**
      * Return the number of BigBlock's this instance uses
      *
      * @return count of BigBlock instances
      */
-    public int countBlocks()
-    {
+    public int countBlocks() {
        long rawSize = _properties.size() * (long)POIFSConstants.PROPERTY_SIZE;
        int blkSize = _bigBigBlockSize.getBigBlockSize();
        int numBlocks = (int)(rawSize / blkSize);
@@ -147,7 +197,7 @@ public final class NPropertyTable extend
     /**
      * Writes the properties out into the given low-level stream
      */
-    public void write(NPOIFSStream stream) throws IOException {
+    public void write(POIFSStream stream) throws IOException {
        OutputStream os = stream.getOutputStream();
        for(Property property : _properties) {
           if(property != null) {
@@ -161,4 +211,47 @@ public final class NPropertyTable extend
           setStartBlock(stream.getStartBlock());
        }
     }
+
+    private void populatePropertyTree(DirectoryProperty root) throws IOException {
+        int index = root.getChildIndex();
+
+        if (!Property.isValidIndex(index)) {
+            // property has no children
+            return;
+        }
+
+        final Stack<Property> children = new Stack<>();
+        children.push(_properties.get(index));
+        while (!children.empty()) {
+            Property property = children.pop();
+            if (property == null) {
+                // unknown / unsupported / corrupted property, skip
+                continue;
+            }
+
+            root.addChild(property);
+            if (property.isDirectory()) {
+                populatePropertyTree(( DirectoryProperty ) property);
+            }
+            index = property.getPreviousChildIndex();
+            if (isValidIndex(index)) {
+                children.push(_properties.get(index));
+            }
+            index = property.getNextChildIndex();
+            if (isValidIndex(index)) {
+                children.push(_properties.get(index));
+            }
+        }
+    }
+
+    private boolean isValidIndex(int index) {
+        if (! Property.isValidIndex(index))
+            return false;
+        if (index < 0 || index >= _properties.size()) {
+            _logger.log(POILogger.WARN, "Property index " + index +
+                    "outside the valid range 0.."+_properties.size());
+            return false;
+        }
+        return true;
+    }
 }

Modified: poi/trunk/src/java/org/apache/poi/poifs/storage/BATBlock.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/storage/BATBlock.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/storage/BATBlock.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/storage/BATBlock.java Fri Aug 31 00:25:50 2018
@@ -31,7 +31,13 @@ import org.apache.poi.util.LittleEndian;
  * A block of block allocation table entries. BATBlocks are created
  * only through a static factory method: createBATBlocks.
  */
-public final class BATBlock extends BigBlock {
+public final class BATBlock implements BlockWritable {
+    /**
+     * Either 512 bytes ({@link POIFSConstants#SMALLER_BIG_BLOCK_SIZE})
+     *  or 4096 bytes ({@link POIFSConstants#LARGER_BIG_BLOCK_SIZE})
+     */
+    private POIFSBigBlockSize bigBlockSize;
+
     /**
      * For a regular fat block, these are 128 / 1024 
      *  next sector values.
@@ -55,7 +61,7 @@ public final class BATBlock extends BigB
      */
     private BATBlock(POIFSBigBlockSize bigBlockSize)
     {
-        super(bigBlockSize);
+        this.bigBlockSize = bigBlockSize;
         
         int _entries_per_block = bigBlockSize.getBATEntriesPerBlock();
         _values = new int[_entries_per_block];
@@ -64,39 +70,14 @@ public final class BATBlock extends BigB
         Arrays.fill(_values, POIFSConstants.UNUSED_BLOCK);
     }
 
-    /**
-     * Create a single instance initialized (perhaps partially) with entries
-     *
-     * @param entries the array of block allocation table entries
-     * @param start_index the index of the first entry to be written
-     *                    to the block
-     * @param end_index the index, plus one, of the last entry to be
-     *                  written to the block (writing is for all index
-     *                  k, start_index <= k < end_index)
-     */
-
-    private BATBlock(POIFSBigBlockSize bigBlockSize, final int [] entries,
-                     final int start_index, final int end_index)
-    {
-        this(bigBlockSize);
-        for (int k = start_index; k < end_index; k++) {
-           _values[k - start_index] = entries[k];
-        }
-        
-        // Do we have any free sectors?
-        if(end_index - start_index == _values.length) {
-           recomputeFree();
-        }
-    }
-    
     private void recomputeFree() {
        boolean hasFree = false;
-       for(int k=0; k<_values.length; k++) {
-          if(_values[k] == POIFSConstants.UNUSED_BLOCK) {
-             hasFree = true;
-             break;
-          }
-       }
+        for (int _value : _values) {
+            if (_value == POIFSConstants.UNUSED_BLOCK) {
+                hasFree = true;
+                break;
+            }
+        }
        _has_free_sectors = hasFree;
     }
 
@@ -127,109 +108,13 @@ public final class BATBlock extends BigB
     public static BATBlock createEmptyBATBlock(final POIFSBigBlockSize bigBlockSize, boolean isXBAT) {
        BATBlock block = new BATBlock(bigBlockSize);
        if(isXBAT) {
-          block.setXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN);
+           final int _entries_per_xbat_block = bigBlockSize.getXBATEntriesPerBlock();
+           block._values[ _entries_per_xbat_block ] = POIFSConstants.END_OF_CHAIN;
        }
        return block;
     }
 
     /**
-     * Create an array of BATBlocks from an array of int block
-     * allocation table entries
-     *
-     * @param entries the array of int entries
-     *
-     * @return the newly created array of BATBlocks
-     */
-    public static BATBlock [] createBATBlocks(final POIFSBigBlockSize bigBlockSize, final int [] entries)
-    {
-        int        block_count = calculateStorageRequirements(bigBlockSize, entries.length);
-        BATBlock[] blocks      = new BATBlock[ block_count ];
-        int        index       = 0;
-        int        remaining   = entries.length;
-
-        int _entries_per_block = bigBlockSize.getBATEntriesPerBlock();
-        for (int j = 0; j < entries.length; j += _entries_per_block)
-        {
-            blocks[ index++ ] = new BATBlock(bigBlockSize, entries, j,
-                                             (remaining > _entries_per_block)
-                                             ? j + _entries_per_block
-                                             : entries.length);
-            remaining         -= _entries_per_block;
-        }
-        return blocks;
-    }
-    
-    /**
-     * Create an array of XBATBlocks from an array of int block
-     * allocation table entries
-     *
-     * @param entries the array of int entries
-     * @param startBlock the start block of the array of XBAT blocks
-     *
-     * @return the newly created array of BATBlocks
-     */
-
-    public static BATBlock [] createXBATBlocks(final POIFSBigBlockSize bigBlockSize,
-                                               final int [] entries,
-                                               final int startBlock)
-    {
-        int        block_count =
-            calculateXBATStorageRequirements(bigBlockSize, entries.length);
-        BATBlock[] blocks      = new BATBlock[ block_count ];
-        int        index       = 0;
-        int        remaining   = entries.length;
-
-        int _entries_per_xbat_block = bigBlockSize.getXBATEntriesPerBlock();
-        if (block_count != 0)
-        {
-            for (int j = 0; j < entries.length; j += _entries_per_xbat_block)
-            {
-                blocks[ index++ ] =
-                    new BATBlock(bigBlockSize, entries, j,
-                                 (remaining > _entries_per_xbat_block)
-                                 ? j + _entries_per_xbat_block
-                                 : entries.length);
-                remaining         -= _entries_per_xbat_block;
-            }
-            for (index = 0; index < blocks.length - 1; index++)
-            {
-                blocks[ index ].setXBATChain(bigBlockSize, startBlock + index + 1);
-            }
-            blocks[ index ].setXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN);
-        }
-        return blocks;
-    }
-
-    /**
-     * Calculate how many BATBlocks are needed to hold a specified
-     * number of BAT entries.
-     *
-     * @param entryCount the number of entries
-     *
-     * @return the number of BATBlocks needed
-     */
-    public static int calculateStorageRequirements(final POIFSBigBlockSize bigBlockSize, final int entryCount)
-    {
-        int _entries_per_block = bigBlockSize.getBATEntriesPerBlock();
-        return (entryCount + _entries_per_block - 1) / _entries_per_block;
-    }
-
-    /**
-     * Calculate how many XBATBlocks are needed to hold a specified
-     * number of BAT entries.
-     *
-     * @param entryCount the number of entries
-     *
-     * @return the number of XBATBlocks needed
-     */
-    public static int calculateXBATStorageRequirements(final POIFSBigBlockSize bigBlockSize, final int entryCount)
-    {
-        int _entries_per_xbat_block = bigBlockSize.getXBATEntriesPerBlock();
-        return (entryCount + _entries_per_xbat_block - 1)
-               / _entries_per_xbat_block;
-    }
-    
-    /**
      * Calculates the maximum size of a file which is addressable given the
      *  number of FAT (BAT) sectors specified. (We don't care if those BAT
      *  blocks come from the 109 in the header, or from header + XBATS, it
@@ -280,19 +165,7 @@ public final class BATBlock extends BigB
      */
     public static BATBlockAndIndex getSBATBlockAndIndex(final int offset, 
           final HeaderBlock header, final List<BATBlock> sbats) {
-       POIFSBigBlockSize bigBlockSize = header.getBigBlockSize();
-       int entriesPerBlock = bigBlockSize.getBATEntriesPerBlock();
-       
-       // SBATs are so much easier, as they're chained streams
-       int whichSBAT = offset / entriesPerBlock;
-       int index = offset % entriesPerBlock;
-       return new BATBlockAndIndex( index, sbats.get(whichSBAT) );
-    }
-    
-    private void setXBATChain(final POIFSBigBlockSize bigBlockSize, int chainIndex)
-    {
-        int _entries_per_xbat_block = bigBlockSize.getXBATEntriesPerBlock();
-        _values[ _entries_per_xbat_block ] = chainIndex;
+        return getBATBlockAndIndex(offset, header, sbats);
     }
     
     /**
@@ -354,10 +227,7 @@ public final class BATBlock extends BigB
        return ourBlockIndex;
     }
 
-
-    /* ********** START extension of BigBlock ********** */
-
-   /**
+    /**
      * Write the block's data to an OutputStream
      *
      * @param stream the OutputStream to which the stored data should
@@ -366,16 +236,13 @@ public final class BATBlock extends BigB
      * @exception IOException on problems writing to the specified
      *            stream
      */
-    void writeData(final OutputStream stream)
-        throws IOException
-    {
-       // Save it out
-       stream.write( serialize() );
+
+    public void writeBlocks(final OutputStream stream) throws IOException {
+        // Save it out
+        stream.write( serialize() );
     }
-    
-    void writeData(final ByteBuffer block)
-        throws IOException
-    {
+
+    public void writeData(final ByteBuffer block) {
        // Save it out
        block.put( serialize() );
     }
@@ -384,21 +251,18 @@ public final class BATBlock extends BigB
        // Create the empty array
        byte[] data = new byte[ bigBlockSize.getBigBlockSize() ];
        
-       // Fill in the values
-       int offset = 0;
-       for(int i=0; i<_values.length; i++) {
-          LittleEndian.putInt(data, offset, _values[i]);
-          offset += LittleEndian.INT_SIZE;
-       }
+        // Fill in the values
+        int offset = 0;
+        for (int _value : _values) {
+            LittleEndian.putInt(data, offset, _value);
+            offset += LittleEndian.INT_SIZE;
+        }
        
        // Done
        return data;
     }
 
-    /* **********  END  extension of BigBlock ********** */
-    
-    
-    public static class BATBlockAndIndex {
+    public static final class BATBlockAndIndex {
        private final int index;
        private final BATBlock block;
        private BATBlockAndIndex(int index, BATBlock block) {

Modified: poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java Fri Aug 31 00:25:50 2018
@@ -214,12 +214,12 @@ public final class HeaderBlock implement
       byte[] data = new byte[512];
       int bsCount = IOUtils.readFully(stream, data);
       if(bsCount != 512) {
-         throw alertShortRead(bsCount, 512);
+         throw alertShortRead(bsCount);
       }
       return data;
 	}
 
-	private static IOException alertShortRead(int pRead, int expectedReadSize) {
+	private static IOException alertShortRead(int pRead) {
 		int read;
 		if (pRead < 0) {
 			//Can't have -1 bytes read in the error message!
@@ -230,8 +230,7 @@ public final class HeaderBlock implement
 		String type = " byte" + (read == 1 ? (""): ("s"));
 
 		return new IOException("Unable to read entire header; "
-				+ read + type + " read; expected "
-				+ expectedReadSize + " bytes");
+				+ read + type + " read; expected 512 bytes");
 	}
 
 	/**
@@ -372,7 +371,7 @@ public final class HeaderBlock implement
     * @exception IOException on problems writing to the specified
     *            stream
     */
-   void writeData(final OutputStream stream) throws IOException {
+   public void writeData(final OutputStream stream) throws IOException {
       // Update the counts and start positions 
       new IntegerField(_bat_count_offset,      _bat_count, _data);
       new IntegerField(_property_start_offset, _property_start, _data);

Modified: poi/trunk/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java Fri Aug 31 00:25:50 2018
@@ -30,7 +30,7 @@ import org.apache.poi.poifs.crypt.Decryp
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
 import org.apache.poi.poifs.filesystem.FileMagic;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
 import org.apache.poi.util.IOUtils;
 
@@ -38,7 +38,7 @@ public class SlideShowFactory {
     /**
      * Creates a SlideShow from the given NPOIFSFileSystem.
      *
-     * @param fs The {@link NPOIFSFileSystem} to read the document from
+     * @param fs The {@link POIFSFileSystem} to read the document from
      *
      * @return The created SlideShow
      *
@@ -47,7 +47,7 @@ public class SlideShowFactory {
     public static <
         S extends Shape<S,P>,
         P extends TextParagraph<S,P,? extends TextRun>
-    > SlideShow<S,P> create(NPOIFSFileSystem fs) throws IOException {
+    > SlideShow<S,P> create(POIFSFileSystem fs) throws IOException {
         return create(fs, null);
     }
 
@@ -55,7 +55,7 @@ public class SlideShowFactory {
      * Creates a SlideShow from the given NPOIFSFileSystem, which may
      * be password protected
      *
-     * @param fs The {@link NPOIFSFileSystem} to read the document from
+     * @param fs The {@link POIFSFileSystem} to read the document from
      * @param password The password that should be used or null if no password is necessary.
      *
      * @return The created SlideShow
@@ -65,7 +65,7 @@ public class SlideShowFactory {
     public static <
         S extends Shape<S,P>,
         P extends TextParagraph<S,P,? extends TextRun>
-    > SlideShow<S,P> create(final NPOIFSFileSystem fs, String password) throws IOException {
+    > SlideShow<S,P> create(final POIFSFileSystem fs, String password) throws IOException {
         return create(fs.getRoot(), password);
     }
 
@@ -188,7 +188,7 @@ public class SlideShowFactory {
         
         switch (fm) {
         case OLE2:
-            NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
+            POIFSFileSystem fs = new POIFSFileSystem(is);
             return create(fs, password);
         case OOXML:
             return createXSLFSlideShow(is);
@@ -264,9 +264,9 @@ public class SlideShowFactory {
             throw new FileNotFoundException(file.toString());
         }
 
-        NPOIFSFileSystem fs = null;
+        POIFSFileSystem fs = null;
         try {
-            fs = new NPOIFSFileSystem(file, readOnly);
+            fs = new POIFSFileSystem(file, readOnly);
             return create(fs, password);
         } catch(OfficeXmlFileException e) {
             IOUtils.closeQuietly(fs);

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java Fri Aug 31 00:25:50 2018
@@ -32,7 +32,7 @@ import org.apache.poi.poifs.crypt.Decryp
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
 import org.apache.poi.poifs.filesystem.FileMagic;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Removal;
@@ -49,13 +49,13 @@ public class WorkbookFactory {
      * Note that in order to properly release resources the
      * Workbook should be closed after use.
      *
-     * @param fs The {@link NPOIFSFileSystem} to read the document from
+     * @param fs The {@link POIFSFileSystem} to read the document from
      *
      * @return The created workbook
      *
      * @throws IOException if an error occurs while reading the data
      */
-    public static Workbook create(NPOIFSFileSystem fs) throws IOException {
+    public static Workbook create(POIFSFileSystem fs) throws IOException {
         return create(fs, null);
     }
 
@@ -63,14 +63,14 @@ public class WorkbookFactory {
      * Creates a Workbook from the given NPOIFSFileSystem, which may
      *  be password protected
      *
-     *  @param fs The {@link NPOIFSFileSystem} to read the document from
+     *  @param fs The {@link POIFSFileSystem} to read the document from
      *  @param password The password that should be used or null if no password is necessary.
      *
      *  @return The created Workbook
      *
      *  @throws IOException if an error occurs while reading the data
      */
-    private static Workbook create(final NPOIFSFileSystem fs, String password) throws IOException {
+    private static Workbook create(final POIFSFileSystem fs, String password) throws IOException {
         return create(fs.getRoot(), password);
     }
 
@@ -208,7 +208,7 @@ public class WorkbookFactory {
 
         switch (fm) {
             case OLE2:
-                NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
+                POIFSFileSystem fs = new POIFSFileSystem(is);
                 return create(fs, password);
             case OOXML:
                 return createXSSFWorkbook(is);
@@ -275,9 +275,9 @@ public class WorkbookFactory {
             throw new FileNotFoundException(file.toString());
         }
 
-        NPOIFSFileSystem fs = null;
+        POIFSFileSystem fs = null;
         try {
-            fs = new NPOIFSFileSystem(file, readOnly);
+            fs = new POIFSFileSystem(file, readOnly);
             return create(fs, password);
         } catch(OfficeXmlFileException e) {
             IOUtils.closeQuietly(fs);

Modified: poi/trunk/src/java/org/apache/poi/util/DrawingDump.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/DrawingDump.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/DrawingDump.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/DrawingDump.java Fri Aug 31 00:25:50 2018
@@ -26,18 +26,20 @@ import java.nio.charset.Charset;
 
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.ss.usermodel.Sheet;
 
 /**
  * Dump out the aggregated escher records
  */
-public class DrawingDump
-{
+public final class DrawingDump {
+    private DrawingDump() {
+    }
+
     public static void main( String[] args ) throws IOException {
         OutputStreamWriter osw = new OutputStreamWriter(System.out, Charset.defaultCharset());
         PrintWriter pw = new PrintWriter(osw);
-        NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(args[0]));
+        POIFSFileSystem fs = new POIFSFileSystem(new File(args[0]));
         HSSFWorkbook wb = new HSSFWorkbook(fs);
         try {
             pw.println( "Drawing group:" );

Modified: poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java Fri Aug 31 00:25:50 2018
@@ -46,10 +46,9 @@ import org.apache.poi.poifs.filesystem.D
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.poifs.filesystem.FileMagic;
-import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.NotOLE2FileException;
 import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.sl.extractor.SlideShowExtractor;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.NotImplemented;
@@ -132,9 +131,9 @@ public final class ExtractorFactory {
 
     @SuppressWarnings("unchecked")
     public static <T extends POITextExtractor> T createExtractor(File f) throws IOException, OpenXML4JException, XmlException {
-        NPOIFSFileSystem fs = null;
+        POIFSFileSystem fs = null;
         try {
-            fs = new NPOIFSFileSystem(f);
+            fs = new POIFSFileSystem(f);
             if (fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
                 return (T)createEncryptedOOXMLExtractor(fs);
             }
@@ -166,7 +165,7 @@ public final class ExtractorFactory {
         
         switch (fm) {
         case OLE2:
-            NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
+            POIFSFileSystem fs = new POIFSFileSystem(is);
             boolean isEncrypted = fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY); 
             return isEncrypted ? createEncryptedOOXMLExtractor(fs) : createExtractor(fs);
         case OOXML:
@@ -262,9 +261,6 @@ public final class ExtractorFactory {
     public static <T extends POITextExtractor> T createExtractor(POIFSFileSystem fs) throws IOException, OpenXML4JException, XmlException {
         return createExtractor(fs.getRoot());
     }
-    public static <T extends POITextExtractor> T createExtractor(NPOIFSFileSystem fs) throws IOException, OpenXML4JException, XmlException {
-        return createExtractor(fs.getRoot());
-    }
 
     @SuppressWarnings("unchecked")
     public static <T extends POITextExtractor> T createExtractor(DirectoryNode poifsDir) throws IOException, OpenXML4JException, XmlException
@@ -408,7 +404,7 @@ public final class ExtractorFactory {
         throw new IllegalStateException("Not yet supported");
     }
     
-    private static POITextExtractor createEncryptedOOXMLExtractor(NPOIFSFileSystem fs)
+    private static POITextExtractor createEncryptedOOXMLExtractor(POIFSFileSystem fs)
     throws IOException {
         String pass = Biff8EncryptionKey.getCurrentUserPassword();
         if (pass == null) {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=1839709&r1=1839708&r2=1839709&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java Fri Aug 31 00:25:50 2018
@@ -1002,7 +1002,7 @@ public class XSSFCellStyle implements Ce
     @Override
     public void setFont(Font font) {
         if(font != null){
-            long index = font.getIndex();
+            long index = font.getIndexAsInt();
             this._cellXf.setFontId(index);
             this._cellXf.setApplyFont(true);
         } else {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org