Skip to content

Instantly share code, notes, and snippets.

@dpsutton
Created April 14, 2023 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpsutton/453d627705638cc215234de0ef20b487 to your computer and use it in GitHub Desktop.
Save dpsutton/453d627705638cc215234de0ef20b487 to your computer and use it in GitHub Desktop.
--- sdw_500.java 2023-04-14 17:55:38
+++ sdw_523.java 2023-04-14 17:56:20
@@ -32,17 +32,18 @@
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FormulaError;
+import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.StringCodepointsIterable;
+import org.apache.poi.util.CodepointsUtil;
+import org.apache.poi.util.Removal;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.model.SharedStringsTable;
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
/**
@@ -52,7 +53,7 @@
* so that it was renamed to "SheetDataWriter"
*/
public class SheetDataWriter implements Closeable {
- private static final POILogger logger = POILogFactory.getLogger(SheetDataWriter.class);
+ private static final Logger LOG = LogManager.getLogger(SheetDataWriter.class);
private final File _fd;
protected final Writer _out;
@@ -82,6 +83,7 @@
this();
this._sharedStringSource = sharedStringsTable;
}
+
/**
* Create a temp file to write sheet data.
* By default, temp files are created in the default temporary-file directory
@@ -89,7 +91,10 @@
* it and specify a different temp directory or filename or suffix, e.g. <code>.gz</code>
*
* @return temp file to write sheet data
+ * @deprecated use {@link TempFile#createTempFile(String, String)} directly
*/
+ @Removal(version = "6.0.0")
+ //make this protected or private in POI 6.0.0 - no need for this to be public
public File createTempFile() throws IOException {
return TempFile.createTempFile("poi-sxssf-sheet", ".xml");
}
@@ -98,7 +103,10 @@
* Create a writer for the sheet data.
*
* @param fd the file to write to
+ * @deprecated this method is due to be made non-public, probably protected
*/
+ @Removal(version = "6.0.0")
+ //make this protected or private in POI 6.0.0 - no need for this to be public
public Writer createWriter(File fd) throws IOException {
FileOutputStream fos = new FileOutputStream(fd);
OutputStream decorated;
@@ -131,7 +139,7 @@
* This method <em>must</em> be invoked before calling {@link #getWorksheetXMLInputStream()}
*/
public void close() throws IOException {
- _out.flush();
+ // this would break writing the same document multiple times: _out.flush();
_out.close();
}
@@ -144,6 +152,9 @@
*/
public InputStream getWorksheetXMLInputStream() throws IOException {
File fd = getTempFile();
+ if (fd == null) {
+ throw new IOException("getWorksheetXMLInputStream only works when a temp file is used");
+ }
FileInputStream fis = new FileInputStream(fd);
try {
return decorateInputStream(fis);
@@ -183,13 +194,6 @@
return _numberLastFlushedRow;
}
- @Override
- protected void finalize() throws Throwable {
- if (_fd.exists() && !_fd.delete()) {
- logger.log(POILogger.ERROR, "Can't delete temporary encryption file: ", _fd);
- }
- }
-
/**
* Write a row to the file
*
@@ -318,7 +322,7 @@
}
case STRING: {
if (_sharedStringSource != null) {
- XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
+ RichTextString rt = cell.getRichStringCellValue();
int sRef = _sharedStringSource.addSharedStringItem(rt);
writeAttribute("t", STCellType.S.toString());
@@ -393,7 +397,8 @@
return;
}
- for (String codepoint : new StringCodepointsIterable(s)) {
+ for (Iterator<String> iter = CodepointsUtil.iteratorFor(s); iter.hasNext(); ) {
+ String codepoint = iter.next();
switch (codepoint) {
case "<":
_out.write("&lt;");
@@ -440,6 +445,10 @@
static boolean replaceWithQuestionMark(char c) {
return c < ' ' || ('\uFFFE' <= c && c <= '\uFFFF');
+ }
+
+ void flush() throws IOException {
+ this._out.flush();
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment