Search in sources :

Example 1 with EmptyFileException

use of org.apache.poi.EmptyFileException in project poi by apache.

the class TestWorkbookFactory method testEmptyInputStream.

/**
     * Check that a helpful exception is given on an empty input stream
     */
@Test
public void testEmptyInputStream() throws Exception {
    InputStream emptyStream = new ByteArrayInputStream(new byte[0]);
    try {
        WorkbookFactory.create(emptyStream);
        fail("Shouldn't be able to create for an empty stream");
    } catch (final EmptyFileException expected) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmptyFileException(org.apache.poi.EmptyFileException) Test(org.junit.Test)

Example 2 with EmptyFileException

use of org.apache.poi.EmptyFileException in project poi by apache.

the class TestWorkbookFactory method testEmptyFile.

/**
     * Check that a helpful exception is given on an empty file
     */
@Test
public void testEmptyFile() throws Exception {
    File emptyFile = TempFile.createTempFile("empty", ".poi");
    try {
        WorkbookFactory.create(emptyFile);
        fail("Shouldn't be able to create for an empty file");
    } catch (final EmptyFileException expected) {
    }
    emptyFile.delete();
}
Also used : EmptyFileException(org.apache.poi.EmptyFileException) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 3 with EmptyFileException

use of org.apache.poi.EmptyFileException in project poi by apache.

the class IOUtils method peekFirstNBytes.

/**
     * 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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) EmptyFileException(org.apache.poi.EmptyFileException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

EmptyFileException (org.apache.poi.EmptyFileException)3 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 TempFile (org.apache.poi.util.TempFile)1