Skip to content

Commit

Permalink
allow decorators to be used in SheetDataWriter and use ZipEntrySource…
Browse files Browse the repository at this point in the history
… in SXSSFWorkbook to allow custom implementations to be used instead of raw ZipFile
  • Loading branch information
PJ Fanning committed Sep 20, 2016
1 parent a4c3ce2 commit 72f0ada
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
Expand Up @@ -35,6 +35,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.zip.ZipOutputStream;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.util.ZipEntrySource;
import org.apache.poi.openxml4j.util.ZipFileZipEntrySource;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellStyle;
Expand Down Expand Up @@ -101,12 +103,12 @@ public class SXSSFWorkbook implements Workbook {
/**
* whether temp files should be compressed.
*/
private boolean _compressTmpFiles = false;
protected boolean _compressTmpFiles = false;

/**
* shared string table - a cache of strings in this workbook
*/
private final SharedStringsTable _sharedStringSource;
protected final SharedStringsTable _sharedStringSource;

/**
* Construct a new workbook with default row window size
Expand Down Expand Up @@ -353,21 +355,19 @@ private XSSFSheet getSheetFromZipEntryName(String sheetRef)
return null;
}

private void injectData(File zipfile, OutputStream out) throws IOException
private void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException
{
// don't use ZipHelper.openZipFile here - see #59743
ZipFile zip = new ZipFile(zipfile);
try
{
ZipOutputStream zos = new ZipOutputStream(out);
try
{
Enumeration<? extends ZipEntry> en = zip.entries();
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements())
{
ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zip.getInputStream(ze);
InputStream is = zipEntrySource.getInputStream(ze);
XSSFSheet xSheet=getSheetFromZipEntryName(ze.getName());
if(xSheet!=null)
{
Expand Down Expand Up @@ -396,7 +396,7 @@ private void injectData(File zipfile, OutputStream out) throws IOException
}
finally
{
zip.close();
zipEntrySource.close();
}
}
private static void copyStream(InputStream in, OutputStream out) throws IOException {
Expand Down Expand Up @@ -946,7 +946,7 @@ public void write(OutputStream stream) throws IOException
}

//Substitute the template entries with the generated sheet data files
injectData(tmplFile, stream);
injectData(new ZipFileZipEntrySource(new ZipFile(tmplFile)), stream);
}
finally
{
Expand Down
15 changes: 12 additions & 3 deletions src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java
Expand Up @@ -25,6 +25,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
Expand Down Expand Up @@ -90,8 +91,12 @@ public File createTempFile() throws IOException {
*
* @param fd the file to write to
*/
public Writer createWriter(File fd)throws IOException {
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fd), "UTF-8"));
public Writer createWriter(File fd) throws IOException {
return new BufferedWriter(new OutputStreamWriter(decorateOutputStream(new FileOutputStream(fd)), "UTF-8"));
}

protected OutputStream decorateOutputStream(FileOutputStream fos) {
return fos;
}

/**
Expand All @@ -112,7 +117,11 @@ File getTempFile(){
*/
public InputStream getWorksheetXMLInputStream() throws IOException {
File fd = getTempFile();
return new FileInputStream(fd);
return decorateInputStream(new FileInputStream(fd));
}

protected InputStream decorateInputStream(FileInputStream fis) {
return fis;
}

public int getNumberOfFlushedRows() {
Expand Down

0 comments on commit 72f0ada

Please sign in to comment.