Java String Encode by Charset urlEncode(final String content, final Charset charset, final BitSet safechars, final boolean blankAsPlus)

Here you can find the source of urlEncode(final String content, final Charset charset, final BitSet safechars, final boolean blankAsPlus)

Description

url Encode

License

Apache License

Declaration

private static String urlEncode(final String content, final Charset charset, final BitSet safechars,
            final boolean blankAsPlus) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

import java.nio.charset.Charset;
import java.util.*;

public class Main {
    private static final int RADIX = 16;

    private static String urlEncode(final String content, final Charset charset, final BitSet safechars,
            final boolean blankAsPlus) {
        if (content == null) {
            return null;
        }//  ww  w  . jav  a2  s  . c o  m
        final StringBuilder buf = new StringBuilder();
        final ByteBuffer bb = charset.encode(content);
        while (bb.hasRemaining()) {
            final int b = bb.get() & 0xff;
            if (safechars.get(b)) {
                buf.append((char) b);
            } else if (blankAsPlus && b == ' ') {
                buf.append('+');
            } else {
                buf.append("%");
                final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
                final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
                buf.append(hex1);
                buf.append(hex2);
            }
        }
        return buf.toString();
    }
}

Related

  1. getEncoder(Charset charset)
  2. getEncoder(String charset)
  3. getHeader(Charset encoding)
  4. loadAsString(String locationPattern, Charset encoding)
  5. pathEncode(String path, Charset charset)
  6. urlEncode(final String str, final Charset encoding)
  7. urlEncode(String url, Charset cs)