diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorage.java index 67a3228a6..482cb9293 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorage.java @@ -17,7 +17,7 @@ public interface FileStorage { * {@link ConfigurationProperties} for the file storage base directory. */ String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory"; - + /** * The default implementation will use this key to ask * {@link ConfigurationProperties} for the default URI namespace. @@ -28,7 +28,7 @@ public interface FileStorage { * How often to we insert path separator characters? */ int SHORTY_LENGTH = 3; - + /** * Store the bytes from this stream as a file with the specified ID and * filename. If the file already exists, it is over-written. @@ -50,13 +50,15 @@ public interface FileStorage { String getFilename(String id) throws IOException; /** - * Get the contents of the file with this ID and this filename. + * Get a stream that will provide the contents of the file that was stored + * with this ID and this filename. Close the stream when you're finished + * with it. * * @throws FileNotFoundException * if there is no file that matches this ID and filename. */ - byte[] getFile(String id, String filename) throws FileNotFoundException, - IOException; + InputStream getInputStream(String id, String filename) + throws FileNotFoundException, IOException; /** * If a file exists with this ID, it will be deleted, regardless of the file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImpl.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImpl.java index b9fda48da..a9aec2cdf 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImpl.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImpl.java @@ -4,7 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.utils.filestorage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -64,7 +63,8 @@ public class FileStorageImpl implements FileStorage { * if the configuration property doesn't point to an existing, * writeable directory. */ - FileStorageImpl(File baseDir, Collection namespaces) throws IOException { + FileStorageImpl(File baseDir, Collection namespaces) + throws IOException { checkBaseDirValid(baseDir); checkNamespacesValid(namespaces); @@ -347,12 +347,10 @@ public class FileStorageImpl implements FileStorage { /** * {@inheritDoc} - * - * @throws IOException - * if the file is larger than the maximum allowable size. */ @Override - public byte[] getFile(String id, String filename) throws IOException { + public InputStream getInputStream(String id, String filename) + throws IOException { File file = FileStorageHelper.getFullPath(this.rootDir, id, filename, this.namespacesMap); @@ -362,25 +360,6 @@ public class FileStorageImpl implements FileStorage { + "', file location '" + file + "'"); } - InputStream in = null; - try { - in = new BufferedInputStream(new FileInputStream(file)); - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - int howMany; - while (-1 != (howMany = in.read(buffer))) { - bytes.write(buffer, 0, howMany); - } - bytes.close(); - return bytes.toByteArray(); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } + return new FileInputStream(file); } } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java index 4a9ed54fe..d198779d6 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java @@ -6,6 +6,7 @@ import static edu.cornell.mannlib.vitro.webapp.utils.filestorage.FileStorageFact import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -148,10 +149,11 @@ public class FileStorageFactoryTest extends AbstractTestClass { return "filename"; } - public byte[] getFile(String id, String filename) + public InputStream getInputStream(String id, String filename) throws FileNotFoundException, IOException { - return new byte[0]; + return new ByteArrayInputStream(new byte[0]); } + } /** This class has no zero-argument constructor. */ @@ -172,10 +174,11 @@ public class FileStorageFactoryTest extends AbstractTestClass { return "filename"; } - public byte[] getFile(String id, String filename) + public InputStream getInputStream(String id, String filename) throws FileNotFoundException, IOException { - return new byte[0]; + return new ByteArrayInputStream(new byte[0]); } + } /** This class does not implement the FileStorage interface. */