[github-340] make more use of StandardCharsets. Thanks to XenoAmess. This closes #340

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902755 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-07-15 21:52:32 +00:00
parent 70bd85fda4
commit 87ee700d04
13 changed files with 39 additions and 32 deletions

View File

@ -22,6 +22,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
@ -62,7 +63,7 @@ public class Msg2txt {
public void processMessage() throws IOException {
String txtFileName = fileNameStem + ".txt";
String attDirName = fileNameStem + "-att";
try (PrintWriter txtOut = new PrintWriter(txtFileName, "UTF-8")) {
try (PrintWriter txtOut = new PrintWriter(txtFileName, StandardCharsets.UTF_8.name())) {
try {
String displayFrom = msg.getDisplayFrom();
txtOut.println("From: " + displayFrom);

View File

@ -190,7 +190,7 @@ public final class ToHtml {
return;
}
try (PrintWriter pw = new PrintWriter(args[1], "UTF-8")) {
try (PrintWriter pw = new PrintWriter(args[1], StandardCharsets.UTF_8.name())) {
ToHtml toHtml = create(args[0], pw);
toHtml.setCompleteHTML(true);
toHtml.printPage();

View File

@ -25,6 +25,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
@ -77,7 +78,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*/
public final class BigGridDemo {
private static final Logger LOG = LogManager.getLogger(BigGridDemo.class);
private static final String XML_ENCODING = "UTF-8";
private static final Random rnd = new Random();
@ -105,7 +105,7 @@ public final class BigGridDemo {
tmp = TempFile.createTempFile("sheet", ".xml");
try (
FileOutputStream stream = new FileOutputStream(tmp);
Writer fw = new OutputStreamWriter(stream, XML_ENCODING)
Writer fw = new OutputStreamWriter(stream, StandardCharsets.UTF_8)
) {
generate(fw, styles);
}
@ -247,7 +247,7 @@ public final class BigGridDemo {
}
void beginSheet() throws IOException {
_out.write("<?xml version=\"1.0\" encoding=\""+XML_ENCODING+"\"?>" +
_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">" );
_out.write("<sheetData>\n");
}

View File

@ -42,7 +42,7 @@ public class TestXLSX2CSV {
// remember and replace default error streams
err = System.err;
PrintStream error = new PrintStream(errorBytes, true, "UTF-8");
PrintStream error = new PrintStream(errorBytes, true, StandardCharsets.UTF_8.name());
System.setErr(error);
}
@ -71,14 +71,14 @@ public class TestXLSX2CSV {
// returns with some System.err
XLSX2CSV.main(new String[] { "not-existing-file.xlsx" });
String output = errorBytes.toString("UTF-8");
String output = errorBytes.toString(StandardCharsets.UTF_8);
assertTrue(output.contains("Not found or not a file: not-existing-file.xlsx"), "Had: " + output);
}
@Test
public void testSampleFile() throws Exception {
final UnsynchronizedByteArrayOutputStream outputBytes = new UnsynchronizedByteArrayOutputStream();
PrintStream out = new PrintStream(outputBytes, true, "UTF-8");
PrintStream out = new PrintStream(outputBytes, true, StandardCharsets.UTF_8.name());
// The package open is instantaneous, as it should be.
try (OPCPackage p = OPCPackage.open(XSSFTestDataSamples.getSampleFile("sample.xlsx").getAbsolutePath(), PackageAccess.READ)) {
@ -97,7 +97,7 @@ public class TestXLSX2CSV {
@Test
public void testMinColumns() throws Exception {
final UnsynchronizedByteArrayOutputStream outputBytes = new UnsynchronizedByteArrayOutputStream();
PrintStream out = new PrintStream(outputBytes, true, "UTF-8");
PrintStream out = new PrintStream(outputBytes, true, StandardCharsets.UTF_8.name());
// The package open is instantaneous, as it should be.
try (OPCPackage p = OPCPackage.open(XSSFTestDataSamples.getSampleFile("sample.xlsx").getAbsolutePath(), PackageAccess.READ)) {

View File

@ -26,6 +26,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.poi.xdgf.usermodel.XDGFPage;
import org.apache.poi.xdgf.usermodel.XDGFShape;
@ -46,7 +47,7 @@ public class HierarchyPrinter {
try (
OutputStream os = new FileOutputStream(pageFile);
PrintStream pos = new PrintStream(os, false, "utf-8")
PrintStream pos = new PrintStream(os, false, StandardCharsets.UTF_8.name())
) {
printHierarchy(page, pos);
}

View File

@ -24,6 +24,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -64,7 +65,7 @@ public class StreamingSheetWriter extends SheetDataWriter {
* @param out the output stream to write to
*/
protected static Writer createWriter(OutputStream out) throws IOException {
return new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
return new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
@Override

View File

@ -74,7 +74,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xml = os.toString("UTF-8");
String xml = os.toString(StandardCharsets.UTF_8);
assertNotNull(xml);
assertFalse(xml.isEmpty());
@ -122,7 +122,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xml = os.toString("UTF-8");
String xml = os.toString(StandardCharsets.UTF_8);
assertNotNull(xml);
assertFalse(xml.isEmpty());
@ -196,7 +196,7 @@ public final class TestXSSFExportToXML {
String xml;
try (UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
exporter.exportToXML(os, true);
xml = os.toString("UTF-8");
xml = os.toString(StandardCharsets.UTF_8);
}
assertNotNull(xml);
@ -258,7 +258,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -302,7 +302,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -337,7 +337,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -376,7 +376,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -402,7 +402,7 @@ public final class TestXSSFExportToXML {
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -480,7 +480,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -558,7 +558,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -589,7 +589,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, false);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -621,7 +621,7 @@ public final class TestXSSFExportToXML {
XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xmlData = os.toString("UTF-8");
String xmlData = os.toString(StandardCharsets.UTF_8);
assertNotNull(xmlData);
assertFalse(xmlData.isEmpty());
@ -647,7 +647,7 @@ public final class TestXSSFExportToXML {
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, false);
assertNotNull(os.toString("UTF-8"));
assertNotNull(os.toString(StandardCharsets.UTF_8));
}
}
}
@ -660,7 +660,7 @@ public final class TestXSSFExportToXML {
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(bos, true);
assertNotNull(DocumentHelper.readDocument(bos.toInputStream()));
String exportedXml = bos.toString("UTF-8");
String exportedXml = bos.toString(StandardCharsets.UTF_8);
assertEquals("<Test><Test>1</Test></Test>", exportedXml.replaceAll("\\s+", ""));
}
}

View File

@ -222,7 +222,7 @@ public final class Chunk {
}
int strLen = endsAt - startsAt;
command.value = new String(contents, startsAt, strLen, header.getChunkCharset().name());
command.value = new String(contents, startsAt, strLen, header.getChunkCharset());
break;
case 25:
command.value = LittleEndian.getShort(contents, offset);

View File

@ -18,6 +18,7 @@
package org.apache.poi.hdgf.chunks;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A chunk header from v4 or v5
@ -59,7 +60,7 @@ public final class ChunkHeaderV4V5 extends ChunkHeader {
@Override
public Charset getChunkCharset() {
return Charset.forName("ASCII");
return StandardCharsets.US_ASCII;
}
void setUnknown2(short unknown2) {

View File

@ -18,6 +18,7 @@
package org.apache.poi.hdgf.chunks;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A chunk header from v6
@ -64,7 +65,7 @@ public class ChunkHeaderV6 extends ChunkHeader {
@Override
public Charset getChunkCharset() {
return Charset.forName("ASCII");
return StandardCharsets.US_ASCII;
}
void setUnknown2(short unknown2) {

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -62,7 +63,7 @@ public class MessageSubmissionChunk extends Chunk {
public void readValue(InputStream value) throws IOException {
// Stored in the file as us-ascii
byte[] data = IOUtils.toByteArray(value);
rawId = new String(data, Charset.forName("ASCII"));
rawId = new String(data, StandardCharsets.US_ASCII);
// Now process the date
String[] parts = rawId.split(";");
@ -112,7 +113,7 @@ public class MessageSubmissionChunk extends Chunk {
@Override
public void writeValue(OutputStream out) throws IOException {
byte[] data = rawId.getBytes(Charset.forName("ASCII"));
final byte[] data = rawId.getBytes(StandardCharsets.US_ASCII);
out.write(data);
}

View File

@ -322,7 +322,7 @@ public final class ExcelCetabFunctionExtractor {
private static void processFile(InputStream input, File outFile) throws IOException {
try (OutputStream os = new SimpleAsciiOutputStream(new FileOutputStream(outFile));
PrintStream ps = new PrintStream(os, true, "UTF-8")) {
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) {
outputLicenseHeader(ps);
Class<?> genClass = ExcelCetabFunctionExtractor.class;

View File

@ -30,6 +30,7 @@ import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
@ -492,7 +493,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
os = new SimpleAsciiOutputStream(os);
PrintStream ps;
try {
ps = new PrintStream(os, true, "UTF-8");
ps = new PrintStream(os, true, StandardCharsets.UTF_8.name());
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}