2.9 KiB
URL Encoder for Java
A simple library to encode/decode URL parameters.
This library was extracted from the RIFE2 Web Application Framework.
A Kotlin version can also be found at https://github.com/ethauvin/urlencoder.
For decades, we've been using java.net.URLEncoder because of its improper naming. It is actually intended to encode HTML form parameters, not URLs, causing the wrong escape sequences to be used.
Additionally, java.net.URLEncoder
allocates memory even when no encoding is
necessary, significantly impacting performance. This library has a negligible
performance impact when a specified string doesn't need to be encoded.
Android's Uri.encode also addresses these issues, but does not currently support unicode surrogate pairs.
Examples (TL;DR)
UrlEncoder.encode("a test &"); // -> "a%20test%20%26"
UrlEncoder.encode("%#okékÉȢ smile!😁"); // -> "%25%23ok%C3%A9k%C3%89%C8%A2%20smile%21%F0%9F%98%81"
UrlEncoder.encode("?test=a test", "?="); // -> ?test=a%20test
UrlEncoder.decode("a%20test%20%26"); // -> "a test &"
UrlEncoder.decode("%25%23ok%C3%A9k%C3%89%C8%A2%20smile%21%F0%9F%98%81"); // -> "%#okékÉȢ smile!😁"
Gradle, Maven, etc.
To use with Gradle, include the following dependency in your build file:
dependencies {
implementation("com.uwyn:urlencoder:1.0.0")
}
Instructions for using with Maven, Ivy, etc. can be found on Maven Central.