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 15:54:24 UTC

[commons-vfs] branch master updated: [VFS-715] Add org.apache.commons.vfs2.FileContent.getByteArray().

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 ce498a1  [VFS-715] Add org.apache.commons.vfs2.FileContent.getByteArray().
ce498a1 is described below

commit ce498a1ca443de814c881632fc2da95648f11a7e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri May 10 11:54:21 2019 -0400

    [VFS-715] Add org.apache.commons.vfs2.FileContent.getByteArray().
---
 .../java/org/apache/commons/vfs2/FileContent.java  | 24 +++++++++++++++++
 .../java/org/apache/commons/vfs2/FileUtil.java     | 10 +-------
 .../org/apache/commons/vfs2/test/ContentTests.java | 30 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 +++
 4 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
index 6226b95..dd4a59f 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileContent.java
@@ -115,6 +115,30 @@ public interface FileContent extends Closeable {
     InputStream getInputStream() throws FileSystemException;
 
     /**
+     * Returns the content of a file, as a byte array.
+     *
+     * @param file The file to get the content of.
+     * @return The content as a byte array.
+     * @throws IOException if the file content cannot be accessed.
+     * @since 2.4
+     */
+    default byte[] getByteArray() throws IOException {
+        final long sizeL = getSize();
+        if (sizeL > Integer.MAX_VALUE) {
+            throw new IllegalStateException(String.format("File content is too large for a byte array: %,d", sizeL));
+        }
+        final int size = (int) sizeL;
+        final byte[] buf = new byte[size];
+        try (final InputStream in = getInputStream(size)) {
+            int read = 0;
+            for (int pos = 0; pos < size && read >= 0; pos += read) {
+                read = in.read(buf, pos, size - pos);
+            }
+        }
+        return buf;
+    }
+    
+    /**
      * Returns an input stream for reading the file's content.
      * <p>
      * There may only be a single input or output stream open for the file at any time.
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 805d142..9e6e383 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
@@ -37,15 +37,7 @@ public final class FileUtil {
      */
     public static byte[] getContent(final FileObject file) throws IOException {
         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);
-                }
-            }
-            return buf;
+            return content.getByteArray();
         }
     }
 
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/ContentTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/ContentTests.java
index ea5738d..e17d4fb 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/ContentTests.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/ContentTests.java
@@ -16,8 +16,10 @@
  */
 package org.apache.commons.vfs2.test;
 
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.vfs2.FileContent;
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSystem;
@@ -332,4 +334,32 @@ public class ContentTests extends AbstractProviderTestCase {
         // Check
         assertTrue(instr1.read() == -1);
     }
+
+    /**
+     * Tests that input streams are cleaned up on file close.
+     */
+    public void testInputStreamReadAll() throws Exception {
+        // Get the test file
+        try (final FileObject file = getReadFolder().resolveFile("file1.txt")) {
+            assertEquals(FileType.FILE, file.getType());
+            assertTrue(file.isFile());
+
+            final ByteArrayOutputStream output = new ByteArrayOutputStream();
+            file.getContent().write(output);
+            assertEquals(FILE1_CONTENT, new String(output.toByteArray()));
+        }
+    }
+
+    /**
+     * Tests that input streams are cleaned up on file close.
+     */
+    public void testByteArrayReadAll() throws Exception {
+        // Get the test file
+        try (final FileObject file = getReadFolder().resolveFile("file1.txt")) {
+            assertEquals(FileType.FILE, file.getType());
+            assertTrue(file.isFile());
+
+            assertEquals(FILE1_CONTENT, new String(file.getContent().getByteArray()));
+        }
+    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f1d80a5..7918c05 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -116,6 +116,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="VFS-713" dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileObjectUtils.readProperties(FileObject) method to read a .properties file.
       </action>
+      <action issue="VFS-715" dev="ggregory" type="add" due-to="Gary Gregory">
+        Add org.apache.commons.vfs2.FileContent.getByteArray().
+      </action>
     </release>
     <release version="2.3" date="2019-02-01" description="New features and bug fix release.">
       <action issue="VFS-645" dev="ggregory" type="fix">