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