From 80df21b22b425b0da19f29053c6b7b6997b9014b Mon Sep 17 00:00:00 2001 From: nco Date: Fri, 14 Jul 2017 12:25:37 +0200 Subject: [PATCH] Fix JSONEncoder for non-printable characters formatter accumulates internally what is being formatted. This quickly lead to OutOfMemoryException when encoding String with lots of non-printable characters --- .../java/com/sun/jersey/json/impl/writer/JsonEncoder.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jersey-json/src/main/java/com/sun/jersey/json/impl/writer/JsonEncoder.java b/jersey-json/src/main/java/com/sun/jersey/json/impl/writer/JsonEncoder.java index 91f5bc3eb..3b0d1570e 100644 --- a/jersey-json/src/main/java/com/sun/jersey/json/impl/writer/JsonEncoder.java +++ b/jersey-json/src/main/java/com/sun/jersey/json/impl/writer/JsonEncoder.java @@ -51,8 +51,8 @@ public static String encode(String text) { if ((null == text) || (text.length() == 0)) { return text; } - Formatter formatter = new java.util.Formatter(); StringBuffer result = new StringBuffer(); + Formatter formatter = new java.util.Formatter(result); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); switch (c) { @@ -79,12 +79,13 @@ public static String encode(String text) { break; default: if (c < ' ') { // TODO: what about c > 255 ??? - result.append(formatter.format("\\u%04X", (int)c)); + formatter.format("\\u%04X", (int)c); } else { result.append(c); } } } + formatter.close(); return result.toString(); } }