View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.vfs2.FileName;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.FileType;
27  
28  /**
29   * RAM File Object Data.
30   */
31  class RamFileData implements Serializable {
32  
33      static final byte[] EMPTY = new byte[0];
34  
35      /**
36       * serialVersionUID format is YYYYMMDD for the date of the last binary change.
37       */
38      private static final long serialVersionUID = 20101208L;
39  
40      /**
41       * File Name.
42       */
43      private FileName name;
44  
45      /**
46       * File Type.
47       */
48      private FileType type;
49  
50      /**
51       * Bytes.
52       */
53      private byte[] content;
54  
55      /**
56       * Last modified time
57       */
58      private long lastModified;
59  
60      /**
61       * Children
62       */
63      private final Collection<RamFileData> children;
64  
65      /**
66       * Constructor.
67       *
68       * @param name The file name.
69       */
70      public RamFileData(final FileName name) {
71          super();
72          this.children = Collections.synchronizedCollection(new ArrayList<RamFileData>());
73          this.clear();
74          if (name == null) {
75              throw new IllegalArgumentException("name can not be null");
76          }
77          this.name = name;
78      }
79  
80      /**
81       * @return Returns the buffer.
82       */
83      byte[] getContent() {
84          return content;
85      }
86  
87      /**
88       * @param content The buffer.
89       */
90      void setContent(final byte[] content) {
91          updateLastModified();
92          this.content = content;
93      }
94  
95      /**
96       * @return Returns the lastModified.
97       */
98      long getLastModified() {
99          return lastModified;
100     }
101 
102     /**
103      * @param lastModified The lastModified to set.
104      */
105     void setLastModified(final long lastModified) {
106         this.lastModified = lastModified;
107     }
108 
109     /**
110      * @return Returns the type.
111      */
112     FileType getType() {
113         return type;
114     }
115 
116     /**
117      * @param type The type to set.
118      */
119     void setType(final FileType type) {
120         this.type = type;
121     }
122 
123     /**
124      */
125     void clear() {
126         this.content = EMPTY;
127         updateLastModified();
128         this.type = FileType.IMAGINARY;
129         this.children.clear();
130         this.name = null;
131     }
132 
133     void updateLastModified() {
134         this.lastModified = System.currentTimeMillis();
135     }
136 
137     /**
138      * @return Returns the name.
139      */
140     FileName getName() {
141         return name;
142     }
143 
144     /*
145      * (non-Javadoc)
146      *
147      * @see java.lang.Object#toString()
148      */
149     @Override
150     public String toString() {
151         return this.name.toString();
152     }
153 
154     /**
155      * Add a child.
156      *
157      * @param data The file data.
158      * @throws FileSystemException if an error occurs.
159      */
160     void addChild(final RamFileData data) throws FileSystemException {
161         if (!this.getType().hasChildren()) {
162             throw new FileSystemException("A child can only be added in a folder");
163         }
164 
165         FileSystemException.requireNonNull(data, "No child can be null");
166 
167         if (this.children.contains(data)) {
168             throw new FileSystemException("Child already exists. " + data);
169         }
170 
171         this.children.add(data);
172         updateLastModified();
173     }
174 
175     /**
176      * Remove a child.
177      *
178      * @param data The file data.
179      * @throws FileSystemException if an error occurs.
180      */
181     void removeChild(final RamFileData data) throws FileSystemException {
182         if (!this.getType().hasChildren()) {
183             throw new FileSystemException("A child can only be removed from a folder");
184         }
185         if (!this.children.contains(data)) {
186             throw new FileSystemException("Child not found. " + data);
187         }
188         this.children.remove(data);
189         updateLastModified();
190     }
191 
192     /**
193      * @return Returns the children.
194      */
195     Collection<RamFileData> getChildren() {
196         if (name == null) {
197             throw new IllegalStateException("Data is clear");
198         }
199         return children;
200     }
201 
202     /*
203      * (non-Javadoc)
204      *
205      * @see java.lang.Object#equals(java.lang.Object)
206      */
207     @Override
208     public boolean equals(final Object o) {
209         if (this == o) {
210             return true;
211         }
212         if (!(o instanceof RamFileData)) {
213             return false;
214         }
215         final RamFileData../../../../../org/apache/commons/vfs2/provider/ram/RamFileData.html#RamFileData">RamFileData data = (RamFileData) o;
216         return this.getName().equals(data.getName());
217     }
218 
219     /*
220      * (non-Javadoc)
221      *
222      * @see java.lang.Object#hashCode()
223      */
224     @Override
225     public int hashCode() {
226         return this.getName().hashCode();
227     }
228 
229     boolean hasChildren(final RamFileData data) {
230         return this.children.contains(data);
231     }
232 
233     /**
234      * @return Returns the size of the buffer
235      */
236     int size() {
237         return content.length;
238     }
239 
240     /**
241      * Resize the buffer
242      *
243      * @param newSize The new buffer size.
244      */
245     void resize(final long newSize) {
246         // A future implementation may allow longs/multiple buffer/and so on
247         if (newSize > Integer.MAX_VALUE) {
248             throw new IllegalArgumentException(
249                     String.format("newSize(%d) > Integer.MAX_VALUE(%d)", newSize, Integer.MAX_VALUE));
250         }
251         final int resize = (int) newSize;
252         final int size = this.size();
253         final byte[] newBuf = new byte[resize];
254         System.arraycopy(this.content, 0, newBuf, 0, Math.min(resize, size));
255         this.content = newBuf;
256         updateLastModified();
257     }
258 
259 }