Java BitSet escape(String s, BitSet safeChars)

Here you can find the source of escape(String s, BitSet safeChars)

Description

Escapes the given string using URL encoding for all bytes not included in the given set of safe characters.

License

Apache License

Parameter

Parameter Description
s the string to escape
safeChars set of safe characters (bytes)

Return

escaped string

Declaration

private static String escape(String s, BitSet safeChars) 

Method Source Code

//package com.java2s;
/*//ww w.j a v  a  2 s.  c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.BitSet;

public class Main {
    /**
     * The escape character used to mark hex escape sequences.
     */
    private static final char ESCAPE_CHAR = '_';

    /**
     * Escapes the given string using URL encoding for all bytes not included
     * in the given set of safe characters.
     *
     * @param s the string to escape
     * @param safeChars set of safe characters (bytes)
     * @return escaped string
     */
    private static String escape(String s, BitSet safeChars) {
        byte[] bytes = s.getBytes();
        StringBuilder out = new StringBuilder(bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            int c = bytes[i] & 0xff;
            if (safeChars.get(c) && c != ESCAPE_CHAR) {
                out.append((char) c);
            } else {
                out.append(ESCAPE_CHAR);
            }
        }
        return out.toString();
    }
}

Related

  1. dualNext(BitSet b)
  2. encode(BitSet allowedCharacters, String s, String charset)
  3. encode(BitSet origin, int originBitLength)
  4. encode(String s, BitSet bs)
  5. ensureExclusivity(BitSet bs1, BitSet bs2)
  6. expand(final BitSet bits, final BitSet add)
  7. extractLong(BitSet bitSet, int from, int to)
  8. extractSetBitsMsgFromTxSet(final BitSet txSet)
  9. filter(double[] data, BitSet mask)