Skip to content
Snippets Groups Projects
Commit 7b9d5cd7 authored by felmer's avatar felmer
Browse files

add new methods to IFile and tests file system implementation

SVN: 2271
parent 0813f522
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......@@ -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();
......
......@@ -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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment