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
This commit is contained in:
Nick Burch 2010-12-19 08:05:44 +00:00
parent b79ad301cf
commit ced5abd457
5 changed files with 65 additions and 21 deletions

View File

@ -48,6 +48,7 @@ import org.apache.poi.poifs.storage.HeaderBlockWriter;
import org.apache.poi.poifs.storage.RawDataBlockList; import org.apache.poi.poifs.storage.RawDataBlockList;
import org.apache.poi.poifs.storage.SmallBlockTableReader; import org.apache.poi.poifs.storage.SmallBlockTableReader;
import org.apache.poi.poifs.storage.SmallBlockTableWriter; import org.apache.poi.poifs.storage.SmallBlockTableWriter;
import org.apache.poi.util.CloseIgnoringInputStream;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LongField; import org.apache.poi.util.LongField;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
@ -66,23 +67,6 @@ public class POIFSFileSystem
private static final POILogger _logger = private static final POILogger _logger =
POILogFactory.getLogger(POIFSFileSystem.class); 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. * Convenience method for clients that want to avoid the auto-close behaviour of the constructor.
*/ */

View File

@ -26,9 +26,12 @@ public class ByteArrayBackedDataSource extends DataSource {
private byte[] buffer; private byte[] buffer;
private long size; private long size;
public ByteArrayBackedDataSource(byte[] data) { public ByteArrayBackedDataSource(byte[] data, int size) {
this.buffer = data; 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 void read(ByteBuffer dst, long position) {

View File

@ -112,7 +112,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
} }
public HeaderBlock(ByteBuffer buffer) throws IOException { public HeaderBlock(ByteBuffer buffer) throws IOException {
this(buffer.array()); this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE));
} }
private HeaderBlock(byte[] data) throws IOException { private HeaderBlock(byte[] data) throws IOException {

View File

@ -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;
}
}

View File

@ -47,6 +47,22 @@ public final class IOUtils {
return baos.toByteArray(); 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> * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
*/ */