Skip to content

Commit

Permalink
Adds a requiresEncoding(Reader) method.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cover jdcove2 committed Nov 1, 2024
1 parent 15d0330 commit 110f82b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main/java/emissary/core/IBaseDataObjectXmlCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') ||
Expand Down

0 comments on commit 110f82b

Please sign in to comment.