From 508e8015efec6ed84b5cfc6ad178cc6ee9df65f0 Mon Sep 17 00:00:00 2001 From: Adam Forgacs Date: Sat, 28 Feb 2015 15:30:53 +0100 Subject: [PATCH] Using try-with-resources for some minor cleanup. --- .../main/java/io/undertow/util/FileUtils.java | 40 ++++--------------- 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/io/undertow/util/FileUtils.java b/core/src/main/java/io/undertow/util/FileUtils.java index f183a81d81..71e93f04f1 100644 --- a/core/src/main/java/io/undertow/util/FileUtils.java +++ b/core/src/main/java/io/undertow/util/FileUtils.java @@ -20,7 +20,6 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -61,26 +60,16 @@ public static String readFile(final File file) { } public static String readFile(InputStream file) { - BufferedInputStream stream = null; - try { - stream = new BufferedInputStream(file); + try (BufferedInputStream stream = new BufferedInputStream(file)) { byte[] buff = new byte[1024]; StringBuilder builder = new StringBuilder(); - int read = -1; + int read; while ((read = stream.read(buff)) != -1) { builder.append(new String(buff, 0, read)); } return builder.toString(); } catch (IOException e) { throw new RuntimeException(e); - } finally { - if (stream != null) { - try { - stream.close(); - } catch (IOException e) { - //ignore - } - } } } @@ -112,33 +101,18 @@ public static File getFileOrCheckParentsIfNotFound(String baseStr, String path) public static void copyFile(final File src, final File dest) throws IOException { - final InputStream in = new BufferedInputStream(new FileInputStream(src)); - try { + try (InputStream in = new BufferedInputStream(new FileInputStream(src))) { copyFile(in, dest); - } finally { - close(in); } } public static void copyFile(final InputStream in, final File dest) throws IOException { dest.getParentFile().mkdirs(); - final OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); - try { - int i = in.read(); - while (i != -1) { - out.write(i); - i = in.read(); + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) { + int read; + while ((read = in.read()) != -1) { + out.write(read); } - } finally { - close(out); - } - } - - - public static void close(Closeable closeable) { - try { - closeable.close(); - } catch (IOException ignore) { } }