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.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.FileSystemOptions;
27  import org.apache.commons.vfs2.FileType;
28  import org.apache.commons.vfs2.RandomAccessContent;
29  import org.apache.commons.vfs2.provider.AbstractFileName;
30  import org.apache.commons.vfs2.provider.AbstractFileObject;
31  import org.apache.commons.vfs2.util.FileObjectUtils;
32  import org.apache.commons.vfs2.util.RandomAccessMode;
33  
34  /**
35   * A RAM File contains a single RAM FileData instance, it provides methods to access the data by implementing FileObject
36   * interface.
37   */
38  public class RamFileObject extends AbstractFileObject<RamFileSystem> {
39  
40      /**
41       * RAM File Object Data.
42       */
43      private RamFileData data;
44  
45      /**
46       * @param name The name of the file.
47       * @param fs The FileSystem.
48       */
49      protected RamFileObject(final AbstractFileName name, final RamFileSystem fs) {
50          super(name, fs);
51          this.getAbstractFileSystem().attach(this);
52      }
53  
54      private void save() throws FileSystemException {
55          this.getAbstractFileSystem().save(this);
56      }
57  
58      /*
59       * (non-Javadoc)
60       *
61       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetType()
62       */
63      @Override
64      protected FileType doGetType() throws Exception {
65          return data.getType();
66      }
67  
68      /*
69       * (non-Javadoc)
70       *
71       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doListChildren()
72       */
73      @Override
74      protected String[] doListChildren() throws Exception {
75          return this.getAbstractFileSystem().listChildren(this.getName());
76      }
77  
78      /*
79       * (non-Javadoc)
80       *
81       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetContentSize()
82       */
83      @Override
84      protected long doGetContentSize() throws Exception {
85          return this.size();
86      }
87  
88      /*
89       * (non-Javadoc)
90       *
91       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetInputStream()
92       */
93      @Override
94      protected InputStream doGetInputStream(final int bufferSize) throws Exception {
95          // VFS-210: ram allows to gather an input stream even from a directory. So we need to check the type anyway.
96          if (!getType().hasContent()) {
97              throw new FileSystemException("vfs.provider/read-not-file.error", getName());
98          }
99  
100         return new ByteArrayInputStream(this.data.getContent());
101     }
102 
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetOutputStream(boolean)
107      */
108     @Override
109     protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
110         if (!bAppend) {
111             this.data.setContent(RamFileData.EMPTY);
112         }
113         return new RamFileOutputStream(this);
114     }
115 
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doDelete()
120      */
121     @Override
122     protected void doDelete() throws Exception {
123 
124         if (this.isContentOpen()) {
125             throw new FileSystemException(this.getName() + " cannot be deleted while the file is openg");
126         }
127         getAbstractFileSystem().delete(this);
128     }
129 
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetLastModifiedTime()
134      */
135     @Override
136     protected long doGetLastModifiedTime() throws Exception {
137         return data.getLastModified();
138     }
139 
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doSetLastModifiedTime(long)
144      */
145     /** @since 2.0 */
146     @Override
147     protected boolean doSetLastModifiedTime(final long modtime) throws Exception {
148         data.setLastModified(modtime);
149         return true;
150     }
151 
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doCreateFolder()
156      */
157     @Override
158     protected void doCreateFolder() throws Exception {
159         this.injectType(FileType.FOLDER);
160         this.save();
161     }
162 
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doRename(org.apache.commons.vfs2.FileObject)
167      */
168     @Override
169     protected void doRename(final FileObject newFile) throws Exception {
170         final RamFileObject/org/apache/commons/vfs2/provider/ram/RamFileObject.html#RamFileObject">RamFileObject newRamFileObject = (RamFileObject) FileObjectUtils.getAbstractFileObject(newFile);
171         getAbstractFileSystem().rename(this, newRamFileObject);
172     }
173 
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetRandomAccessContent(
178      * org.apache.commons.vfs2.util.RandomAccessMode)
179      */
180     @Override
181     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
182         return new RamFileRandomAccessContent(this, mode);
183     }
184 
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doAttach()
189      */
190     @Override
191     protected void doAttach() throws Exception {
192         this.getAbstractFileSystem().attach(this);
193     }
194 
195     /**
196      * @return Returns the data.
197      */
198     RamFileData getData() {
199         return data;
200     }
201 
202     /**
203      * @param data The data to set.
204      */
205     void setData(final RamFileData data) {
206         this.data = data;
207     }
208 
209     /*
210      * (non-Javadoc)
211      *
212      * @see org.apache.commons.vfs2.provider.AbstractFileObject#injectType(org.apache.commons.vfs2.FileType)
213      */
214     @Override
215     protected void injectType(final FileType fileType) {
216         this.data.setType(fileType);
217         super.injectType(fileType);
218     }
219 
220     /*
221      * (non-Javadoc)
222      *
223      * @see org.apache.commons.vfs2.provider.AbstractFileObject#endOutput()
224      */
225     @Override
226     protected void endOutput() throws Exception {
227         super.endOutput();
228         this.save();
229     }
230 
231     /**
232      * @return Returns the size of the {@link RamFileData}.
233      */
234     int size() {
235         return data == null ? 0 : data.size();
236     }
237 
238     /**
239      * @param newSize
240      * @throws IOException if the new size exceeds the limit
241      */
242     synchronized void resize(final long newSize) throws IOException {
243         final RamFileSystem afs = getAbstractFileSystem();
244         final FileSystemOptions afsOptions = afs.getFileSystemOptions();
245         if (afsOptions != null) {
246             final long maxSize = RamFileSystemConfigBuilder.getInstance().getLongMaxSize(afsOptions);
247             if (afs.size() + newSize - this.size() > maxSize) {
248                 throw new IOException("FileSystem capacity (" + maxSize + ") exceeded.");
249             }
250         }
251         this.data.resize(newSize);
252     }
253 
254 }