01: package org.jicengine.io;
02:
03: import java.io.*;
04:
05: /**
06: *
07: * <p>
08: * Copyright (C) 2004 Timo Laitinen
09: * </p>
10: * @author Timo Laitinen
11: * @created 2004-09-20
12: * @since JICE-0.10
13: * @version 1.0
14: */
15:
16: public class FileResource extends AbstractResource implements
17: UrlReadable {
18:
19: private File file;
20:
21: public FileResource(File file) {
22: super (file.getAbsolutePath());
23: this .file = file;
24: }
25:
26: public FileResource(String filePath) {
27: this (new File(filePath));
28: }
29:
30: public File getFile() {
31: return this .file;
32: }
33:
34: public File toFile() {
35: return getFile();
36: }
37:
38: public java.net.URL getUrl() throws IOException {
39: try {
40: return getFile().toURL();
41: } catch (java.net.MalformedURLException e) {
42: throw new IOException(
43: "Failed to transform FileResource to URL: " + e);
44: }
45: }
46:
47: public boolean isAvailable() {
48: return getFile().canRead();
49: }
50:
51: public InputStream getInputStream() throws java.io.IOException {
52: return new FileInputStream(getFile());
53: }
54:
55: public Resource getResource(String relativePath) {
56: File baseFile = getFile();
57: if (!baseFile.isDirectory()) {
58: baseFile = baseFile.getParentFile();
59: }
60: return new FileResource(new File(baseFile, relativePath));
61: }
62: }
|