Java String Decode by Charset decodeFormFields(final String content, final Charset charset)

Here you can find the source of decodeFormFields(final String content, final Charset charset)

Description

decode Form Fields

License

Open Source License

Declaration

private static String decodeFormFields(final String content, final Charset charset) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    private static String decodeFormFields(final String content, final Charset charset) {
        if (content == null) {
            return null;
        }/*from   ww  w .  jav a  2 s . co m*/
        return urlDecode(content, charset != null ? charset : Charset.forName("UTF-8"), true);
    }

    private static String urlDecode(final String content, final Charset charset, final boolean plusAsBlank) {
        if (content == null) {
            return null;
        }
        final ByteBuffer bb = ByteBuffer.allocate(content.length());
        final CharBuffer cb = CharBuffer.wrap(content);
        while (cb.hasRemaining()) {
            final char c = cb.get();
            if (c == '%' && cb.remaining() >= 2) {
                final char uc = cb.get();
                final char lc = cb.get();
                final int u = Character.digit(uc, 16);
                final int l = Character.digit(lc, 16);
                if (u != -1 && l != -1) {
                    bb.put((byte) ((u << 4) + l));
                } else {
                    bb.put((byte) '%');
                    bb.put((byte) uc);
                    bb.put((byte) lc);
                }
            } else if (plusAsBlank && c == '+') {
                bb.put((byte) ' ');
            } else {
                bb.put((byte) c);
            }
        }
        bb.flip();
        return charset.decode(bb).toString();
    }
}

Related

  1. decode(String url, Charset charset)
  2. decode(String value, Charset charset)
  3. decode(String value, Charset charset)
  4. decodeCharset(String value, String charset)
  5. decodeComponent(final String s, final Charset charset)
  6. decodeURL(@Nullable String str, Charset charSet)
  7. decodeWithDefaultCharSet(String urlToDecode)
  8. getCharsetDecoder(String charsetName)
  9. getDecoder(Charset charset)