1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.ram;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23
24 import org.apache.commons.vfs.FileName;
25 import org.apache.commons.vfs.FileObject;
26 import org.apache.commons.vfs.FileSystemException;
27 import org.apache.commons.vfs.FileType;
28 import org.apache.commons.vfs.RandomAccessContent;
29 import org.apache.commons.vfs.provider.AbstractFileObject;
30 import org.apache.commons.vfs.util.RandomAccessMode;
31
32 /***
33 * A RAM File contains a single RAM FileData instance, it provides methods to
34 * access the data by implementing FileObject interface.
35 */
36 public class RamFileObject extends AbstractFileObject implements FileObject
37 {
38 /***
39 * File System
40 */
41 RamFileSystem fs;
42
43 /***
44 * RAM File Object Data
45 */
46 private RamFileData data;
47
48 private void save() throws FileSystemException
49 {
50 this.fs.save(this);
51 }
52
53 /***
54 * @param name
55 * @param fs
56 */
57 protected RamFileObject(FileName name, RamFileSystem fs)
58 {
59 super(name, fs);
60 this.fs = fs;
61 this.fs.attach(this);
62 }
63
64
65
66
67
68
69 protected FileType doGetType() throws Exception
70 {
71 return data.getType();
72 }
73
74
75
76
77
78
79 protected String[] doListChildren() throws Exception
80 {
81 return this.fs.listChildren(this.getName());
82 }
83
84
85
86
87
88
89 protected long doGetContentSize() throws Exception
90 {
91 return this.data.getBuffer().length;
92 }
93
94
95
96
97
98
99 protected InputStream doGetInputStream() throws Exception
100 {
101 return new ByteArrayInputStream(this.data.getBuffer());
102 }
103
104
105
106
107
108
109 protected OutputStream doGetOutputStream(boolean bAppend) throws Exception
110 {
111 if (!bAppend)
112 {
113 this.data.setBuffer(new byte[0]);
114 }
115 return new RamFileOutputStream(this);
116 }
117
118
119
120
121
122
123 protected void doDelete() throws Exception
124 {
125 fs.delete(this);
126 }
127
128
129
130
131
132
133 protected long doGetLastModifiedTime() throws Exception
134 {
135 return data.getLastModified();
136 }
137
138
139
140
141
142
143 protected void doSetLastModifiedTime(long modtime) throws Exception
144 {
145 data.setLastModified(modtime);
146 }
147
148
149
150
151
152
153 protected void doCreateFolder() throws Exception
154 {
155 this.injectType(FileType.FOLDER);
156 this.save();
157 }
158
159
160
161
162
163
164 protected void doRename(FileObject newfile) throws Exception
165 {
166 fs.rename(this, (RamFileObject) newfile);
167 }
168
169
170
171
172
173
174 protected RandomAccessContent doGetRandomAccessContent(RandomAccessMode mode)
175 throws Exception
176 {
177 return new RamFileRandomAccessContent(this, mode);
178 }
179
180
181
182
183
184
185 protected void doAttach() throws Exception
186 {
187 this.fs.attach(this);
188 }
189
190 /***
191 * @return Returns the data.
192 */
193 RamFileData getData()
194 {
195 return data;
196 }
197
198 /***
199 * @param data
200 * The data to set.
201 */
202 void setData(RamFileData data)
203 {
204 this.data = data;
205 }
206
207
208
209
210
211
212 protected void injectType(FileType fileType)
213 {
214 this.data.setType(fileType);
215 super.injectType(fileType);
216 }
217
218
219
220
221
222
223 protected void endOutput() throws Exception
224 {
225 super.endOutput();
226 this.save();
227 }
228
229 /***
230 * @return Returns the size of the RAMFileData
231 */
232 int size()
233 {
234 if (data == null)
235 {
236 return 0;
237 }
238 return data.size();
239 }
240
241 /***
242 * @param newSize
243 * @throws IOException
244 * if the new size exceeds the limit
245 */
246 synchronized void resize(int newSize) throws IOException
247 {
248 if (fs.getFileSystemOptions() != null)
249 {
250 int maxSize = RamFileSystemConfigBuilder.getInstance().getMaxSize(
251 fs.getFileSystemOptions());
252 if (fs.size() + newSize - this.size() > maxSize)
253 {
254 throw new IOException("FileSystem capacity (" + maxSize
255 + ") exceeded.");
256 }
257 }
258 this.data.resize(newSize);
259 }
260
261 }