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;
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.
*/
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) {
}
public HeaderBlock(ByteBuffer buffer) throws IOException {
- this(buffer.array());
+ this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE));
}
private HeaderBlock(byte[] data) throws IOException {
--- /dev/null
+/* ====================================================================
+ 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;
+ }
+}
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>
*/
}
}
}
-
+
/**
* 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.