Here you can find the source of escape(String s, BitSet safeChars)
Parameter | Description |
---|---|
s | the string to escape |
safeChars | set of safe characters (bytes) |
private static String escape(String s, BitSet safeChars)
//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(); } }