You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/10 14:32:53 UTC

[commons-vfs] branch master updated: Fix compiler warning and use try-with-resources with input stream.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new ed4cf7c  Fix compiler warning and use try-with-resources with input stream.
ed4cf7c is described below

commit ed4cf7c2eec30e04db38935d45d200185ff70862
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri May 10 10:32:49 2019 -0400

    Fix compiler warning and use try-with-resources with input stream.
---
 .../java/org/apache/commons/vfs2/FileUtil.java     | 26 +++++++++-------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileUtil.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileUtil.java
index d0aa50c..805d142 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileUtil.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileUtil.java
@@ -36,27 +36,23 @@ public final class FileUtil {
      * @throws IOException if the file content cannot be accessed.
      */
     public static byte[] getContent(final FileObject file) throws IOException {
-        final FileContent content = file.getContent();
-        final int size = (int) content.getSize();
-        final byte[] buf = new byte[size];
-
-        final InputStream in = content.getInputStream();
-        try {
-            int read = 0;
-            for (int pos = 0; pos < size && read >= 0; pos += read) {
-                read = in.read(buf, pos, size - pos);
+        try (final FileContent content = file.getContent()) {
+            final int size = (int) content.getSize();
+            final byte[] buf = new byte[size];
+            try (final InputStream in = content.getInputStream();) {
+                int read = 0;
+                for (int pos = 0; pos < size && read >= 0; pos += read) {
+                    read = in.read(buf, pos, size - pos);
+                }
             }
-        } finally {
-            in.close();
+            return buf;
         }
-
-        return buf;
     }
 
     /**
      * Writes the content of a file to an OutputStream.
      *
-     * @param file The FileObject to write.
+     * @param file   The FileObject to write.
      * @param output The OutputStream to write to.
      * @throws IOException if an error occurs writing the file.
      * @see FileContent#write(OutputStream)
@@ -68,7 +64,7 @@ public final class FileUtil {
     /**
      * Copies the content from a source file to a destination file.
      *
-     * @param srcFile The source FileObject.
+     * @param srcFile  The source FileObject.
      * @param destFile The target FileObject
      * @throws IOException If an error occurs copying the file.
      * @see FileContent#write(FileContent)