]> source.dussan.org Git - vaadin-framework.git/commitdiff
Convert to try-with-resource
authorPer-Åke Minborg <minborg@speedment.com>
Fri, 28 Oct 2016 06:25:02 +0000 (23:25 -0700)
committerHenri Sara <hesara@vaadin.com>
Wed, 2 Nov 2016 14:39:10 +0000 (14:39 +0000)
And also replace "utf-8" with StandardCharsets.UTF_8

Change-Id: I80b73b653e97904605dc62484a7448f3bfbf7215

server/src/main/java/com/vaadin/server/BootstrapHandler.java
server/src/main/java/com/vaadin/server/UnsupportedBrowserHandler.java
server/src/main/java/com/vaadin/server/VaadinService.java
server/src/main/java/com/vaadin/server/VaadinServlet.java
server/src/main/java/com/vaadin/server/communication/FileUploadHandler.java
server/src/main/java/com/vaadin/server/communication/PortletDummyRequestHandler.java
server/src/main/java/com/vaadin/server/communication/ResourceWriter.java
server/src/main/java/com/vaadin/server/communication/UIInitHandler.java
server/src/main/java/com/vaadin/server/widgetsetutils/WidgetSetBuilder.java
server/src/test/java/com/vaadin/data/Jsr303Test.java
server/src/test/java/com/vaadin/server/VaadinSessionTest.java

