Path: blob/master/test/jdk/sun/net/www/protocol/file/EncodedMultiByteChar.java
41159 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 652116626* @summary Exception when opening file URLConnection with percent encoded 4 byte UTF8 char27*/2829import java.net.*;30import java.io.*;3132/* java.lang.IllegalArgumentException if fails */3334public class EncodedMultiByteChar35{36static String filename = "EncodedMultiByteChar" + new String(Character.toChars(0x2123D));37static String urlStr;38static String message = "This is a message";3940static {41try {42urlStr = "file://" + System.getProperty("java.io.tmpdir") + "/EncodedMultiByteChar" +43URLEncoder.encode(new String(Character.toChars(0x2123D)), "UTF-8") + ".txt";44} catch (UnsupportedEncodingException e) {45assert false;46}47}4849public static void main(String[] args) {50File file = null;51try {52//Create a file with a 4 byte UTF8 character in its name.53file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".txt");54file.createNewFile();55file.deleteOnExit();5657FileOutputStream fos = new FileOutputStream(file);58fos.write(message.getBytes("UTF-8"));59fos.close();60} catch (IOException e) {61System.out.println("Failed to create test file ");62e.printStackTrace();63return;64}6566System.out.println("file = " + file);6768try {69System.out.println("URL = " + urlStr);70URL url = new URL(urlStr);71URLConnection conn = url.openConnection();72BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));7374String line;75while ((line = reader.readLine()) != null) {76if (!line.equals(message)) {77throw new RuntimeException("Failed: read \"" + line + "\" from file");78}79}80} catch (IOException ioe) {81ioe.printStackTrace();82}83}84}858687