avoid unnecessary string concats in SXSSF SheetDataWriter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1801806 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-07-13 07:14:01 +00:00
parent f1cf95ecb5
commit d1a51f76f0
3 changed files with 48 additions and 29 deletions

View File

@ -406,7 +406,7 @@ public class SXSSFWorkbook implements Workbook {
} }
private static void copyStreamAndInjectWorksheet(InputStream in, OutputStream out, InputStream worksheetData) throws IOException { private static void copyStreamAndInjectWorksheet(InputStream in, OutputStream out, InputStream worksheetData) throws IOException {
InputStreamReader inReader=new InputStreamReader(in,"UTF-8"); //TODO: Is it always UTF-8 or do we need to read the xml encoding declaration in the file? If not, we should perhaps use a SAX reader instead. InputStreamReader inReader=new InputStreamReader(in,"UTF-8");
OutputStreamWriter outWriter=new OutputStreamWriter(out,"UTF-8"); OutputStreamWriter outWriter=new OutputStreamWriter(out,"UTF-8");
boolean needsStartTag = true; boolean needsStartTag = true;
int c; int c;

View File

@ -207,23 +207,27 @@ public class SheetDataWriter implements Closeable {
} }
void beginRow(int rownum, SXSSFRow row) throws IOException { void beginRow(int rownum, SXSSFRow row) throws IOException {
_out.write("<row r=\"" + (rownum + 1) + "\""); _out.write("<row");
if (row.hasCustomHeight()) writeAttribute("r", Integer.toString(rownum + 1));
_out.write(" customHeight=\"true\" ht=\"" + row.getHeightInPoints() + "\""); if (row.hasCustomHeight()) {
if (row.getZeroHeight()) writeAttribute("customHeight", "true");
_out.write(" hidden=\"true\""); writeAttribute("ht", Float.toString(row.getHeightInPoints()));
}
if (row.getZeroHeight()) {
writeAttribute("hidden", "true");
}
if (row.isFormatted()) { if (row.isFormatted()) {
_out.write(" s=\"" + row.getRowStyleIndex() + "\""); writeAttribute("s", Integer.toString(row.getRowStyleIndex()));
_out.write(" customFormat=\"1\""); writeAttribute("customFormat", "1");
} }
if (row.getOutlineLevel() != 0) { if (row.getOutlineLevel() != 0) {
_out.write(" outlineLevel=\"" + row.getOutlineLevel() + "\""); writeAttribute("outlineLevel", Integer.toString(row.getOutlineLevel()));
} }
if(row.getHidden() != null) { if(row.getHidden() != null) {
_out.write(" hidden=\"" + (row.getHidden() ? "1" : "0") + "\""); writeAttribute("hidden", row.getHidden() ? "1" : "0");
} }
if(row.getCollapsed() != null) { if(row.getCollapsed() != null) {
_out.write(" collapsed=\"" + (row.getCollapsed() ? "1" : "0") + "\""); writeAttribute("collapsed", row.getCollapsed() ? "1" : "0");
} }
_out.write(">\n"); _out.write(">\n");
@ -239,30 +243,32 @@ public class SheetDataWriter implements Closeable {
return; return;
} }
String ref = new CellReference(_rownum, columnIndex).formatAsString(); String ref = new CellReference(_rownum, columnIndex).formatAsString();
_out.write("<c r=\"" + ref + "\""); _out.write("<c");
writeAttribute("r", ref);
CellStyle cellStyle = cell.getCellStyle(); CellStyle cellStyle = cell.getCellStyle();
if (cellStyle.getIndex() != 0) { if (cellStyle.getIndex() != 0) {
// need to convert the short to unsigned short as the indexes can be up to 64k // need to convert the short to unsigned short as the indexes can be up to 64k
// ideally we would use int for this index, but that would need changes to some more // ideally we would use int for this index, but that would need changes to some more
// APIs // APIs
_out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\""); writeAttribute("s", Integer.toString(cellStyle.getIndex() & 0xffff));
} }
CellType cellType = cell.getCellTypeEnum(); CellType cellType = cell.getCellTypeEnum();
switch (cellType) { switch (cellType) {
case BLANK: { case BLANK: {
_out.write(">"); _out.write('>');
break; break;
} }
case FORMULA: { case FORMULA: {
_out.write(">"); _out.write("><f>");
_out.write("<f>");
outputQuotedString(cell.getCellFormula()); outputQuotedString(cell.getCellFormula());
_out.write("</f>"); _out.write("</f>");
switch (cell.getCachedFormulaResultTypeEnum()) { switch (cell.getCachedFormulaResultTypeEnum()) {
case NUMERIC: case NUMERIC:
double nval = cell.getNumericCellValue(); double nval = cell.getNumericCellValue();
if (!Double.isNaN(nval)) { if (!Double.isNaN(nval)) {
_out.write("<v>" + nval + "</v>"); _out.write("<v>");
_out.write(Double.toString(nval));
_out.write("</v>");
} }
break; break;
default: default:
@ -275,15 +281,15 @@ public class SheetDataWriter implements Closeable {
XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue()); XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
int sRef = _sharedStringSource.addEntry(rt.getCTRst()); int sRef = _sharedStringSource.addEntry(rt.getCTRst());
_out.write(" t=\"" + STCellType.S + "\">"); writeAttribute("t", STCellType.S.toString());
_out.write("<v>"); _out.write("><v>");
_out.write(String.valueOf(sRef)); _out.write(String.valueOf(sRef));
_out.write("</v>"); _out.write("</v>");
} else { } else {
_out.write(" t=\"inlineStr\">"); writeAttribute("t", "inlineStr");
_out.write("<is><t"); _out.write("><is><t");
if (hasLeadingTrailingSpaces(cell.getStringCellValue())) { if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
_out.write(" xml:space=\"preserve\""); writeAttribute("xml:space", "preserve");
} }
_out.write(">"); _out.write(">");
outputQuotedString(cell.getStringCellValue()); outputQuotedString(cell.getStringCellValue());
@ -292,20 +298,26 @@ public class SheetDataWriter implements Closeable {
break; break;
} }
case NUMERIC: { case NUMERIC: {
_out.write(" t=\"n\">"); writeAttribute("t", "n");
_out.write("<v>" + cell.getNumericCellValue() + "</v>"); _out.write("><v>");
_out.write(Double.toString(cell.getNumericCellValue()));
_out.write("</v>");
break; break;
} }
case BOOLEAN: { case BOOLEAN: {
_out.write(" t=\"b\">"); writeAttribute("t", "b");
_out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>"); _out.write("><v>");
_out.write(cell.getBooleanCellValue() ? "1" : "0");
_out.write("</v>");
break; break;
} }
case ERROR: { case ERROR: {
FormulaError error = FormulaError.forInt(cell.getErrorCellValue()); FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
_out.write(" t=\"e\">"); writeAttribute("t", "e");
_out.write("<v>" + error.getString() + "</v>"); _out.write("><v>");
_out.write(error.getString());
_out.write("</v>");
break; break;
} }
default: { default: {
@ -315,6 +327,13 @@ public class SheetDataWriter implements Closeable {
_out.write("</c>"); _out.write("</c>");
} }
private void writeAttribute(String name, String value) throws IOException {
_out.write(' ');
_out.write(name);
_out.write("=\"");
_out.write(value);
_out.write('\"');
}
/** /**
* @return whether the string has leading / trailing spaces that * @return whether the string has leading / trailing spaces that

View File

@ -132,7 +132,7 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook {
public void useSharedStringsTable() throws Exception { public void useSharedStringsTable() throws Exception {
SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true); SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);
SharedStringsTable sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource"); SharedStringsTable sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
assertNotNull(sss); assertNotNull(sss);