KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > io > FileResource


1 package org.jicengine.io;
2
3 import java.io.*;
4
5 /**
6  *
7  * <p>
8  * Copyright (C) 2004 Timo Laitinen
9  * </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 UrlReadable {
17
18     private File file;
19
20     public FileResource(File file)
21     {
22         super(file.getAbsolutePath());
23         this.file = file;
24     }
25
26     public FileResource(String JavaDoc filePath)
27     {
28         this(new File(filePath));
29     }
30
31     public File getFile()
32     {
33         return this.file;
34     }
35
36     public File toFile()
37     {
38         return getFile();
39     }
40
41     public java.net.URL JavaDoc getUrl() throws IOException
42     {
43         try {
44             return getFile().toURL();
45         } catch (java.net.MalformedURLException JavaDoc e){
46             throw new IOException("Failed to transform FileResource to URL: " + e);
47         }
48     }
49
50     public boolean isAvailable()
51     {
52         return getFile().canRead();
53     }
54
55     public InputStream getInputStream() throws java.io.IOException JavaDoc
56     {
57         return new FileInputStream(getFile());
58     }
59
60     public Resource getResource(String JavaDoc relativePath)
61     {
62         File baseFile = getFile();
63         if( !baseFile.isDirectory() ){
64             baseFile = baseFile.getParentFile();
65         }
66         return new FileResource(new File(baseFile, relativePath));
67     }
68 }
69
Popular Tags