Example usage for org.apache.poi.util IOUtils peekFirst8Bytes

List of usage examples for org.apache.poi.util IOUtils peekFirst8Bytes

Introduction

In this page you can find the example usage for org.apache.poi.util IOUtils peekFirst8Bytes.

Prototype

public static byte[] peekFirst8Bytes(InputStream stream) throws IOException, EmptyFileException 

Source Link

Document

Peeks at the first 8 bytes of the stream.

Usage

From source file:com.kplot.web.data.WorkbookFactory.java

License:Apache License

/**
 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 *  the given InputStream, which may be password protected.
 * <p>Your input stream MUST either support mark/reset, or
 *  be wrapped as a {@link PushbackInputStream}! Note that
 *  using an {@link InputStream} has a higher memory footprint
 *  than using a {@link File}.</p>
 *
 * <p>Note that in order to properly release resources the
 *  Workbook should be closed after use. Note also that loading
 *  from an InputStream requires more memory than loading
 *  from a File, so prefer {@link #create(File)} where possible.</p>
 *
 *  @param inp The {@link InputStream} to read data from.
 *  @param password The password that should be used or null if no password is necessary.
 *
 *  @return The created Workbook/*from w  w  w  .j  a v  a2 s  .c o m*/
 *
 *  @throws IOException if an error occurs while reading the data
 *  @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook}
 *  @throws EncryptedDocumentException If the wrong password is given for a protected file
 *  @throws EmptyFileException If an empty stream is given
 */
public static Workbook create(InputStream inp, String password)
        throws IOException, InvalidFormatException, EncryptedDocumentException {
    // If clearly doesn't do mark/reset, wrap up
    if (!inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }

    // Ensure that there is at least some data there
    byte[] header8 = IOUtils.peekFirst8Bytes(inp);

    // Try to create
    if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
        NPOIFSFileSystem fs = new NPOIFSFileSystem(inp);
        return create(fs, password);
    }
    if (POIXMLDocument.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

License:Open Source License

/**
 * Detect the excel format with only peeking at the first 8 bytes of the input stream (leaving the stream untouched).
 *
 * @param inputStream the xls input stream.
 * @return true if the given input stream is a xls new format.
 * @throws IOException if an error occurs.
 *///  w w w  . j  a v  a  2s .c o  m
public static boolean isNewExcelFormat(InputStream inputStream) throws IOException {

    boolean newFormat = false;

    // Ensure that there is at least some data there
    byte[] headers = IOUtils.peekFirst8Bytes(inputStream);

    if (NPOIFSFileSystem.hasPOIFSHeader(headers)) {
        newFormat = false;
    }
    if (POIXMLDocument.hasOOXMLHeader(new ByteArrayInputStream(headers))) {
        newFormat = true;
    }

    return newFormat;

}