org.apache.poi.poifs.filesystem.POIFSFileSystem Maven / Gradle / Ivy
/* ====================================================================
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.poifs.filesystem;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.poifs.dev.POIFSViewable;
import org.apache.poi.util.CloseIgnoringInputStream;
/**
* Transition class for the move from {@link POIFSFileSystem} to
* {@link OPOIFSFileSystem}, and from {@link NPOIFSFileSystem} to
* {@link POIFSFileSystem}. Currently, this is OPOIFS-powered
*/
public class POIFSFileSystem
extends NPOIFSFileSystem // TODO Temporary workaround during #56791
implements POIFSViewable
{
/**
* Convenience method for clients that want to avoid the auto-close behaviour of the constructor.
*/
public static InputStream createNonClosingInputStream(InputStream is) {
return new CloseIgnoringInputStream(is);
}
/**
* Constructor, intended for writing
*/
public POIFSFileSystem()
{
super();
}
/**
* Create a POIFSFileSystem from an InputStream. Normally the stream is read until
* EOF. The stream is always closed.
*
* Some streams are usable after reaching EOF (typically those that return true
* for markSupported()). In the unlikely case that the caller has such a stream
* and needs to use it after this constructor completes, a work around is to wrap the
* stream in order to trap the close() call. A convenience method (
* createNonClosingInputStream()) has been provided for this purpose:
*
* InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is);
* HSSFWorkbook wb = new HSSFWorkbook(wrappedStream);
* is.reset();
* doSomethingElse(is);
*
* Note also the special case of ByteArrayInputStream for which the close()
* method does nothing.
*
* ByteArrayInputStream bais = ...
* HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() !
* bais.reset(); // no problem
* doSomethingElse(bais);
*
*
* @param stream the InputStream from which to read the data
*
* @exception IOException on errors reading, or on invalid data
*/
public POIFSFileSystem(InputStream stream)
throws IOException
{
super(stream);
}
/**
* Creates a POIFSFileSystem from a File. This uses less memory than
* creating from an InputStream. The File will be opened read-only
*
* Note that with this constructor, you will need to call {@link #close()}
* when you're done to have the underlying file closed, as the file is
* kept open during normal operation to read the data out.
*
* @param file the File from which to read the data
*
* @exception IOException on errors reading, or on invalid data
*/
public POIFSFileSystem(File file) throws IOException {
super(file);
}
/**
* Checks that the supplied InputStream (which MUST
* support mark and reset, or be a PushbackInputStream)
* has a POIFS (OLE2) header at the start of it.
* If your InputStream does not support mark / reset,
* then wrap it in a PushBackInputStream, then be
* sure to always use that, and not the original!
* @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
*/
public static boolean hasPOIFSHeader(InputStream inp) throws IOException {
return NPOIFSFileSystem.hasPOIFSHeader(inp);
}
/**
* Checks if the supplied first 8 bytes of a stream / file
* has a POIFS (OLE2) header.
*/
public static boolean hasPOIFSHeader(byte[] header8Bytes) {
return NPOIFSFileSystem.hasPOIFSHeader(header8Bytes);
}
/**
* read in a file and write it back out again
*
* @param args names of the files; arg[ 0 ] is the input file,
* arg[ 1 ] is the output file
*
* @exception IOException
*/
public static void main(String args[])
throws IOException
{
OPOIFSFileSystem.main(args);
}
}