Example usage for org.apache.poi.poifs.filesystem FileMagic valueOf

List of usage examples for org.apache.poi.poifs.filesystem FileMagic valueOf

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem FileMagic valueOf.

Prototype

public static FileMagic valueOf(InputStream inp) throws IOException 

Source Link

Document

Get the file magic of the supplied InputStream (which MUST support mark and reset).

If unsure if your InputStream does support mark / reset, use #prepareToCheckMagic(InputStream) to wrap it and make sure to always use that, and not the original!

Even if this method returns FileMagic#UNKNOWN it could potentially mean, that the ZIP stream has leading junk bytes

Usage

From source file:com.jkoolcloud.tnt4j.streams.inputs.ExcelSXSSFRowStream.java

License:Apache License

@Override
protected void initialize() throws Exception {
    super.initialize();

    Thread excelFileReader = new Thread(new Runnable() {
        @Override//from   www.j a  v a2  s  .  c  om
        public void run() {
            try {
                File inputFile = new File(fileName);

                InputStream is = new FileInputStream(inputFile);
                is = FileMagic.prepareToCheckMagic(is);
                FileMagic fm = FileMagic.valueOf(is);
                Utils.close(is);

                if (fm == FileMagic.OOXML) {
                    readXLXS(inputFile);
                } else if (fm == FileMagic.OLE2) {
                    readXLS(inputFile);
                } else {
                    throw new IOException(
                            StreamsResources.getStringFormatted(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME,
                                    "ExcelSXSSFRowStream.unsupported.format", fileName));
                }
            } catch (Exception e) {
                Utils.logThrowable(LOGGER, OpLevel.ERROR,
                        StreamsResources.getBundle(MsOfficeStreamConstants.RESOURCE_BUNDLE_NAME),
                        "ExcelSXSSFRowStream.file.read.failed", fileName, e);
            }
            ended = true;
        }
    });
    excelFileReader.start();
}