poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject [Bug 58480] HSSFWorkbook.write(OutputStream) does not close the NPOIFSFileSystem in use
Date Tue, 06 Oct 2015 12:26:55 GMT
https://bz.apache.org/bugzilla/show_bug.cgi?id=58480

--- Comment #3 from Kasper Sørensen <i.am.kasper.sorensen@gmail.com> ---
That's not the problem IMO. I am closing the stream.

Please see this test case that I just built to reproduce the issue on current
trunk:

------------

package org.apache.poi.hssf.usermodel;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.Test;

public class HSSFWorkbookTest {

    @Test
    public void testWriteScenario() throws Exception {
        final File file = new File(
                "build/HSSFWorkbookTest-testWriteScenario.xls");

        // create new workbook
        {
            final Workbook workbook = new HSSFWorkbook();
            final Sheet sheet = workbook.createSheet("foo");
            final Row row = sheet.createRow(1);
            row.createCell(1).setCellValue("bar");

            writeAndCloseWorkbook(workbook, file);
        }

        // edit the workbook
        {
            final Workbook workbook = WorkbookFactory.create(file);
            final Sheet sheet = workbook.getSheet("foo");
            sheet.getRow(1).createCell(2).setCellValue("baz");

            writeAndCloseWorkbook(workbook, file);
        }
    }

    private void writeAndCloseWorkbook(Workbook workbook, File file)
            throws IOException {
        final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        workbook.write(bytesOut);
        workbook.close();

        final byte[] byteArray = bytesOut.toByteArray();
        bytesOut.close();

        final FileOutputStream fileOut = new FileOutputStream(file);
        fileOut.write(byteArray);
        fileOut.close();

    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Mime
View raw message