test/java/io/OutputStreamWriter/Encode.java

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

@@ -54,22 +54,24 @@
     }
 
     ServerSocket ss;
 
     public void run() {
-        try {
-            Socket s = ss.accept();
-            BufferedReader in = new BufferedReader(
-                new InputStreamReader(s.getInputStream()));
+        try (ServerSocket serv = ss;
+             Socket s = serv.accept();
+             InputStream is = s.getInputStream();
+             InputStreamReader isr = new InputStreamReader(is);
+             BufferedReader in = new BufferedReader(isr))
+        {
             String req = in.readLine();
-            PrintStream out = new PrintStream(new BufferedOutputStream(
-                s.getOutputStream()));
+            try (OutputStream os = s.getOutputStream();
+                 BufferedOutputStream bos = new BufferedOutputStream(os);
+                 PrintStream out = new PrintStream(bos))
+            {
             out.print("HTTP/1.1 403 Forbidden\r\n");
             out.print("\r\n");
-            out.flush();
-            s.close();
-            ss.close();
+            }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }