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.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.vfs2.FileName;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileType;
28
29
30
31
32 class RamFileData implements Serializable {
33
34
35
36
37 private static final long serialVersionUID = 20101208L;
38
39
40
41
42 private FileName name;
43
44
45
46
47 private FileType type;
48
49
50
51
52 private byte[] content;
53
54
55
56
57 private long lastModifiedMillis;
58
59
60
61
62 private final Collection<RamFileData> children;
63
64
65
66
67
68
69 public RamFileData(final FileName name) {
70 this.children = Collections.synchronizedCollection(new ArrayList<>());
71 this.clear();
72 if (name == null) {
73 throw new IllegalArgumentException("name can not be null");
74 }
75 this.name = name;
76 }
77
78
79
80
81 byte[] getContent() {
82 return content;
83 }
84
85
86
87
88 void setContent(final byte[] content) {
89 updateLastModified();
90 this.content = content;
91 }
92
93
94
95
96 long getLastModified() {
97 return lastModifiedMillis;
98 }
99
100
101
102
103 void setLastModified(final long lastModified) {
104 this.lastModifiedMillis = lastModified;
105 }
106
107
108
109
110 FileType getType() {
111 return type;
112 }
113
114
115
116
117 void setType(final FileType type) {
118 this.type = type;
119 }
120
121
122
123 void clear() {
124 this.content = ArrayUtils.EMPTY_BYTE_ARRAY;
125 updateLastModified();
126 this.type = FileType.IMAGINARY;
127 this.children.clear();
128 this.name = null;
129 }
130
131 void updateLastModified() {
132 this.lastModifiedMillis = System.currentTimeMillis();
133 }
134
135
136
137
138 FileName getName() {
139 return name;
140 }
141
142
143
144
145
146
147 @Override
148 public String toString() {
149 return this.name.toString();
150 }
151
152
153
154
155
156
157
158 void addChild(final RamFileData data) throws FileSystemException {
159 if (!this.getType().hasChildren()) {
160 throw new FileSystemException("A child can only be added in a folder");
161 }
162
163 FileSystemException.requireNonNull(data, "No child can be null");
164
165 if (this.children.contains(data)) {
166 throw new FileSystemException("Child already exists. " + data);
167 }
168
169 this.children.add(data);
170 updateLastModified();
171 }
172
173
174
175
176
177
178
179 void removeChild(final RamFileData data) throws FileSystemException {
180 if (!this.getType().hasChildren()) {
181 throw new FileSystemException("A child can only be removed from a folder");
182 }
183 if (!this.children.contains(data)) {
184 throw new FileSystemException("Child not found. " + data);
185 }
186 this.children.remove(data);
187 updateLastModified();
188 }
189
190
191
192
193 Collection<RamFileData> getChildren() {
194 if (name == null) {
195 throw new IllegalStateException("Data is clear");
196 }
197 return children;
198 }
199
200
201
202
203
204
205 @Override
206 public boolean equals(final Object o) {
207 if (this == o) {
208 return true;
209 }
210 if (!(o instanceof RamFileData)) {
211 return false;
212 }
213 final RamFileData../../../../../org/apache/commons/vfs2/provider/ram/RamFileData.html#RamFileData">RamFileData data = (RamFileData) o;
214 return this.getName().equals(data.getName());
215 }
216
217
218
219
220
221
222 @Override
223 public int hashCode() {
224 return this.getName().hashCode();
225 }
226
227 boolean hasChildren(final RamFileData data) {
228 return this.children.contains(data);
229 }
230
231
232
233
234 int size() {
235 return content.length;
236 }
237
238
239
240
241
242
243 void resize(final long newSize) {
244
245 if (newSize > Integer.MAX_VALUE) {
246 throw new IllegalArgumentException(
247 String.format("newSize(%d) > Integer.MAX_VALUE(%d)", newSize, Integer.MAX_VALUE));
248 }
249 final int resize = (int) newSize;
250 final int size = this.size();
251 final byte[] newBuf = new byte[resize];
252 System.arraycopy(this.content, 0, newBuf, 0, Math.min(resize, size));
253 this.content = newBuf;
254 updateLastModified();
255 }
256
257 }