Here you can find the source of encode(final String source, final String encoding, BitSet notEncoded)
private static String encode(final String source, final String encoding, BitSet notEncoded) throws UnsupportedEncodingException
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.util.BitSet; public class Main { private static final int BITS_256 = 256; private static String encode(final String source, final String encoding, BitSet notEncoded) throws UnsupportedEncodingException { byte[] bytes = encode(source.getBytes(encoding), notEncoded); return new String(bytes, "US-ASCII"); }/* w w w.j av a 2 s . co m*/ private static byte[] encode(final byte[] source, final BitSet notEncoded) { ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2); for (int i = 0; i < source.length; i++) { int b = source[i]; if (b < 0) { b += BITS_256; } if (notEncoded.get(b)) { bos.write(b); } else { bos.write('%'); char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)); char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16)); bos.write(hex1); bos.write(hex2); } } return bos.toByteArray(); } }