01: package ri.cache.maps;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07:
08: public class FileLoaderMap extends AbstractLoaderMap {
09:
10: private final File rootDirectory;
11:
12: public FileLoaderMap(String directory) {
13: this (new File(directory));
14: }
15:
16: public FileLoaderMap(File directory) {
17: rootDirectory = directory;
18: }
19:
20: public Object get(Object key) {
21: File f = new File(rootDirectory, (String) key);
22: try {
23: long length = f.length();
24: if (length > Integer.MAX_VALUE) {
25: throw new RuntimeException(
26: "FileLoader: file is too long");
27: }
28: InputStream is = new FileInputStream(f);
29: try {
30: byte[] bytes = new byte[(int) length];
31: // REVIEW adam@bea.com 23-Jun-04 - There is no guarantee that a
32: // single call to read will indeed read the entire file.
33: is.read(bytes);
34: return bytes;
35: } finally {
36: is.close();
37: }
38: } catch (IOException e) {
39: throw new RuntimeException("IOException reading file "
40: + f.getAbsolutePath(), e);
41: }
42: }
43:
44: }
|