Top Description Fields Constructors Methods
java.net

public Class URLEncoder

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.CharacterCodingException, .Charset, .CharsetEncoder, .CoderResult, .CodingErrorAction, .IllegalCharsetNameException, .UnsupportedCharsetException, java.util.BitSet, .Objects, .HexFormat, java.util.function.IntPredicate, jdk.internal.util.ImmutableBitSetPredicate

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

When encoding a String, the following rules apply:

For example using UTF-8 as the encoding scheme the string "The string ü@foo-bar" would get converted to "The+string+%C3%BC%40foo-bar" because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

Author
Herb Jellinek
Since
1.0
External Specification
https://www.w3.org/TR/html4
See Also
Charset#defaultCharset()

Field Summary

Modifier and TypeField and Description
private static final IntPredicate
private static final int

Constructor Summary

AccessConstructor and Description
private
URLEncoder()

You can't call the constructor.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the translated String.
encode
(String
String to be translated.
s
)
Deprecated The resulting string may vary depending on the default charset. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format.

public static String

Returns:

the translated String.
encode
(String
String to be translated.
s
,
String
The name of a supported character encoding.
enc
)

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.

public static String

Returns:

the translated String.
encode
(String
String to be translated.
s
,
Charset
the given charset
charset
)

Translates a string into application/x-www-form-urlencoded format using a specific Charset.

private static void
flushToStringBuilder(StringBuilder
the StringBuilder to output encoded and escaped bytes to
out
,
CharsetEncoder
charset encoder. Will be reset if endOfInput is true
ce
,
CharBuffer
input buffer, will be cleared
cb
,
ByteBuffer
output buffer, will be cleared
bb
,
boolean
true if this is the last flush for an encoding chunk, to all bytes in ce is flushed to out and reset
endOfInput
)

Encodes input chars in cb and appends the byte values in an escaped format ("%XX") to out.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DONT_NEED_ENCODINGback to summary
private static final IntPredicate DONT_NEED_ENCODING
ENCODING_CHUNK_SIZEback to summary
private static final int ENCODING_CHUNK_SIZE

Constructor Detail

URLEncoderback to summary
private URLEncoder()

You can't call the constructor.

Method Detail

encodeback to summary
public static String encode(String s)

Deprecated

The resulting string may vary depending on the default charset. Instead, use the encode(String,String) method to specify the encoding.

Translates a string into x-www-form-urlencoded format. This method uses the default charset as the encoding scheme to obtain the bytes for unsafe characters.

Parameters
s:String

String to be translated.

Returns:String

the translated String.

Annotations
@Deprecated
encodeback to summary
public static String encode(String s, String enc) throws UnsupportedEncodingException

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.

This method behaves the same as encode(String s, Charset charset) except that it will look up the charset using the given encoding name.

Parameters
s:String

String to be translated.

enc:String

The name of a supported character encoding.

Returns:String

the translated String.

Exceptions
UnsupportedEncodingException:
If the named encoding is not supported
Since
1.4
See Also
URLDecoder#decode(java.lang.String, java.lang.String)
encodeback to summary
public static String encode(String s, Charset charset)

Translates a string into application/x-www-form-urlencoded format using a specific Charset. This method uses the supplied charset to obtain the bytes for unsafe characters.

If the input string is malformed, or if the input cannot be mapped to a valid byte sequence in the given Charset, then the erroneous input will be replaced with the Charset's replacement values.

API Note

The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Parameters
s:String

String to be translated.

charset:Charset

the given charset

Returns:String

the translated String.

Exceptions
NullPointerException:
if s or charset is null.
Since
10
External Specification
https://www.w3.org/TR/html4
See Also
URLDecoder#decode(java.lang.String, Charset)
flushToStringBuilderback to summary
private static void flushToStringBuilder(StringBuilder out, CharsetEncoder ce, CharBuffer cb, ByteBuffer bb, boolean endOfInput)

Encodes input chars in cb and appends the byte values in an escaped format ("%XX") to out. The temporary byte buffer, bb, must be able to accept cb.position() * ce.maxBytesPerChar() bytes.

Parameters
out:StringBuilder

the StringBuilder to output encoded and escaped bytes to

ce:CharsetEncoder

charset encoder. Will be reset if endOfInput is true

cb:CharBuffer

input buffer, will be cleared

bb:ByteBuffer

output buffer, will be cleared

endOfInput:boolean

true if this is the last flush for an encoding chunk, to all bytes in ce is flushed to out and reset