Java String Encode encode(final String source, final String encoding, BitSet notEncoded)

Here you can find the source of encode(final String source, final String encoding, BitSet notEncoded)

Description

encode

License

Open Source License

Declaration

private static String encode(final String source, final String encoding, BitSet notEncoded)
            throws UnsupportedEncodingException 

Method Source Code


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

Related

  1. encode(final String s)
  2. encode(final String s)
  3. encode(final String s, final String encoding)
  4. encode(String data, String encode)
  5. encode(String encoding, String string)
  6. encode(String encoding, String text)
  7. encode(String name)