public class Utf8Utilities extends Object
java.*.Why this class exists where it does. These methods lived in se.entra.phantom.common.Utilities, but the com.netphantom.common packages are duplicated into the com.netphantom.eclipse.common plugin, which does not and cannot carry se.entra.phantom.common. This class therefore depends on nothing but java.* and this package — a property the whole com.netphantom.common tree must keep, or the plugin stops compiling.
Three encoding schemes, deliberately kept apart. Which pair to use depends on what produced or will consume the value:
_encode(String) / _decode(String) — application/x-www-form-urlencoded rules: a space becomes '+'. For query strings and form POST bodies.encodeURIComponent(String) / decodeURIComponent(String) — exactly the JavaScript functions of the same names: a space becomes "%20" and '+' is a literal plus. For values that cross into or out of JavaScript.encodeAnyPath(String) — the path component of a URL: separators normalized to '/', everything reserved or excluded by RFC 2396 escaped, characters above U+FFFF emitted as four-byte UTF-8 (never CESU-8).Case convention, per RFC 3986/3987: escapes are PRODUCED with uppercase hexadecimal ("%C3%A9", never "%c3%a9"), matching URLEncoder and JavaScript; when CONSUMING, hexadecimal digits are accepted in either case, which URLDecoder already guarantees.
All methods are static and thread-safe; the only state is immutable and built in the static initializer.
| Constructor and Description |
|---|
Utf8Utilities() |
| Modifier and Type | Method and Description |
|---|---|
static String | _decode(String s)URL-decodes a string in UTF-8 using application/x-www-form-urlencoded rules. |
static String | _encode(String s)URL-encodes a string in UTF-8 using application/x-www-form-urlencoded rules. |
static int | _escape(char[] cc, char c, int index)Appends the URL escape sequence for the specified char to the specified buffer. |
static String | decodeURIComponent(String s)Decodes a String exactly as the JavaScript decodeURIComponent(string) function does. |
static String | encodeAnyPath(String path)Constructs an encoded version of the specified path string suitable for use in the construction of a URL. |
static String | encodeURIComponent(String s)Encodes a String exactly as the JavaScript encodeURIComponent(string) function does. |
static File | getCanonicalFile(File file)Attempts to get the canonical file of a File, and if it fails, the absolute file is returned. |
static File | getCanonicalFile(String fileName)Attempts to get the canonical file of a File, and if it fails, the absolute file is returned. |
public static File getCanonicalFile(String fileName)
fileName - File or directory.public static File getCanonicalFile(File file)
file - File or directory.public static final String _encode(String s)
application/x-www-form-urlencoded rules.Space handling: a space is encoded as '+', not as "%20", because that is what form encoding specifies. This is the correct pairing for a query string or for a POST body of type application/x-www-form-urlencoded. For JavaScript encodeURIComponent semantics instead, use encodeURIComponent(String).
Do not mix the two: a value encoded with one and decoded with the other loses either every space or every literal plus sign.
s - The string to encode.public static final String _decode(String s)
application/x-www-form-urlencoded rules.Space handling: a '+' is decoded as a space, because that is what form encoding specifies, so a literal plus sign must have been encoded as "%2B". For JavaScript decodeURIComponent semantics, where '+' is left alone, use decodeURIComponent(String) instead.
s - The string to decode.IllegalArgumentException - If s contains a malformed escape sequence, such as a trailing bare '%' or "%ZZ". Any caller handling untrusted input, in particular anything reachable over HTTP, MUST catch this and answer with an error rather than letting it escape.public static String encodeURIComponent(String s)
encodeURIComponent(string) function does.URLEncoder.encode(s,"UTF-8") differs from JavaScript in six places, all of which are corrected here: it encodes a space as '+' rather than "%20", and it escapes ! ~ ' ( ), which JavaScript leaves alone. The unreserved set is then identical: A-Z a-z 0-9 - _ . ! ~ * ' ( ).
The replacements are case sensitive and rely on URLEncoder emitting UPPERCASE hexadecimal, which it does. Do not feed this method output produced by _escape(char[], char, int) unless that output is uppercase too.
s - The string to encode.s is null.public static String decodeURIComponent(String s)
decodeURIComponent(string) function does.The only difference from _decode(String) is the plus sign. URLDecoder follows form-encoding rules and turns '+' into a space; JavaScript does not, and treats it as a literal plus. Every '+' is therefore escaped to "%2B" before decoding, so a literal plus survives.
No other substitution is needed: URLDecoder already decodes %20, %21, %27, %28, %29 and %7E correctly, in either case.
Use this for a value that was produced by encodeURIComponent(String) or by the JavaScript function. For an application/x-www-form-urlencoded query string or POST body, use _decode(String) instead.
s - The string to decode.s is null.IllegalArgumentException - If s contains a malformed escape sequence. See _decode(String).public static String encodeAnyPath(String path)
A path separator is replaced by a forward OR backslash. The string is UTF8 encoded. The % escape sequence is used for characters that are above 0x7F or those defined in RFC2396 as reserved or excluded in the path component of a URL.
Characters above U+FFFF are handled correctly: a surrogate pair is combined into one code point and emitted as a four-byte UTF-8 sequence. Encoding each surrogate separately would produce CESU-8, which no conformant decoder accepts. An unpaired surrogate, which cannot be represented at all, is replaced by U+FFFD.
Escapes are UPPERCASE hexadecimal, matching URLEncoder and JavaScript.
path - The path to encode.public static int _escape(char[] cc,
char c,
int index)The two hexadecimal digits are UPPERCASE, matching URLEncoder and JavaScript. encodeURIComponent(String) performs case-sensitive replacements on %21, %27, %28, %29, %7E and %7e, so lowercase output here would silently fail to match the first four.
cc - Character array for storage of encoded path.c - Character to escape.index - Index in storage array of encoded path.cc character array.Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.