1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.vfs2.provider.local;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileType;
28 import org.apache.commons.vfs2.RandomAccessContent;
29 import org.apache.commons.vfs2.provider.AbstractFileName;
30 import org.apache.commons.vfs2.provider.AbstractFileObject;
31 import org.apache.commons.vfs2.provider.UriParser;
32 import org.apache.commons.vfs2.util.FileObjectUtils;
33 import org.apache.commons.vfs2.util.RandomAccessMode;
34
35
36
37
38
39 public class AludraLocalFile extends AbstractFileObject implements FileObject {
40 private final String rootFile;
41
42 private File file;
43
44
45 protected AludraLocalFile(final LocalFileSystem fileSystem, final String rootFile, final AbstractFileName name)
46 throws FileSystemException {
47 super(name, fileSystem);
48 this.rootFile = rootFile;
49 }
50
51
52 protected File getLocalFile() {
53 return file;
54 }
55
56
57 @Override
58 protected void doAttach() throws Exception {
59 if (file == null) {
60
61
62 String fileName = rootFile + getName().getPathDecoded();
63
64 file = new File(fileName);
65 }
66 }
67
68
69 @Override
70 protected FileType doGetType() throws Exception {
71
72
73 if (!file.exists() && file.length() < 1) {
74 return FileType.IMAGINARY;
75 }
76
77 if (file.isDirectory()) {
78 return FileType.FOLDER;
79 }
80
81
82
83
84 return FileType.FILE;
85
86
87
88 }
89
90
91 @Override
92 protected String[] doListChildren() throws Exception {
93 return UriParser.encode(file.list());
94 }
95
96
97 @Override
98 protected void doDelete() throws Exception {
99 if (!file.delete()) {
100 throw new FileSystemException("vfs.provider.local/delete-file.error", file);
101 }
102 }
103
104
105 @Override
106 protected void doRename(final FileObject newfile) throws Exception {
107 AludraLocalFile newLocalFile = (AludraLocalFile) FileObjectUtils.getAbstractFileObject(newfile);
108
109 if (!file.renameTo(newLocalFile.getLocalFile())) {
110 throw new FileSystemException("vfs.provider.local/rename-file.error", new String[] { file.toString(),
111 newfile.toString() });
112 }
113 }
114
115
116 @Override
117 protected void doCreateFolder() throws Exception {
118 if (!file.mkdirs()) {
119
120
121 if (!file.exists()) {
122 throw new FileSystemException("vfs.provider.local/create-folder.error", file);
123 }
124 }
125 }
126
127
128 @Override
129 protected boolean doIsWriteable() throws FileSystemException {
130 return file.canWrite();
131 }
132
133
134 @Override
135 protected boolean doIsHidden() {
136 return file.isHidden();
137 }
138
139
140 @Override
141 protected boolean doIsReadable() throws FileSystemException {
142 return file.canRead();
143 }
144
145
146 @Override
147 protected long doGetLastModifiedTime() throws FileSystemException {
148 return file.lastModified();
149 }
150
151
152
153 @Override
154 protected boolean doSetLastModifiedTime(final long modtime) throws FileSystemException {
155 return file.setLastModified(modtime);
156 }
157
158
159 @Override
160 protected InputStream doGetInputStream() throws Exception {
161 return new FileInputStream(file);
162 }
163
164
165 @Override
166 protected OutputStream doGetOutputStream(boolean bAppend) throws Exception {
167 return new FileOutputStream(file.getPath(), bAppend);
168 }
169
170
171 @Override
172 protected long doGetContentSize() throws Exception {
173 return file.length();
174 }
175
176 @Override
177 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
178 return new LocalFileRandomAccessContent(file, mode);
179 }
180
181 @Override
182 protected boolean doIsSameFile(FileObject destFile) throws FileSystemException {
183 if (!FileObjectUtils.isInstanceOf(destFile, AludraLocalFile.class)) {
184 return false;
185 }
186
187 AludraLocalFile destLocalFile = (AludraLocalFile) FileObjectUtils.getAbstractFileObject(destFile);
188 if (!exists() || !destLocalFile.exists()) {
189 return false;
190 }
191
192 try {
193 return file.getCanonicalPath().equals(destLocalFile.file.getCanonicalPath());
194 }
195 catch (IOException e) {
196 throw new FileSystemException(e);
197 }
198
199 }
200 }