poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1050772 - in /poi/trunk/src/java/org/apache/poi: poifs/filesystem/POIFSFileSystem.java poifs/nio/ByteArrayBackedDataSource.java poifs/storage/HeaderBlock.java util/CloseIgnoringInputStream.java util/IOUtils.java
Date Sun, 19 Dec 2010 08:05:44 GMT
Author: nick
Date: Sun Dec 19 08:05:44 2010
New Revision: 1050772

URL: http://svn.apache.org/viewvc?rev=1050772&view=rev
Log:
Move CloseIgnoringInputStream out to its own class, and add more helper methods

Added:
    poi/trunk/src/java/org/apache/poi/util/CloseIgnoringInputStream.java
Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java
    poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
    poi/trunk/src/java/org/apache/poi/util/IOUtils.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java?rev=1050772&r1=1050771&r2=1050772&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java Sun Dec 19 08:05:44
2010
@@ -48,6 +48,7 @@ import org.apache.poi.poifs.storage.Head
 import org.apache.poi.poifs.storage.RawDataBlockList;
 import org.apache.poi.poifs.storage.SmallBlockTableReader;
 import org.apache.poi.poifs.storage.SmallBlockTableWriter;
+import org.apache.poi.util.CloseIgnoringInputStream;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LongField;
 import org.apache.poi.util.POILogFactory;
@@ -66,23 +67,6 @@ public class POIFSFileSystem
 	private static final POILogger _logger =
 		POILogFactory.getLogger(POIFSFileSystem.class);
 
-    private static final class CloseIgnoringInputStream extends InputStream {
-
-        private final InputStream _is;
-        public CloseIgnoringInputStream(InputStream is) {
-            _is = is;
-        }
-        public int read() throws IOException {
-            return _is.read();
-        }
-        public int read(byte[] b, int off, int len) throws IOException {
-            return _is.read(b, off, len);
-        }
-        public void close() {
-            // do nothing
-        }
-    }
-
     /**
      * Convenience method for clients that want to avoid the auto-close behaviour of the
constructor.
      */

Modified: poi/trunk/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java?rev=1050772&r1=1050771&r2=1050772&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java Sun Dec 19
08:05:44 2010
@@ -26,9 +26,12 @@ public class ByteArrayBackedDataSource e
    private byte[] buffer;
    private long size;
    
-   public ByteArrayBackedDataSource(byte[] data) {
+   public ByteArrayBackedDataSource(byte[] data, int size) {
       this.buffer = data;
-      this.size = data.length;
+      this.size = size;
+   }
+   public ByteArrayBackedDataSource(byte[] data) {
+      this(data, data.length);
    }
                 
    public void read(ByteBuffer dst, long position) {

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=1050772&r1=1050771&r2=1050772&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 Sun Dec 19 08:05:44 2010
@@ -112,7 +112,7 @@ public final class HeaderBlock implement
 	}
 	
 	public HeaderBlock(ByteBuffer buffer) throws IOException {
-	   this(buffer.array());
+	   this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE));
 	}
 	
 	private HeaderBlock(byte[] data) throws IOException {

Added: poi/trunk/src/java/org/apache/poi/util/CloseIgnoringInputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/CloseIgnoringInputStream.java?rev=1050772&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/CloseIgnoringInputStream.java (added)
+++ poi/trunk/src/java/org/apache/poi/util/CloseIgnoringInputStream.java Sun Dec 19 08:05:44
2010
@@ -0,0 +1,41 @@
+/* ====================================================================
+   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.util;
+
+import java.io.FilterInputStream;
+import java.io.InputStream;
+
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
+/**
+ * A wrapper around an {@link InputStream}, which 
+ *  ignores close requests made to it.
+ *
+ * Useful with {@link POIFSFileSystem}, where you want
+ *  to control the close yourself.
+ */
+public class CloseIgnoringInputStream extends FilterInputStream {
+   public CloseIgnoringInputStream(InputStream in) {
+      super(in);
+   }
+
+   public void close() {
+      // Does nothing and ignores you
+      return;
+   }
+}

Modified: poi/trunk/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/IOUtils.java?rev=1050772&r1=1050771&r2=1050772&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/IOUtils.java Sun Dec 19 08:05:44 2010
@@ -47,6 +47,22 @@ public final class IOUtils {
 		return baos.toByteArray();
 	}
 
+   /**
+    * Returns an array (that shouldn't be written to!) of the
+    *  ByteBuffer. Will be of the requested length, or possibly
+    *  longer if that's easier.
+    */
+   public static byte[] toByteArray(ByteBuffer buffer, int length) {
+      if(buffer.hasArray() && buffer.arrayOffset() == 0) {
+         // The backing array should work out fine for us
+         return buffer.array();
+      }
+      
+      byte[] data = new byte[length];
+      buffer.get(data);
+      return data;
+   }
+
 	/**
 	 * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
 	 */
@@ -99,7 +115,7 @@ public final class IOUtils {
          }
       }
 	}
-
+	
 	/**
 	 * Copies all the data from the given InputStream to the OutputStream. It
 	 * leaves both streams open, so you will still need to close them once done.



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


Mime
View raw message