index fd30d2891d23aadc43d356a71c275cef30ff7932..3cb1dc9097f324c3b2b077097e39ecc5e3eacb97 100644 (file)
@@ -353,10 +353,10 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler {
     private void writeBootstrapPage(VaadinResponse response, String html)
             throws IOException {
         response.setContentType("text/html");
-        BufferedWriter writer = new BufferedWriter(
-                new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
-        writer.append(html);
-        writer.close();
+        try (BufferedWriter writer = new BufferedWriter(
+            new OutputStreamWriter(response.getOutputStream(), "UTF-8"))) {
+            writer.append(html);
+        }
     }
 
     private void setupStandaloneDocument(BootstrapContext context,
index 2165830101690c55768bebad82da4c8995a6b1c9..fa1f48b7909f5372736759a776126d8aa653b3bf 100644 (file)
@@ -61,34 +61,33 @@ public class UnsupportedBrowserHandler extends SynchronizedRequestHandler {
      */
     protected void writeBrowserTooOldPage(VaadinRequest request,
             VaadinResponse response) throws IOException {
-        Writer page = response.getWriter();
-        WebBrowser b = VaadinSession.getCurrent().getBrowser();
-
-        page.write(
+        try (Writer page = response.getWriter()) {
+            WebBrowser b = VaadinSession.getCurrent().getBrowser();
+            
+            page.write(
                 "<html><body><h1>I'm sorry, but your browser is not supported</h1>"
-                        + "<p>The version (" + b.getBrowserMajorVersion() + "."
-                        + b.getBrowserMinorVersion()
-                        + ") of the browser you are using "
+                    + "<p>The version (" + b.getBrowserMajorVersion() + "."
+                    + b.getBrowserMinorVersion()
+                    + ") of the browser you are using "
                         + " is outdated and not supported.</p>"
                         + "<p>You should <b>consider upgrading</b> to a more up-to-date browser.</p> "
                         + "<p>The most popular browsers are <b>"
                         + " <a href=\"https://www.google.com/chrome\">Chrome</a>,"
                         + " <a href=\"http://www.mozilla.com/firefox\">Firefox</a>,"
-                        + (b.isWindows()
-                                ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
-                                : "")
-                        + " <a href=\"http://www.opera.com/browser\">Opera</a>"
+                    + (b.isWindows()
+                        ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
+                        : "")
+                    + " <a href=\"http://www.opera.com/browser\">Opera</a>"
                         + " and <a href=\"http://www.apple.com/safari\">Safari</a>.</b><br/>"
                         + "Upgrading to the latest version of one of these <b>will make the web safer, faster and better looking.</b></p>"
-                        + (b.isIE()
-                                ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
-                                        + "<p>If you can not upgrade your browser, please consider trying <a onclick=\"CFInstall.check({mode:'overlay'});return false;\" href=\"http://www.google.com/chromeframe\">Chrome Frame</a>.</p>"
-                                : "") //
-                        + "<p><sub><a onclick=\"document.cookie='"
-                        + FORCE_LOAD_COOKIE
-                        + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
+                    + (b.isIE()
+                        ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
+                            + "<p>If you can not upgrade your browser, please consider trying <a onclick=\"CFInstall.check({mode:'overlay'});return false;\" href=\"http://www.google.com/chromeframe\">Chrome Frame</a>.</p>"
+                        : "") //
+                    + "<p><sub><a onclick=\"document.cookie='"
+                    + FORCE_LOAD_COOKIE
+                    + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
                         + "</body>\n" + "</html>");
-
-        page.close();
+        }
     }
 }
index 922c7cfe52e79350e78089416ed07ac80c4b0a79..3899965622c577f967eb400c92633f5f1fbf4831 100644 (file)
@@ -1499,10 +1499,10 @@ public abstract class VaadinService implements Serializable {
         response.setContentType(contentType);
 
         final OutputStream out = response.getOutputStream();
-        final PrintWriter outWriter = new PrintWriter(
-                new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
-        outWriter.print(reponseString);
-        outWriter.close();
+        try (PrintWriter outWriter = new PrintWriter(
+            new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+            outWriter.print(reponseString);
+        }
     }
 
     /**
index d43db5c303492e56322640d7e84e4cbcda093373..13713dcc54b24d1e140bd0c5fc74c689bfc099f1 100644 (file)
@@ -605,12 +605,12 @@ public class VaadinServlet extends HttpServlet implements Constants {
             String output) throws IOException {
         response.setContentType(contentType);
         final OutputStream out = response.getOutputStream();
-        // Set the response type
-        final PrintWriter outWriter = new PrintWriter(
-                new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
-        outWriter.print(output);
-        outWriter.flush();
-        outWriter.close();
+        try ( // Set the response type
+            PrintWriter outWriter = new PrintWriter(
+                new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+            outWriter.print(output);
+            outWriter.flush();
+        }
     }
 
     /**
@@ -1405,8 +1405,7 @@ public class VaadinServlet extends HttpServlet implements Constants {
 
     private static String readFile(File file, Charset charset)
             throws IOException {
-        InputStream in = new FileInputStream(file);
-        try {
+        try (InputStream in = new FileInputStream(file)) {
             // no point in reading files over 2GB to a String
             byte[] b = new byte[(int) file.length()];
             int len = b.length;
@@ -1420,18 +1419,13 @@ public class VaadinServlet extends HttpServlet implements Constants {
                 total += result;
             }
             return new String(b, charset);
-        } finally {
-            in.close();
         }
     }
 
     private static void writeFile(String content, File file, Charset charset)
             throws IOException {
-        FileOutputStream fos = new FileOutputStream(file);
-        try {
+        try (FileOutputStream fos = new FileOutputStream(file)) {
             fos.write(content.getBytes(charset));
-        } finally {
-            fos.close();
         }
     }
 
index 19c3d45fa9444773e046bbd0166a9891c82e2d57..d5c047b0eb0bc45b871da6ee2d105cb4ce78fe3c 100644 (file)
@@ -676,12 +676,12 @@ public class FileUploadHandler implements RequestHandler {
     protected void sendUploadResponse(VaadinRequest request,
             VaadinResponse response) throws IOException {
         response.setContentType("text/html");
-        final OutputStream out = response.getOutputStream();
-        final PrintWriter outWriter = new PrintWriter(
+        try (OutputStream out = response.getOutputStream()) {
+            final PrintWriter outWriter = new PrintWriter(
                 new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
-        outWriter.print("<html><body>download handled</body></html>");
-        outWriter.flush();
-        out.close();
+            outWriter.print("<html><body>download handled</body></html>");
+            outWriter.flush();
+        }
     }
 
     private void cleanStreamVariable(VaadinSession session, final UI ui,
index 04d0e8b66930281f77afe8ba599b5abd5aa37e24..7540fcb1907546550fc33617b14f8c89c65ae222 100644 (file)
@@ -60,10 +60,10 @@ public class PortletDummyRequestHandler implements RequestHandler {
 
         final OutputStream out = ((ResourceResponse) response)
                 .getPortletOutputStream();
-        final PrintWriter outWriter = new PrintWriter(
-                new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
-        outWriter.print("<html><body>dummy page</body></html>");
-        outWriter.close();
+        try (PrintWriter outWriter = new PrintWriter(
+            new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
+            outWriter.print("<html><body>dummy page</body></html>");
+        }
 
         return true;
     }
index 2ca4b296bb8a0a5223c104f12ba359b666324bf0..be83bf7185dd471e0dfed13292be8573308715dd 100644 (file)
@@ -29,6 +29,7 @@ import com.vaadin.server.JsonPaintTarget;
 import com.vaadin.server.LegacyCommunicationManager;
 import com.vaadin.ui.CustomLayout;
 import com.vaadin.ui.UI;
+import java.nio.charset.StandardCharsets;
 
 /**
  * Serializes resources to JSON. Currently only used for {@link CustomLayout}
@@ -82,15 +83,13 @@ public class ResourceWriter implements Serializable {
                         + "\" : ");
                 final StringBuffer layout = new StringBuffer();
 
-                try {
-                    final InputStreamReader r = new InputStreamReader(is,
-                            "UTF-8");
+                try (InputStreamReader r = new InputStreamReader(is,
+                    StandardCharsets.UTF_8)) {
                     final char[] buffer = new char[20000];
                     int charsRead = 0;
                     while ((charsRead = r.read(buffer)) > 0) {
                         layout.append(buffer, 0, charsRead);
                     }
-                    r.close();
                 } catch (final java.io.IOException e) {
                     // FIXME: Handle exception
                     getLogger().log(Level.INFO, "Resource transfer failed", e);
index 0050872aaa598d915affd7f2c180aadcf54c6a74..09d2b0aa790f5daa342290a26bed47736cc44246 100644 (file)
@@ -273,8 +273,7 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler {
      */
     protected String getInitialUidl(VaadinRequest request, UI uI)
             throws IOException {
-        StringWriter writer = new StringWriter();
-        try {
+        try (StringWriter writer = new StringWriter()) {
             writer.write("{");
 
             VaadinSession session = uI.getSession();
@@ -287,8 +286,6 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler {
             String initialUIDL = writer.toString();
             getLogger().log(Level.FINE, "Initial UIDL:" + initialUIDL);
             return initialUIDL;
-        } finally {
-            writer.close();
         }
     }
 
index 951944d8ace9861adee6105543e0bf52a4d66fa0..689ac21cd7ebe34b2ef87f8b7c2835145cf55b0f 100644 (file)
@@ -84,12 +84,12 @@ public class WidgetSetBuilder {
                 }
             }
             widgetsetFile.createNewFile();
-            PrintStream printStream = new PrintStream(
-                    new FileOutputStream(widgetsetFile));
-            printStream.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            try (PrintStream printStream = new PrintStream(
+                new FileOutputStream(widgetsetFile))) {
+                printStream.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                     + "<!DOCTYPE module PUBLIC \"-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN\" \"http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd\">\n");
-            printStream.print("<module>\n");
-            printStream.print("    <!--\n"
+                printStream.print("<module>\n");
+                printStream.print("    <!--\n"
                     + "     Uncomment the following to compile the widgetset for one browser only.\n\n"
                     + "     Multiple browsers can be specified as a comma separated list. The\n"
                     + "     supported user agents at the moment of writing were:\n"
@@ -103,8 +103,8 @@ public class WidgetSetBuilder {
                     + "    See https://vaadin.com/wiki/-/wiki/Main/Using%20SuperDevMode for more\n"
                     + "    information and instructions.\n" + "    -->\n"
                     + "    <!-- <set-configuration-property name=\"devModeRedirectEnabled\" value=\"true\" /> -->\n\n");
-            printStream.print("\n</module>\n");
-            printStream.close();
+                printStream.print("\n</module>\n");
+            }
             changed = true;
         }
 
@@ -155,10 +155,10 @@ public class WidgetSetBuilder {
 
     private static void commitChanges(File widgetsetFile, String content)
             throws IOException {
-        BufferedWriter bufferedWriter = new BufferedWriter(
-                new OutputStreamWriter(new FileOutputStream(widgetsetFile)));
-        bufferedWriter.write(content);
-        bufferedWriter.close();
+        try (BufferedWriter bufferedWriter = new BufferedWriter(
+            new OutputStreamWriter(new FileOutputStream(widgetsetFile)))) {
+            bufferedWriter.write(content);
+        }
     }
 
     private static String addWidgetSet(String ws, String content) {
@@ -187,15 +187,16 @@ public class WidgetSetBuilder {
     }
 
     private static String readFile(File widgetsetFile) throws IOException {
-        Reader fi = new FileReader(widgetsetFile);
-        BufferedReader bufferedReader = new BufferedReader(fi);
-        StringBuilder sb = new StringBuilder();
-        String line;
-        while ((line = bufferedReader.readLine()) != null) {
-            sb.append(line);
-            sb.append("\n");
+        StringBuilder sb;
+        try (Reader fi = new FileReader(widgetsetFile)) {
+            BufferedReader bufferedReader = new BufferedReader(fi);
+            sb = new StringBuilder();
+            String line;
+            while ((line = bufferedReader.readLine()) != null) {
+                sb.append(line);
+                sb.append("\n");
+            }
         }
-        fi.close();
         return sb.toString();
     }
 
index 74e97432ba8a9ed171a68f4d1b3d37f2ada347c2..da30f56743c50811872f8ab4a9e3e08ab0fea730 100644 (file)
@@ -108,11 +108,11 @@ public class Jsr303Test {
             NoSuchMethodException, SecurityException, InstantiationException,
             IllegalAccessException, IllegalArgumentException,
             InvocationTargetException, IOException, InterruptedException {
-        URLClassLoader loader = new TestClassLoader();
-        Class<?> clazz = loader.loadClass(Jsr303UnitTest.class.getName());
-        UnitTest test = (UnitTest) clazz.newInstance();
-        test.execute();
-        loader.close();
+        try (URLClassLoader loader = new TestClassLoader()) {
+            Class<?> clazz = loader.loadClass(Jsr303UnitTest.class.getName());
+            UnitTest test = (UnitTest) clazz.newInstance();
+            test.execute();
+        }
     }
 
 }
index 466f89df0727dbd40a2c3b7093e28e0cf31cc241..58fb0658c5eec9ac331191466383aea1417d8daf 100644 (file)
@@ -286,9 +286,9 @@ public class VaadinSessionTest implements Serializable {
         int uiId = ui.getUIId();
 
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        ObjectOutputStream out = new ObjectOutputStream(bos);
-        out.writeObject(session);
-        out.close();
+        try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
+            out.writeObject(session);
+        }
 
         session.unlock();