Browse Source

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
tags/REL_3_8_BETA1
Nick Burch 13 years ago
parent
commit
ced5abd457

+ 1
- 17
src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java 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.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.
*/

+ 5
- 2
src/java/org/apache/poi/poifs/nio/ByteArrayBackedDataSource.java View File

@@ -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) {

+ 1
- 1
src/java/org/apache/poi/poifs/storage/HeaderBlock.java View File

@@ -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 {

+ 41
- 0
src/java/org/apache/poi/util/CloseIgnoringInputStream.java 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;
}
}

+ 17
- 1
src/java/org/apache/poi/util/IOUtils.java View File

@@ -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.

Loading…
Cancel
Save