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