NIHVIVO-160 FileStorage.getInputStream returns an InputStream, instead of an array of bytes. Read it however you like.

This commit is contained in:
jeb228 2010-05-24 19:27:27 +00:00
parent b86ec9e7b2
commit f40e2d1af7
3 changed files with 19 additions and 35 deletions

View file

@ -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

View file

@ -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<String> namespaces) throws IOException {
FileStorageImpl(File baseDir, Collection<String> 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);
}
}

View file

@ -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. */