KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > resource > FileResource


1 /*
2  * $Id: FileResource.java,v 1.3 2004/12/01 07:54:26 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.resource;
15
16 import org.wings.StaticResource;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 /**
24  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
25  * @version $Revision: 1.3 $
26  */

27 public class FileResource extends StaticResource {
28
29     private final File JavaDoc file;
30
31     public FileResource(String JavaDoc name)
32             throws IOException JavaDoc {
33         this(new File JavaDoc(name));
34     }
35
36     public FileResource(File JavaDoc file) {
37         this(file, null, "unknown");
38     }
39
40     public FileResource(File JavaDoc file, String JavaDoc ext, String JavaDoc mt) {
41         super(ext, mt);
42         this.file = file;
43         if (extension == null) {
44             int dotIndex = file.getName().lastIndexOf('.');
45             if (dotIndex > -1) {
46                 extension = file.getName().substring(dotIndex + 1);
47             }
48         }
49         try {
50             size = (int) file.length();
51         } catch (SecurityException JavaDoc ignore) {
52         }
53     }
54
55     public String JavaDoc toString() {
56         return getId() + (file != null ? " " + file.getName() : "");
57     }
58
59     public final File JavaDoc getFile() {
60         return file;
61     }
62
63     protected final InputStream JavaDoc getResourceStream() throws IOException JavaDoc {
64         return new FileInputStream JavaDoc(file);
65     }
66 }
67
Popular Tags