Here you can find the source of encodePart(final String part, final String charset, final BitSet allowed)
Parameter | Description |
---|---|
UnsupportedEncodingException | an exception |
public static String encodePart(final String part, final String charset, final BitSet allowed) throws UnsupportedEncodingException
//package com.java2s; import java.io.UnsupportedEncodingException; import java.util.BitSet; public class Main { /**/*w w w. ja v a 2s . com*/ * Encodes a string to be a valid URI part, with the given characters allowed. The rest will be encoded. * * @throws UnsupportedEncodingException */ public static String encodePart(final String part, final String charset, final BitSet allowed) throws UnsupportedEncodingException { if (part == null) { return null; } // start at *3 for the worst case when everything is %encoded on one byte final StringBuffer encoded = new StringBuffer(part.length() * 3); final char[] toEncode = part.toCharArray(); for (final char c : toEncode) { if (allowed.get(c)) { encoded.append(c); } else { final byte[] bytes = String.valueOf(c).getBytes(charset); for (final byte b : bytes) { // make it unsigned final int u8 = b & 0xFF; encoded.append(String.format("%%%1$02X", u8)); } } } return encoded.toString(); } }