From cbbf7b529848b123ea0f3dc3850acc3eb449454f Mon Sep 17 00:00:00 2001 From: Dennis Bijlsma Date: Sun, 5 Jan 2020 10:42:00 +0100 Subject: [PATCH] Add support for URL encode/decode methods added in Java 10. --- .../teavm/classlib/java/net/TURLEncoder.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/net/TURLEncoder.java b/classlib/src/main/java/org/teavm/classlib/java/net/TURLEncoder.java index a1d552398..367300021 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/net/TURLEncoder.java +++ b/classlib/src/main/java/org/teavm/classlib/java/net/TURLEncoder.java @@ -51,18 +51,11 @@ public final class TURLEncoder { } return buf.toString(); } - - public static String encode(String s, String enc) throws UnsupportedEncodingException { + + public static String encode(String s, Charset enc) { Objects.requireNonNull(s); Objects.requireNonNull(enc); - // check for UnsupportedEncodingException - try { - Charset.forName(enc); - } catch (UnsupportedCharsetException e) { - throw new UnsupportedEncodingException(enc); - } - // Guess a bit bigger for encoded form StringBuffer buf = new StringBuffer(s.length() + 16); int start = -1; @@ -91,7 +84,19 @@ public final class TURLEncoder { return buf.toString(); } - private static void convert(String s, StringBuffer buf, String enc) throws UnsupportedEncodingException { + public static String encode(String s, String enc) throws UnsupportedEncodingException { + Objects.requireNonNull(s); + Objects.requireNonNull(enc); + + // check for UnsupportedEncodingException + try { + return encode(s, Charset.forName(enc)); + } catch (UnsupportedCharsetException e) { + throw new UnsupportedEncodingException(enc); + } + } + + private static void convert(String s, StringBuffer buf, Charset enc) { byte[] bytes = s.getBytes(enc); for (int j = 0; j < bytes.length; j++) { buf.append('%');