org.apache.poi.EmptyFileException Java Examples

The following examples show how to use org.apache.poi.EmptyFileException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IOUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Peeks at the first N bytes of the stream. Returns those bytes, but
 *  with the stream unaffected. Requires a stream that supports mark/reset,
 *  or a PushbackInputStream. If the stream has >0 but <N bytes, 
 *  remaining bytes will be zero.
 * @throws EmptyFileException if the stream is empty
 */
public static byte[] peekFirstNBytes(InputStream stream, int limit) throws IOException, EmptyFileException {
    stream.mark(limit);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(limit);
    copy(new BoundedInputStream(stream, limit), bos);

    int readBytes = bos.size();
    if (readBytes == 0) {
        throw new EmptyFileException();
    }
    
    if (readBytes < limit) {
        bos.write(new byte[limit-readBytes]);
    }
    byte peekedBytes[] = bos.toByteArray();
    if(stream instanceof PushbackInputStream) {
        PushbackInputStream pin = (PushbackInputStream)stream;
        pin.unread(peekedBytes, 0, readBytes);
    } else {
        stream.reset();
    }

    return peekedBytes;
}
 
Example #2
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a list of POJOs from the given excel file.
 *
 * @param path Excel file to read from
 * @param sheetName The sheet to extract from in the workbook
 * @param reader The reader class to use to load the file from the sheet
 */
public static void process(String path, String sheetName, ZeroCellReader reader) {
    if (path == null || path.trim().isEmpty()) {
        throw new IllegalArgumentException("'path' must be given");
    }

    File file = new File(path);
    if (file.exists() && file.isDirectory()) {
        throw new IllegalArgumentException("path must not be a directory");
    }
    try (OPCPackage opcPackage = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ)) {
        process(opcPackage, sheetName, reader);
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch (IOException ioe) {
        throw new ZeroCellException("Failed to process file", ioe);
    }
}
 
Example #3
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a list of POJOs from the given excel file.
 *
 * @param file Excel file to read from
 * @param sheetName The sheet to extract from in the workbook
 * @param reader The reader class to use to load the file from the sheet
 */
public static void process(File file, String sheetName, ZeroCellReader reader) {
    try (OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ)) {
        process(opcPackage, sheetName, reader);
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch (IOException ioe) {
        throw new ZeroCellException("Failed to process file", ioe);
    }
}
 
Example #4
Source File: RunIteratorTests.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
@Test(expected = EmptyFileException.class)
public void testEmptyDoc() throws InvalidFormatException, IOException {
    final File file = new File("resources/document/empty/empty-template.docx");
    assertTrue(file.exists());
    try (FileInputStream is = new FileInputStream(file);
            OPCPackage oPackage = OPCPackage.open(is);
            XWPFDocument document = new XWPFDocument(oPackage);) {
        TokenIterator iterator = new TokenIterator(document);

        assertTrue(!iterator.hasNext());
    }
}
 
Example #5
Source File: ReaderUtil.java    From zerocell with Apache License 2.0 4 votes vote down vote up
/**
 * Processes data from an Excel file contained in the OPCPackage using the
 * reader implementation
 * <p>
 * Please note that the process will read data from the first sheet in the
 * File when if sheet name is not specified
 * (i.e. the sheet name defaults to the {@link EntityHandler.DEFAULT_SHEET})
 * </p>
 * @param opcPackage the OpenXML OPC Package
 * @param sheetName The sheet name
 * @param reader the reader implementation that handles the entity mapping
 */
private static void process(OPCPackage opcPackage, String sheetName, ZeroCellReader reader) {
    try {
        DataFormatter dataFormatter = new DataFormatter();
        ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opcPackage);
        XSSFReader xssfReader = new XSSFReader(opcPackage);
        StylesTable stylesTable = xssfReader.getStylesTable();
        InputStream sheetInputStream = null;
        XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
        while (sheets.hasNext()) {
            sheetInputStream = sheets.next();

            if (EntityHandler.DEFAULT_SHEET.equalsIgnoreCase(sheetName)) {
                break;
            }
            if (sheets.getSheetName().equalsIgnoreCase(sheetName)) {
                break;
            } else {
                sheetInputStream = null;
            }
        }

        if (Objects.isNull(sheetInputStream)) {
            throw new SheetNotFoundException(sheetName);
        }

        XMLReader xmlReader = SAXHelper.newXMLReader();
        xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, strings, reader, dataFormatter, false));
        xmlReader.parse(new InputSource(sheetInputStream));
        sheetInputStream.close();
        xmlReader = null;
        sheetInputStream = null;
        stylesTable = null;
        strings = null;
        xssfReader = null;
    } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) {
        throw new ZeroCellException(ERROR_NOT_OPENXML);
    } catch(SheetNotFoundException ex) {
        throw new ZeroCellException(ex.getMessage());
    } catch (ZeroCellException ze) {
        throw ze; // Rethrow the Exception
    } catch (Exception e) {
        throw new ZeroCellException("Failed to process file", e);
    }
}
 
Example #6
Source File: IOUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Peeks at the first 8 bytes of the stream. Returns those bytes, but
 *  with the stream unaffected. Requires a stream that supports mark/reset,
 *  or a PushbackInputStream. If the stream has &gt;0 but &lt;8 bytes, 
 *  remaining bytes will be zero.
 * @throws EmptyFileException if the stream is empty
 */
public static byte[] peekFirst8Bytes(InputStream stream) throws IOException, EmptyFileException {
    return peekFirstNBytes(stream, 8);
}