From ced5abd45761ea3659bd0d6d60920e22f74ac06b Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 19 Dec 2010 08:05:44 +0000 Subject: [PATCH] Move CloseIgnoringInputStream out to its own class, and add more helper methods git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1050772 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/POIFSFileSystem.java | 18 +------- .../poifs/nio/ByteArrayBackedDataSource.java | 7 +++- .../apache/poi/poifs/storage/HeaderBlock.java | 2 +- .../poi/util/CloseIgnoringInputStream.java | 41 +++++++++++++++++++ src/java/org/apache/poi/util/IOUtils.java | 18 +++++++- 5 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 src/java/org/apache/poi/util/CloseIgnoringInputStream.java diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index bbc22515dd..aa8e2966db 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -48,6 +48,7 @@ import org.apache.poi.poifs.storage.HeaderBlockWriter; 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. */ diff --git a/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java index 4e368994bb..823b84e939 100644 --- a/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java +++ b/src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java @@ -26,9 +26,12 @@ public class ByteArrayBackedDataSource extends DataSource { 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) { diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index b3bf9a5a16..d9098749ee 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -112,7 +112,7 @@ public final class HeaderBlock implements HeaderBlockConstants { } public HeaderBlock(ByteBuffer buffer) throws IOException { - this(buffer.array()); + this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE)); } private HeaderBlock(byte[] data) throws IOException { diff --git a/src/java/org/apache/poi/util/CloseIgnoringInputStream.java b/src/java/org/apache/poi/util/CloseIgnoringInputStream.java new file mode 100644 index 0000000000..f4896a8312 --- /dev/null +++ b/src/java/org/apache/poi/util/CloseIgnoringInputStream.java @@ -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; + } +} diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 4428c9c544..719330f122 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -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 readFully(in, b, 0, b.length) */ @@ -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. -- 2.39.5