jdk/test/java/io/OutputStreamWriter/Encode.java
changeset 8559 826c03991926
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
equal deleted inserted replaced
8558:e51c07113d09 8559:826c03991926
    33 public class Encode implements Runnable {
    33 public class Encode implements Runnable {
    34     public static void main(String args[]) throws Exception {
    34     public static void main(String args[]) throws Exception {
    35         new Encode();
    35         new Encode();
    36     }
    36     }
    37 
    37 
       
    38     final ServerSocket ss = new ServerSocket(0);
       
    39 
    38     Encode() throws Exception {
    40     Encode() throws Exception {
    39         ss = new ServerSocket(0);
       
    40         (new Thread(this)).start();
    41         (new Thread(this)).start();
    41         String toEncode = "\uD800\uDC00 \uD801\uDC01 ";
    42         String toEncode = "\uD800\uDC00 \uD801\uDC01 ";
    42         String enc1 = URLEncoder.encode(toEncode, "UTF-8");
    43         String enc1 = URLEncoder.encode(toEncode, "UTF-8");
    43         byte bytes[] = {};
    44         byte bytes[] = {};
    44         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    45         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    45         InputStreamReader reader = new InputStreamReader( bais, "8859_1");
    46         InputStreamReader reader = new InputStreamReader( bais, "8859_1");
    46         String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) +
    47         String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) +
    47             "/missing.nothtml";
    48             "/missing.nothtml";
    48         HttpURLConnection uc =  (HttpURLConnection)new URL(url).openConnection();
    49         HttpURLConnection uc =  (HttpURLConnection)new URL(url).openConnection();
    49         uc.connect();
    50         uc.connect();
    50         String enc2 = URLEncoder.encode(toEncode, "UTF-8");
    51         try {
    51         if (!enc1.equals(enc2))
    52             String enc2 = URLEncoder.encode(toEncode, "UTF-8");
    52             throw new RuntimeException("test failed");
    53             if (!enc1.equals(enc2)) {
    53         uc.disconnect();
    54                 System.out.println("test failed");
       
    55                 throw new RuntimeException("test failed");
       
    56             }
       
    57         } finally {
       
    58             uc.disconnect();
       
    59         }
    54     }
    60     }
    55 
    61 
    56     ServerSocket ss;
       
    57 
       
    58     public void run() {
    62     public void run() {
    59         try {
    63         try (ServerSocket serv = ss;
    60             Socket s = ss.accept();
    64              Socket s = serv.accept();
    61             BufferedReader in = new BufferedReader(
    65              BufferedReader in =
    62                 new InputStreamReader(s.getInputStream()));
    66                  new BufferedReader(new InputStreamReader(s.getInputStream())))
       
    67         {
    63             String req = in.readLine();
    68             String req = in.readLine();
    64             PrintStream out = new PrintStream(new BufferedOutputStream(
    69             try (OutputStream os = s.getOutputStream();
    65                 s.getOutputStream()));
    70                  BufferedOutputStream bos = new BufferedOutputStream(os);
    66             out.print("HTTP/1.1 403 Forbidden\r\n");
    71                  PrintStream out = new PrintStream(bos))
    67             out.print("\r\n");
    72             {
    68             out.flush();
    73                 out.print("HTTP/1.1 403 Forbidden\r\n");
    69             s.close();
    74                 out.print("\r\n");
    70             ss.close();
    75             }
    71         } catch (Exception e) {
    76         } catch (Exception e) {
    72             e.printStackTrace();
    77             e.printStackTrace();
    73         }
    78         }
    74     }
    79     }
    75 }
    80 }