From 7b9d5cd7c0d9fd3cce059fc97f19c09d33f4f189 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 29 Oct 2007 09:32:18 +0000
Subject: [PATCH] add new methods to IFile and tests file system implementation

SVN: 2271
---
 .../ch/systemsx/cisd/bds/storage/IFile.java   | 13 ++++++
 .../cisd/bds/storage/filesystem/File.java     | 29 ++++++++++++-
 .../cisd/bds/storage/filesystem/FileTest.java | 41 +++++++++++++++++++
 3 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
index b063d035894..9b8702c860f 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
@@ -16,6 +16,9 @@
 
 package ch.systemsx.cisd.bds.storage;
 
+import java.io.InputStream;
+import java.io.Reader;
+
 /**
  * Node representing a file with some content.
  *
@@ -28,8 +31,18 @@ public interface IFile extends INode
      */
     public byte[] getBinaryContent();
     
+    /**
+     * Returns the content of this file node as an input stream.
+     */
+    public InputStream getInputStream();
+    
     /**
      * Returns the content of this file node as a string.
      */
     public String getStringContent();
+    
+    /**
+     * Returns the content of this file node as a reader.
+     */
+    public Reader getReader();
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/File.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/File.java
index a759313a6d5..25fbe305894 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/File.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/File.java
@@ -17,7 +17,11 @@
 package ch.systemsx.cisd.bds.storage.filesystem;
 
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -39,10 +43,9 @@ class File extends AbstractNode implements IFile
 
     public byte[] getBinaryContent()
     {
-        FileInputStream inputStream = null;
+        InputStream inputStream = getInputStream();
         try
         {
-            inputStream = new FileInputStream(nodeFile);
             return IOUtils.toByteArray(inputStream);
         } catch (IOException ex)
         {
@@ -53,11 +56,33 @@ class File extends AbstractNode implements IFile
         }
     }
 
+    public InputStream getInputStream()
+    {
+        try
+        {
+            return new FileInputStream(nodeFile);
+        } catch (FileNotFoundException ex)
+        {
+            throw new EnvironmentFailureException("Couldn't open input stream for file " + nodeFile.getAbsolutePath());
+        }
+    }
+
     public String getStringContent()
     {
         return FileUtilities.loadToString(nodeFile);
     }
 
+    public Reader getReader()
+    {
+        try
+        {
+            return new FileReader(nodeFile);
+        } catch (FileNotFoundException ex)
+        {
+            throw new EnvironmentFailureException("Couldn't open reader for file " + nodeFile.getAbsolutePath());
+        }
+    }
+    
     public final void extractTo(final java.io.File directory) throws EnvironmentFailureException
     {
         assert directory != null && directory.isDirectory();
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java
index 936b8c70a6c..363c64788ca 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileTest.java
@@ -18,6 +18,11 @@ package ch.systemsx.cisd.bds.storage.filesystem;
 
 import static org.testng.AssertJUnit.assertEquals;
 
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.IOUtils;
 import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.common.utilities.FileUtilities;
@@ -52,4 +57,40 @@ public class FileTest extends StorageTestCase
         assertEquals("Hello\nworld!\n", FileUtilities.loadToString(new java.io.File(subdir, stringFile.getName())));
     }
 
+    @Test
+    public void testGetInputStream() throws Exception
+    {
+        java.io.File file = new java.io.File(TEST_DIR, "test");
+        FileOutputStream fileOutputStream = null;
+        try
+        {
+            fileOutputStream = new FileOutputStream(file);
+            fileOutputStream.write(new byte[] {1, 2, 3, 4});
+        } catch (IOException ex)
+        {
+            throw ex;
+        } finally
+        {
+            IOUtils.closeQuietly(fileOutputStream);
+        }
+        
+        File binaryFile = new File(file);
+        InputStream inputStream = binaryFile.getInputStream();
+        try
+        {
+            byte[] bytes = new byte[5];
+            inputStream.read(bytes);
+            assertEquals(1, bytes[0]);
+            assertEquals(2, bytes[1]);
+            assertEquals(3, bytes[2]);
+            assertEquals(4, bytes[3]);
+            assertEquals(0, bytes[4]);
+        } catch (IOException ex)
+        {
+            throw ex;
+        } finally
+        {
+            IOUtils.closeQuietly(inputStream);
+        }
+    }
 }
-- 
GitLab