KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > local > LocalFile


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.local;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.FileType;
22 import org.apache.commons.vfs.RandomAccessContent;
23 import org.apache.commons.vfs.provider.AbstractFileObject;
24 import org.apache.commons.vfs.provider.UriParser;
25 import org.apache.commons.vfs.util.RandomAccessMode;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32
33 /**
34  * A file object implementation which uses direct file access.
35  *
36  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
37  * @author Gary D. Gregory
38  */

39 public class LocalFile
40     extends AbstractFileObject
41     implements FileObject
42 {
43     private File JavaDoc file;
44     private final String JavaDoc fileName;
45
46     /**
47      * Creates a non-root file.
48      */

49     protected LocalFile(final LocalFileSystem fileSystem,
50                         final String JavaDoc fileName,
51                         final FileName name) throws FileSystemException
52     {
53         super(name, fileSystem);
54         this.fileName = UriParser.decode(fileName);
55     }
56
57     /**
58      * Returns the local file that this file object represents.
59      */

60     protected File JavaDoc getLocalFile()
61     {
62         return file;
63     }
64
65     /**
66      * Attaches this file object to its file resource.
67      */

68     protected void doAttach()
69         throws Exception JavaDoc
70     {
71         if (file == null)
72         {
73             file = new File JavaDoc(fileName);
74         }
75     }
76
77     /**
78      * Returns the file's type.
79      */

80     protected FileType doGetType()
81         throws Exception JavaDoc
82     {
83         if (!file.exists())
84         {
85             return FileType.IMAGINARY;
86         }
87         else if (file.isDirectory())
88         {
89             return FileType.FOLDER;
90         }
91         else if (file.isFile())
92         {
93             return FileType.FILE;
94         }
95
96         throw new FileSystemException("vfs.provider.local/get-type.error", file);
97     }
98
99     /**
100      * Returns the children of the file.
101      */

102     protected String JavaDoc[] doListChildren()
103         throws Exception JavaDoc
104     {
105         return UriParser.encode(file.list());
106     }
107
108     /**
109      * Deletes this file, and all children.
110      */

111     protected void doDelete()
112         throws Exception JavaDoc
113     {
114         if (!file.delete())
115         {
116             throw new FileSystemException("vfs.provider.local/delete-file.error", file);
117         }
118     }
119
120     /**
121      * rename this file
122      */

123     protected void doRename(FileObject newfile) throws Exception JavaDoc
124     {
125         if (!file.renameTo(((LocalFile) newfile).getLocalFile()))
126         {
127             throw new FileSystemException("vfs.provider.local/rename-file.error",
128                 new String JavaDoc[]{file.toString(), newfile.toString()});
129         }
130     }
131
132     /**
133      * Creates this folder.
134      */

135     protected void doCreateFolder()
136         throws Exception JavaDoc
137     {
138         if (!file.mkdirs())
139         {
140             throw new FileSystemException("vfs.provider.local/create-folder.error", file);
141         }
142     }
143
144     /**
145      * Determines if this file can be written to.
146      */

147     protected boolean doIsWriteable() throws FileSystemException
148     {
149         return file.canWrite();
150     }
151
152     /**
153      * Determines if this file is hidden.
154      */

155     protected boolean doIsHidden()
156     {
157         return file.isHidden();
158     }
159
160     /**
161      * Determines if this file can be read.
162      */

163     protected boolean doIsReadable() throws FileSystemException
164     {
165         return file.canRead();
166     }
167
168     /**
169      * Gets the last modified time of this file.
170      */

171     protected long doGetLastModifiedTime() throws FileSystemException
172     {
173         return file.lastModified();
174     }
175
176     /**
177      * Sets the last modified time of this file.
178      */

179     protected void doSetLastModifiedTime(final long modtime)
180         throws FileSystemException
181     {
182         file.setLastModified(modtime);
183     }
184
185     /**
186      * Creates an input stream to read the content from.
187      */

188     protected InputStream JavaDoc doGetInputStream()
189         throws Exception JavaDoc
190     {
191         return new FileInputStream JavaDoc(file);
192     }
193
194     /**
195      * Creates an output stream to write the file content to.
196      */

197     protected OutputStream JavaDoc doGetOutputStream(boolean bAppend)
198         throws Exception JavaDoc
199     {
200         return new FileOutputStream JavaDoc(file.getPath(), bAppend);
201     }
202
203     /**
204      * Returns the size of the file content (in bytes).
205      */

206     protected long doGetContentSize()
207         throws Exception JavaDoc
208     {
209         return file.length();
210     }
211
212     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception JavaDoc
213     {
214         return new LocalFileRandomAccessContent(file, mode);
215     }
216 }
217
Popular Tags