您可以使用以下版本来 修饰 传递给 POIFSFileSystem的 InputStream
,该版本在调用close()时会使用reset()进行响应:
class ResetonCloseInputStream extends InputStream { private final InputStream decorated; public ResetonCloseInputStream(InputStream anInputStream) { if (!anInputStream.markSupported()) { throw new IllegalArgumentException("marking not supported"); } anInputStream.mark( 1 << 24); // magic constant: BEWARE decorated = anInputStream; } @Override public void close() throws IOException { decorated.reset(); } @Override public int read() throws IOException { return decorated.read(); }}测试用例
static void closeAfterInputStreamIsConsumed(InputStream is) throws IOException { int r; while ((r = is.read()) != -1) { System.out.println(r); } is.close(); System.out.println("=========");}public static void main(String[] args) throws IOException { InputStream is = new ByteArrayInputStream("sample".getBytes()); ResetonCloseInputStream decoratedIs = new ResetonCloseInputStream(is); closeAfterInputStreamIsConsumed(decoratedIs); closeAfterInputStreamIsConsumed(decoratedIs); closeAfterInputStreamIsConsumed(is);}编辑2
您可以读取整个文件并以byte [](读取模式)将其传递给ByteArrayInputStream



