J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6521166 |
| 27 | * @summary Exception when opening file URLConnection with percent encoded 4 byte UTF8 char |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | /* java.lang.IllegalArgumentException if fails */ |
| 34 | |
| 35 | public class EncodedMultiByteChar |
| 36 | { |
| 37 | static String filename = "EncodedMultiByteChar" + new String(Character.toChars(0x2123D)); |
| 38 | static String urlStr; |
| 39 | static String message = "This is a message"; |
| 40 | |
| 41 | static { |
| 42 | try { |
| 43 | urlStr = "file://" + System.getProperty("java.io.tmpdir") + "/EncodedMultiByteChar" + |
| 44 | URLEncoder.encode(new String(Character.toChars(0x2123D)), "UTF-8") + ".txt"; |
| 45 | } catch (UnsupportedEncodingException e) { |
| 46 | assert false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static void main(String[] args) { |
| 51 | File file = null; |
| 52 | try { |
| 53 | //Create a file with a 4 byte UTF8 character in its name. |
| 54 | file = new File(System.getProperty("java.io.tmpdir") + File.separator + filename + ".txt"); |
| 55 | file.createNewFile(); |
| 56 | file.deleteOnExit(); |
| 57 | |
| 58 | FileOutputStream fos = new FileOutputStream(file); |
| 59 | fos.write(message.getBytes("UTF-8")); |
| 60 | fos.close(); |
| 61 | } catch (IOException e) { |
| 62 | System.out.println("Failed to create test file "); |
| 63 | e.printStackTrace(); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | System.out.println("file = " + file); |
| 68 | |
| 69 | try { |
| 70 | System.out.println("URL = " + urlStr); |
| 71 | URL url = new URL(urlStr); |
| 72 | URLConnection conn = url.openConnection(); |
| 73 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 74 | |
| 75 | String line; |
| 76 | while ((line = reader.readLine()) != null) { |
| 77 | if (!line.equals(message)) { |
| 78 | throw new RuntimeException("Failed: read \"" + line + "\" from file"); |
| 79 | } |
| 80 | } |
| 81 | } catch (IOException ioe) { |
| 82 | ioe.printStackTrace(); |
| 83 | } |
| 84 | } |
| 85 | } |