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) {
}
}
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();
}
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;
}
Aggregations