From 110f82b9197a9544599d3dba2d752c78d1dfbc13 Mon Sep 17 00:00:00 2001 From: James Cover jdcove2 Date: Fri, 1 Nov 2024 20:25:27 +0000 Subject: [PATCH] Adds a requiresEncoding(Reader) method. --- .../core/IBaseDataObjectXmlCodecs.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/emissary/core/IBaseDataObjectXmlCodecs.java b/src/main/java/emissary/core/IBaseDataObjectXmlCodecs.java index 0bbd4840a0..a548168113 100644 --- a/src/main/java/emissary/core/IBaseDataObjectXmlCodecs.java +++ b/src/main/java/emissary/core/IBaseDataObjectXmlCodecs.java @@ -15,7 +15,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.AbstractMap; @@ -827,13 +830,21 @@ public static Method getIbdoMethod(final String methodName, final Class... pa return IBaseDataObject.class.getMethod(methodName, parameterTypes); } - // https://stackoverflow.com/questions/3770117/what-is-the-range-of-unicode-printable-characters public static boolean requiresEncoding(final byte[] utf8Bytes) { - final String string = new String(utf8Bytes, StandardCharsets.UTF_8); + try (ByteArrayInputStream bais = new ByteArrayInputStream(utf8Bytes); + Reader r = new InputStreamReader(bais, StandardCharsets.UTF_8)) { + return requiresEncoding(r); + } catch (IOException e) { + // Should never happen given a byte array. + } - for (int i = 0; i < string.length(); i++) { - final char c = string.charAt(i); + return true; + } + // https://stackoverflow.com/questions/3770117/what-is-the-range-of-unicode-printable-characters + public static boolean requiresEncoding(final Reader reader) throws IOException { + int c; + while ((c = reader.read()) >= 0) { if (('\u0000' <= c && c <= '\u0008') || ('\u000E' <= c && c <= '\u001F') || ('\u007F' <= c && c <= '\u009F') ||