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