Java Encode encodePart(final String part, final String charset, final BitSet allowed)

Here you can find the source of encodePart(final String part, final String charset, final BitSet allowed)

Description

Encodes a string to be a valid URI part, with the given characters allowed.

License

Open Source License

Exception

Parameter Description
UnsupportedEncodingException an exception

Declaration

public static String encodePart(final String part, final String charset, final BitSet allowed)
        throws UnsupportedEncodingException 

Method Source Code


//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();
    }
}

Related

  1. encodeKeyForFileSystemName(String originalKey)
  2. encodeObject(final Object object)
  3. encodeObject(Object obj)
  4. encodeOID(byte[] in, OutputStream os)
  5. encodeParameter(String name, String value, String encoding, String lang)
  6. encodePersonal( InternetAddress[] addresses)
  7. encodeQP(String text)
  8. encodeResult(final Object result)
  9. encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out)