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