test/java/io/OutputStreamWriter/Encode.java

Print this page
rev 3565 : 7022624: use try-with-resources in java.io tests
Reviewed-by: XXX


  39         ss = new ServerSocket(0);
  40         (new Thread(this)).start();
  41         String toEncode = "\uD800\uDC00 \uD801\uDC01 ";
  42         String enc1 = URLEncoder.encode(toEncode, "UTF-8");
  43         byte bytes[] = {};
  44         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  45         InputStreamReader reader = new InputStreamReader( bais, "8859_1");
  46         String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) +
  47             "/missing.nothtml";
  48         HttpURLConnection uc =  (HttpURLConnection)new URL(url).openConnection();
  49         uc.connect();
  50         String enc2 = URLEncoder.encode(toEncode, "UTF-8");
  51         if (!enc1.equals(enc2))
  52             throw new RuntimeException("test failed");
  53         uc.disconnect();
  54     }
  55 
  56     ServerSocket ss;
  57 
  58     public void run() {
  59         try {
  60             Socket s = ss.accept();
  61             BufferedReader in = new BufferedReader(
  62                 new InputStreamReader(s.getInputStream()));


  63             String req = in.readLine();
  64             PrintStream out = new PrintStream(new BufferedOutputStream(
  65                 s.getOutputStream()));


  66             out.print("HTTP/1.1 403 Forbidden\r\n");
  67             out.print("\r\n");
  68             out.flush();
  69             s.close();
  70             ss.close();
  71         } catch (Exception e) {
  72             e.printStackTrace();
  73         }
  74     }
  75 }


  39         ss = new ServerSocket(0);
  40         (new Thread(this)).start();
  41         String toEncode = "\uD800\uDC00 \uD801\uDC01 ";
  42         String enc1 = URLEncoder.encode(toEncode, "UTF-8");
  43         byte bytes[] = {};
  44         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  45         InputStreamReader reader = new InputStreamReader( bais, "8859_1");
  46         String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) +
  47             "/missing.nothtml";
  48         HttpURLConnection uc =  (HttpURLConnection)new URL(url).openConnection();
  49         uc.connect();
  50         String enc2 = URLEncoder.encode(toEncode, "UTF-8");
  51         if (!enc1.equals(enc2))
  52             throw new RuntimeException("test failed");
  53         uc.disconnect();
  54     }
  55 
  56     ServerSocket ss;
  57 
  58     public void run() {
  59         try (ServerSocket serv = ss;
  60              Socket s = serv.accept();
  61              InputStream is = s.getInputStream();
  62              InputStreamReader isr = new InputStreamReader(is);
  63              BufferedReader in = new BufferedReader(isr))
  64         {
  65             String req = in.readLine();
  66             try (OutputStream os = s.getOutputStream();
  67                  BufferedOutputStream bos = new BufferedOutputStream(os);
  68                  PrintStream out = new PrintStream(bos))
  69             {
  70                 out.print("HTTP/1.1 403 Forbidden\r\n");
  71                 out.print("\r\n");
  72             }


  73         } catch (Exception e) {
  74             e.printStackTrace();
  75         }
  76     }
  77 }