0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A @bug 4922813
0N/A @summary Check the new impl of encodePath will not cause regression
0N/A */
0N/A
0N/Aimport java.util.BitSet;
0N/Aimport java.io.File;
0N/Aimport java.util.Random;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/Apublic class ParseUtil_4922813 {
0N/A public static void main(String[] argv) throws Exception {
0N/A
0N/A int num = 400;
0N/A while (num-- >= 0) {
0N/A String source = getTestSource();
0N/A String ec = sun.net.www.ParseUtil.encodePath(source);
0N/A String v117 = ParseUtil_V117.encodePath(source);
0N/A if (!ec.equals(v117)) {
0N/A throw new RuntimeException("Test Failed for : \n"
0N/A + " source =<"
0N/A + getUnicodeString(source)
0N/A + ">");
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A static int maxCharCount = 200;
0N/A static int maxCodePoint = 0x10ffff;
0N/A static Random random;
0N/A static String getTestSource() {
0N/A if (random == null) {
0N/A long seed = System.currentTimeMillis();
0N/A random = new Random(seed);
0N/A }
0N/A String source = "";
0N/A int i = 0;
0N/A int count = random.nextInt(maxCharCount) + 1;
0N/A while (i < count) {
0N/A int codepoint = random.nextInt(127);
0N/A source = source + String.valueOf((char)codepoint);
0N/A
0N/A codepoint = random.nextInt(0x7ff);
0N/A source = source + String.valueOf((char)codepoint);
0N/A
0N/A codepoint = random.nextInt(maxCodePoint);
0N/A source = source + new String(Character.toChars(codepoint));
0N/A
0N/A i += 3;
0N/A }
0N/A return source;
0N/A }
0N/A
0N/A static String getUnicodeString(String s){
0N/A String unicodeString = "";
0N/A for(int j=0; j< s.length(); j++){
0N/A unicodeString += "0x"+ Integer.toString(s.charAt(j), 16);
0N/A }
0N/A return unicodeString;
0N/A }
0N/A}
0N/Aclass ParseUtil_V117 {
0N/A static BitSet encodedInPath;
0N/A static {
0N/A encodedInPath = new BitSet(256);
0N/A
0N/A // Set the bits corresponding to characters that are encoded in the
0N/A // path component of a URI.
0N/A
0N/A // These characters are reserved in the path segment as described in
0N/A // RFC2396 section 3.3.
0N/A encodedInPath.set('=');
0N/A encodedInPath.set(';');
0N/A encodedInPath.set('?');
0N/A encodedInPath.set('/');
0N/A
0N/A // These characters are defined as excluded in RFC2396 section 2.4.3
0N/A // and must be escaped if they occur in the data part of a URI.
0N/A encodedInPath.set('#');
0N/A encodedInPath.set(' ');
0N/A encodedInPath.set('<');
0N/A encodedInPath.set('>');
0N/A encodedInPath.set('%');
0N/A encodedInPath.set('"');
0N/A encodedInPath.set('{');
0N/A encodedInPath.set('}');
0N/A encodedInPath.set('|');
0N/A encodedInPath.set('\\');
0N/A encodedInPath.set('^');
0N/A encodedInPath.set('[');
0N/A encodedInPath.set(']');
0N/A encodedInPath.set('`');
0N/A
0N/A // US ASCII control characters 00-1F and 7F.
0N/A for (int i=0; i<32; i++)
0N/A encodedInPath.set(i);
0N/A encodedInPath.set(127);
0N/A }
0N/A /**
0N/A * Constructs an encoded version of the specified path string suitable
0N/A * for use in the construction of a URL.
0N/A *
0N/A * A path separator is replaced by a forward slash. The string is UTF8
0N/A * encoded. The % escape sequence is used for characters that are above
0N/A * 0x7F or those defined in RFC2396 as reserved or excluded in the path
0N/A * component of a URL.
0N/A */
0N/A public static String encodePath(String path) {
0N/A StringBuffer sb = new StringBuffer();
0N/A int n = path.length();
0N/A for (int i=0; i<n; i++) {
0N/A char c = path.charAt(i);
0N/A if (c == File.separatorChar)
0N/A sb.append('/');
0N/A else {
0N/A if (c <= 0x007F) {
0N/A if (encodedInPath.get(c))
0N/A escape(sb, c);
0N/A else
0N/A sb.append(c);
0N/A } else if (c > 0x07FF) {
0N/A escape(sb, (char)(0xE0 | ((c >> 12) & 0x0F)));
0N/A escape(sb, (char)(0x80 | ((c >> 6) & 0x3F)));
0N/A escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
0N/A } else {
0N/A escape(sb, (char)(0xC0 | ((c >> 6) & 0x1F)));
0N/A escape(sb, (char)(0x80 | ((c >> 0) & 0x3F)));
0N/A }
0N/A }
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Appends the URL escape sequence for the specified char to the
0N/A * specified StringBuffer.
0N/A */
0N/A private static void escape(StringBuffer s, char c) {
0N/A s.append('%');
0N/A s.append(Character.forDigit((c >> 4) & 0xF, 16));
0N/A s.append(Character.forDigit(c & 0xF, 16));
0N/A }
0N/A}