Here you can find the source of encodePath(String path)
public static String encodePath(String path)
//package com.java2s; /*/*from ww w . j a v a2s .c om*/ * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code 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. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ import java.util.BitSet; import java.io.File; public class Main { static BitSet encodedInPath; /** * Constructs an encoded version of the specified path string suitable * for use in the construction of a URL. * * A path separator is replaced by a forward slash. The string is UTF8 * encoded. The % escape sequence is used for characters that are above * 0x7F or those defined in RFC2396 as reserved or excluded in the path * component of a URL. */ public static String encodePath(String path) { return encodePath(path, true); } public static String encodePath(String path, boolean flag) { char[] retCC = new char[path.length() * 2 + 16]; int retLen = 0; char[] pathCC = path.toCharArray(); int n = path.length(); for (int i = 0; i < n; i++) { char c = pathCC[i]; if ((!flag && c == '/') || (flag && c == File.separatorChar)) retCC[retLen++] = '/'; else { if (c <= 0x007F) { if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') { retCC[retLen++] = c; } else if (encodedInPath.get(c)) retLen = escape(retCC, c, retLen); else retCC[retLen++] = c; } else if (c > 0x07FF) { retLen = escape(retCC, (char) (0xE0 | ((c >> 12) & 0x0F)), retLen); retLen = escape(retCC, (char) (0x80 | ((c >> 6) & 0x3F)), retLen); retLen = escape(retCC, (char) (0x80 | ((c >> 0) & 0x3F)), retLen); } else { retLen = escape(retCC, (char) (0xC0 | ((c >> 6) & 0x1F)), retLen); retLen = escape(retCC, (char) (0x80 | ((c >> 0) & 0x3F)), retLen); } } //worst case scenario for character [0x7ff-] every single //character will be encoded into 9 characters. if (retLen + 9 > retCC.length) { int newLen = retCC.length * 2 + 16; if (newLen < 0) { newLen = Integer.MAX_VALUE; } char[] buf = new char[newLen]; System.arraycopy(retCC, 0, buf, 0, retLen); retCC = buf; } } return new String(retCC, 0, retLen); } /** * Appends the URL escape sequence for the specified char to the * specified StringBuffer. */ private static int escape(char[] cc, char c, int index) { cc[index++] = '%'; cc[index++] = Character.forDigit((c >> 4) & 0xF, 16); cc[index++] = Character.forDigit(c & 0xF, 16); return index; } }