001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.ram;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023:
024: import org.apache.commons.vfs.FileName;
025: import org.apache.commons.vfs.FileObject;
026: import org.apache.commons.vfs.FileSystemException;
027: import org.apache.commons.vfs.FileType;
028: import org.apache.commons.vfs.RandomAccessContent;
029: import org.apache.commons.vfs.provider.AbstractFileObject;
030: import org.apache.commons.vfs.util.RandomAccessMode;
031:
032: /**
033: * A RAM File contains a single RAM FileData instance, it provides methods to
034: * access the data by implementing FileObject interface.
035: */
036: public class RamFileObject extends AbstractFileObject implements
037: FileObject {
038: /**
039: * File System
040: */
041: RamFileSystem fs;
042:
043: /**
044: * RAM File Object Data
045: */
046: private RamFileData data;
047:
048: private void save() throws FileSystemException {
049: this .fs.save(this );
050: }
051:
052: /**
053: * @param name
054: * @param fs
055: */
056: protected RamFileObject(FileName name, RamFileSystem fs) {
057: super (name, fs);
058: this .fs = fs;
059: this .fs.attach(this );
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetType()
066: */
067: protected FileType doGetType() throws Exception {
068: return data.getType();
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see org.apache.commons.vfs.provider.AbstractFileObject#doListChildren()
075: */
076: protected String[] doListChildren() throws Exception {
077: return this .fs.listChildren(this .getName());
078: }
079:
080: /*
081: * (non-Javadoc)
082: *
083: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetContentSize()
084: */
085: protected long doGetContentSize() throws Exception {
086: return this .data.getBuffer().length;
087: }
088:
089: /*
090: * (non-Javadoc)
091: *
092: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetInputStream()
093: */
094: protected InputStream doGetInputStream() throws Exception {
095: return new ByteArrayInputStream(this .data.getBuffer());
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetOutputStream(boolean)
102: */
103: protected OutputStream doGetOutputStream(boolean bAppend)
104: throws Exception {
105: if (!bAppend) {
106: this .data.setBuffer(new byte[0]);
107: }
108: return new RamFileOutputStream(this );
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see org.apache.commons.vfs.provider.AbstractFileObject#doDelete()
115: */
116: protected void doDelete() throws Exception {
117: fs.delete(this );
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetLastModifiedTime()
124: */
125: protected long doGetLastModifiedTime() throws Exception {
126: return data.getLastModified();
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see org.apache.commons.vfs.provider.AbstractFileObject#doSetLastModifiedTime(long)
133: */
134: protected boolean doSetLastModTime(long modtime) throws Exception {
135: data.setLastModified(modtime);
136: return true;
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see org.apache.commons.vfs.provider.AbstractFileObject#doCreateFolder()
143: */
144: protected void doCreateFolder() throws Exception {
145: this .injectType(FileType.FOLDER);
146: this .save();
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see org.apache.commons.vfs.provider.AbstractFileObject#doRename(org.apache.commons.vfs.FileObject)
153: */
154: protected void doRename(FileObject newfile) throws Exception {
155: fs.rename(this , (RamFileObject) newfile);
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetRandomAccessContent(org.apache.commons.vfs.util.RandomAccessMode)
162: */
163: protected RandomAccessContent doGetRandomAccessContent(
164: RandomAccessMode mode) throws Exception {
165: return new RamFileRandomAccessContent(this , mode);
166: }
167:
168: /*
169: * (non-Javadoc)
170: *
171: * @see org.apache.commons.vfs.provider.AbstractFileObject#doAttach()
172: */
173: protected void doAttach() throws Exception {
174: this .fs.attach(this );
175: }
176:
177: /**
178: * @return Returns the data.
179: */
180: RamFileData getData() {
181: return data;
182: }
183:
184: /**
185: * @param data
186: * The data to set.
187: */
188: void setData(RamFileData data) {
189: this .data = data;
190: }
191:
192: /*
193: * (non-Javadoc)
194: *
195: * @see org.apache.commons.vfs.provider.AbstractFileObject#injectType(org.apache.commons.vfs.FileType)
196: */
197: protected void injectType(FileType fileType) {
198: this .data.setType(fileType);
199: super .injectType(fileType);
200: }
201:
202: /*
203: * (non-Javadoc)
204: *
205: * @see org.apache.commons.vfs.provider.AbstractFileObject#endOutput()
206: */
207: protected void endOutput() throws Exception {
208: super .endOutput();
209: this .save();
210: }
211:
212: /**
213: * @return Returns the size of the RAMFileData
214: */
215: int size() {
216: if (data == null) {
217: return 0;
218: }
219: return data.size();
220: }
221:
222: /**
223: * @param newSize
224: * @throws IOException
225: * if the new size exceeds the limit
226: */
227: synchronized void resize(int newSize) throws IOException {
228: if (fs.getFileSystemOptions() != null) {
229: int maxSize = RamFileSystemConfigBuilder.getInstance()
230: .getMaxSize(fs.getFileSystemOptions());
231: if (fs.size() + newSize - this .size() > maxSize) {
232: throw new IOException("FileSystem capacity (" + maxSize
233: + ") exceeded.");
234: }
235: }
236: this.data.resize(newSize);
237: }
238:
239: }
|