Here you can find the source of encode(BitSet allowedCharacters, String s, String charset)
public static String encode(BitSet allowedCharacters, String s, String charset)
//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(); } }