org.snapscript.common.store.StoreReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.common.store;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StoreReader {
private final Store store;
private final int read;
public StoreReader(Store store) {
this(store, 8192);
}
public StoreReader(Store store, int read) {
this.store = store;
this.read = read;
}
public byte[] getBytes(String path) {
InputStream source = store.getInputStream(path);
try {
if(source != null) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(read);
try {
byte[] array = new byte[read];
int count = 0;
while((count = source.read(array)) != -1) {
buffer.write(array, 0, count);
}
return buffer.toByteArray();
} finally {
source.close();
}
}
} catch(Exception e) {
throw new StoreException("Could not read resource '" + path + "'", e);
}
return null;
}
}