Path: blob/master/test/jdk/java/io/OutputStreamWriter/Encode.java
41149 views
/*1* Copyright (c) 2003, 2011, 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 480220926* @summary check that the right utf-8 encoder is used27*/2829import java.io.*;30import java.net.*;3132public class Encode implements Runnable {33public static void main(String args[]) throws Exception {34new Encode();35}3637final ServerSocket ss = new ServerSocket(0);3839Encode() throws Exception {40(new Thread(this)).start();41String toEncode = "\uD800\uDC00 \uD801\uDC01 ";42String enc1 = URLEncoder.encode(toEncode, "UTF-8");43byte bytes[] = {};44ByteArrayInputStream bais = new ByteArrayInputStream(bytes);45InputStreamReader reader = new InputStreamReader( bais, "8859_1");46String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) +47"/missing.nothtml";48HttpURLConnection uc = (HttpURLConnection)new URL(url).openConnection();49uc.connect();50try {51String enc2 = URLEncoder.encode(toEncode, "UTF-8");52if (!enc1.equals(enc2)) {53System.out.println("test failed");54throw new RuntimeException("test failed");55}56} finally {57uc.disconnect();58}59}6061public void run() {62try (ServerSocket serv = ss;63Socket s = serv.accept();64BufferedReader in =65new BufferedReader(new InputStreamReader(s.getInputStream())))66{67String req = in.readLine();68try (OutputStream os = s.getOutputStream();69BufferedOutputStream bos = new BufferedOutputStream(os);70PrintStream out = new PrintStream(bos))71{72out.print("HTTP/1.1 403 Forbidden\r\n");73out.print("\r\n");74}75} catch (Exception e) {76e.printStackTrace();77}78}79}808182