Java BitSet encode(BitSet allowedCharacters, String s, String charset)

Here you can find the source of encode(BitSet allowedCharacters, String s, String charset)

Description

encode

License

CDDL license

Declaration

public static String encode(BitSet allowedCharacters, String s, String charset) 

Method Source Code

//package com.java2s;
/**//from   www  .j  a  v  a  2  s .  c o  m
 * Copyright (C) 2014 4th Line GmbH, Switzerland and others
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License Version 1 or later
 * ("CDDL") (collectively, the "License"). You may not use this file
 * except in compliance with the License. See LICENSE.txt for more
 * information.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.util.BitSet;

public class Main {
    public static String encode(BitSet allowedCharacters, String s, String charset) {
        if (s == null)
            return null;
        final StringBuilder encoded = new StringBuilder(s.length() * 3);
        final char[] characters = s.toCharArray();
        try {
            for (char c : characters) {
                if (allowedCharacters.get(c)) {
                    encoded.append(c);
                } else {
                    byte[] bytes = String.valueOf(c).getBytes(charset);
                    for (byte b : bytes)
                        encoded.append(String.format("%%%1$02X", b & 0xFF));
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return encoded.toString();
    }
}

Related

  1. createSimpleBitSet(Set setBits)
  2. decode(BitSet encoded, int encodedBitLength)
  3. decodeIndex(final BitSet bs, final long rangePerDimension)
  4. deleteBits(BitSet bs, BitSet bsDelete)
  5. dualNext(BitSet b)
  6. encode(BitSet origin, int originBitLength)
  7. encode(String s, BitSet bs)
  8. ensureExclusivity(BitSet bs1, BitSet bs2)
  9. escape(String s, BitSet safeChars)