https://bz.apache.org/bugzilla/show_bug.cgi?id=58420

            Bug ID: 58420
           Summary: Document with many sheets may exceed FileHandle limit
           Product: POI
           Version: 3.12-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: SXSSF
          Assignee: dev@poi.apache.org
          Reporter: newssc...@gmx.de

SXSSFWorkbook creates a temporary file for each sheet. The FileHandle to this
temporary file is closed during the call to workbook.write. This is no problem
as long as the number of sheets is lower than the process' openfilehandle
limit. Here's some code to reproduce:

    public static void main(String[] args) throws IOException {
        Workbook wb = new SXSSFWorkbook();
        for (int i = 0; i < 10000; i++) {
            Sheet sheet = wb.createSheet("sheet_" + i);

            //manipulate current sheet
        }

        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }

Depending on the current openfilehandel limit, this code will fail.

After manipulateion of the current sheet, it should not be neccessary to keep
the filehandle open. I tried to close the handle manually by adding

            ((SXSSFSheet) sheet).getWorksheetXMLInputStream().close();

just before the end of the "sheet loop". Note that an side effect of
getWorksheetXMLInputStream is to close the writer FileHandle.
Unfortunately the final call to 

        wb.write(fileOut);

tries to call flush again:

Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:126)
    at java.io.BufferedWriter.flush(BufferedWriter.java:253)
    at
org.apache.poi.xssf.streaming.SheetDataWriter.close(SheetDataWriter.java:97)
    at
org.apache.poi.xssf.streaming.SXSSFSheet.getWorksheetXMLInputStream(SXSSFSheet.java:86)
    at
org.apache.poi.xssf.streaming.SXSSFWorkbook.injectData(SXSSFWorkbook.java:353)
    at
org.apache.poi.xssf.streaming.SXSSFWorkbook.write(SXSSFWorkbook.java:891)
    at
com.gfk.ait.convertserver.xml2excel.xlsx.Xml2XlsxConverter.main(Xml2XlsxConverter.java:56)

If i debug-eleminate the final calls to SheetDataWriter.close (as they have
been closed before), the code works and the resulting xlsx document is valid.

A call getWorksheetXMLInputStream should try to close the _writer stream only
once.
The interface should provide a clean method to release the filehandle.

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to