Java Utililty Methods String Encode by Charset

List of utility methods to do String Encode by Charset

Description

The list of methods to do String Encode by Charset are organized into topic(s).

Method

Stringencode(String value, Charset charset)
Uses URLEncoder to translate a string into application/x-www-form-urlencoded format, using the given Charset.
try {
    return URLEncoder.encode(value, charset.name());
} catch (UnsupportedEncodingException ex) {
    throw new IllegalArgumentException("Characterset is not supported.");
StringencodeBase64(String s, Charset cs)
encode Base
String out = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    OutputStream os = MimeUtility.encode(baos, "BASE64");
    os.write(s.getBytes(cs));
    out = new String(baos.toByteArray(), cs);
} catch (IOException ignore) {
    ;
...
byte[]encodeCHARSET(String string, Charset charset)
Codifica un String para un juego de caracteres
return string.getBytes(charset);
StringencodeFormFields(final String content, final Charset charset)
encode Form Fields
if (content == null) {
    return null;
return urlEncode(content, charset != null ? charset : Charset.forName("UTF-8"), URLENCODER, true);
intencodeOneChar(CharsetEncoder encoder, int uchar)
Encode a single character in UTF-16.
ByteBuffer bb = ByteBuffer.allocate(10);
CharBuffer cb = CharBuffer.wrap(new char[] { (char) uchar });
CoderResult result = encoder.encode(cb, bb, true);
if (result.isError()) {
    return 0;
if (bb.position() > 4) {
    return 0;
...
Stringencoding(final String content, final Charset charset, final boolean blankAsPlus)
encoding
if (content == null) {
    return null;
StringBuilder buf = new StringBuilder();
ByteBuffer bb = charset.encode(content);
while (bb.hasRemaining()) {
    int b = bb.get() & 0xff;
    if (URLENCODER.get(b)) {
...
ListensureEncodingSet(List options, Charset defaultEncoding)
If there is no encoding set, make sure to set the default encoding.
if (getEncodingOption(options) == null) {
    options.add("-encoding");
    options.add(defaultEncoding.name());
return options;
CharsetEncodergetEncoder(Charset charset)
get Encoder
if (charset == null)
    throw new IllegalArgumentException(
            "charset cannot be null, consider using one of the pre-defined Charset constants from this class for convenience.");
CharsetEncoder result = encoderMap.get(charset);
if (result == null) {
    result = charset.newEncoder();
    encoderMap.put(charset, result);
return result;
CharsetEncodergetEncoder(Charset charset)
Returns a cached thread-local CharsetEncoder for the specified charset.
if (charset == null) {
    throw new NullPointerException("charset");
Map<Charset, CharsetEncoder> map = encoders.get();
CharsetEncoder e = map.get(charset);
if (e != null) {
    e.reset();
    e.onMalformedInput(CodingErrorAction.REPLACE);
...
CharsetEncodergetEncoder(String charset)
get Encoder
return Charset.forName(charset).newEncoder();