spring-projects / spring-batch-extensions

Spring Batch Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make it possible to read FileSystemResource in PoiItemReader

haansuk opened this issue · comments

Thank you for making a good library.
But it has one drawback, which it cannot read excel file resources using FileSystemResource.

In current version, If you put FileSystemResource in openExcelFile(Final Resources resources) method’s parameter and execute it, it throws Exception. Because FileInputStream isn't mark supported and also not wrapped as PushBackInputStream.

@Override
protected void openExcelFile(final Resource resource) throws Exception {
    workbookStream = resource.getInputStream();

    if (!workbookStream.markSupported() && !(workbookStream instanceof PushbackInputStream)) {
        throw new IllegalStateException("InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream");
    }

    this.workbook = WorkbookFactory.create(workbookStream);
    this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
}

But it’s an unnecessary check because WorkbookFactory.create(workbookStream) method can wrap InputStream as PushBackInputStream when it isn’t mark supported.

public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
    if(!((InputStream)inp).markSupported()) {
        inp = new PushbackInputStream((InputStream)inp, 8);
    }

    if(POIFSFileSystem.hasPOIFSHeader((InputStream)inp)) {
        return new HSSFWorkbook((InputStream)inp);
    } else if(POIXMLDocument.hasOOXMLHeader((InputStream)inp)) {
        return new XSSFWorkbook(OPCPackage.open((InputStream)inp));
    } else {
        throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
    }
}

So I think it's more useful to remove unnecessary validation check in openExcelFile(Final Resources resources) method to read excel file as FileSystemResource which is used frequenly in batch environment

Fixed in #65.