1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.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.lang3.ArrayUtils;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.FileType;
29 import org.apache.commons.vfs2.RandomAccessContent;
30 import org.apache.commons.vfs2.provider.AbstractFileName;
31 import org.apache.commons.vfs2.provider.AbstractFileObject;
32 import org.apache.commons.vfs2.util.FileObjectUtils;
33 import org.apache.commons.vfs2.util.RandomAccessMode;
34
35
36
37
38
39 public class RamFileObject extends AbstractFileObject<RamFileSystem> {
40
41
42
43
44 private RamFileData data;
45
46
47
48
49
50 protected RamFileObject(final AbstractFileName name, final RamFileSystem fs) {
51 super(name, fs);
52 this.getAbstractFileSystem().attach(this);
53 }
54
55 private void save() throws FileSystemException {
56 this.getAbstractFileSystem().save(this);
57 }
58
59
60
61
62
63
64 @Override
65 protected FileType doGetType() throws Exception {
66 return data.getType();
67 }
68
69
70
71
72
73
74 @Override
75 protected String[] doListChildren() throws Exception {
76 return this.getAbstractFileSystem().listChildren(this.getName());
77 }
78
79
80
81
82
83
84 @Override
85 protected long doGetContentSize() throws Exception {
86 return this.size();
87 }
88
89
90
91
92
93
94 @Override
95 protected InputStream doGetInputStream(final int bufferSize) throws Exception {
96
97 if (!getType().hasContent()) {
98 throw new FileSystemException("vfs.provider/read-not-file.error", getName());
99 }
100
101 return new ByteArrayInputStream(this.data.getContent());
102 }
103
104
105
106
107
108
109 @Override
110 protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
111 if (!bAppend) {
112 this.data.setContent(ArrayUtils.EMPTY_BYTE_ARRAY);
113 }
114 return new RamFileOutputStream(this);
115 }
116
117
118
119
120
121
122 @Override
123 protected void doDelete() throws Exception {
124
125 if (this.isContentOpen()) {
126 throw new FileSystemException(this.getName() + " cannot be deleted while the file is openg");
127 }
128 getAbstractFileSystem().delete(this);
129 }
130
131
132
133
134
135
136 @Override
137 protected long doGetLastModifiedTime() throws Exception {
138 return data.getLastModified();
139 }
140
141
142
143
144
145
146
147 @Override
148 protected boolean doSetLastModifiedTime(final long modtime) throws Exception {
149 data.setLastModified(modtime);
150 return true;
151 }
152
153
154
155
156
157
158 @Override
159 protected void doCreateFolder() throws Exception {
160 this.injectType(FileType.FOLDER);
161 this.save();
162 }
163
164
165
166
167
168
169 @Override
170 protected void doRename(final FileObject newFile) throws Exception {
171 final RamFileObject/org/apache/commons/vfs2/provider/ram/RamFileObject.html#RamFileObject">RamFileObject newRamFileObject = (RamFileObject) FileObjectUtils.getAbstractFileObject(newFile);
172 getAbstractFileSystem().rename(this, newRamFileObject);
173 }
174
175
176
177
178
179
180
181 @Override
182 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
183 return new RamFileRandomAccessContent(this, mode);
184 }
185
186
187
188
189
190
191 @Override
192 protected void doAttach() throws Exception {
193 this.getAbstractFileSystem().attach(this);
194 }
195
196
197
198
199 RamFileData getData() {
200 return data;
201 }
202
203
204
205
206 void setData(final RamFileData data) {
207 this.data = data;
208 }
209
210
211
212
213
214
215 @Override
216 protected void injectType(final FileType fileType) {
217 this.data.setType(fileType);
218 super.injectType(fileType);
219 }
220
221
222
223
224
225
226 @Override
227 protected void endOutput() throws Exception {
228 super.endOutput();
229 this.save();
230 }
231
232
233
234
235 int size() {
236 return data == null ? 0 : data.size();
237 }
238
239
240
241
242
243 synchronized void resize(final long newSize) throws IOException {
244 final RamFileSystem afs = getAbstractFileSystem();
245 final FileSystemOptions afsOptions = afs.getFileSystemOptions();
246 if (afsOptions != null) {
247 final long maxSize = RamFileSystemConfigBuilder.getInstance().getLongMaxSize(afsOptions);
248 if (afs.size() + newSize - this.size() > maxSize) {
249 throw new IOException("FileSystem capacity (" + maxSize + ") exceeded.");
250 }
251 }
252 this.data.resize(newSize);
253 }
254
255 }