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.IOException;
    20  import java.io.OutputStream;
    21
    22  import org.apache.commons.vfs.FileSystemException;
    23
    24  /***
    25   * OutputStream to a RamFile
    26   */
    27  public class RamFileOutputStream extends OutputStream
    28  {
    29
    30  	/***
    31  	 * File
    32  	 */
    33  	protected RamFileObject file;
    34
    35  	/***
    36  	 * buffer
    37  	 */
    38  	protected byte buffer1[] = new byte[1];
    39
    40  	protected boolean closed = false;
    41
    42  	private IOException exc;
    43
    44  	/***
    45  	 * @param mode
    46  	 */
    47  	public RamFileOutputStream(RamFileObject file)
    48  	{
    49  		super();
    50  		this.file = file;
    51  	}
    52
    53  	/*
    54  	 * (non-Javadoc)
    55  	 *
    56  	 * @see java.io.DataOutput#write(byte[], int, int)
    57  	 */
    58  	public void write(byte[] b, int off, int len) throws IOException
    59  	{
    60  		int size = this.file.getData().size();
    61  		int newSize = this.file.getData().size() + len;
    62  		// Store the Exception in order to notify the client again on close()
    63  		try
    64  		{
    65  			this.file.resize(newSize);
    66  		}
    67  		catch (IOException e)
    68  		{
    69  			this.exc = e;
    70  			throw e;
    71  		}
    72  		System.arraycopy(b, off, this.file.getData().getBuffer(), size, len);
    73  	}
    74
    75  	/*
    76  	 * (non-Javadoc)
    77  	 *
    78  	 * @see java.io.DataOutput#write(int)
    79  	 */
    80  	public void write(int b) throws IOException
    81  	{
    82  		buffer1[0] = (byte) b;
    83  		this.write(buffer1);
    84  	}
    85
    86  	public void flush() throws IOException
    87  	{
    88  	}
    89
    90  	public void close() throws IOException
    91  	{
    92  		if (closed)
    93  		{
    94  			return;
    95  		}
    96  		// Notify on close that there was an IOException while writing
    97  		if (exc != null)
    98  		{
    99  			throw exc;
    100 		}
    101 		try
    102 		{
    103 			this.closed = true;
    104 			// Close the
    105 			this.file.endOutput();
    106 		}
    107 		catch (Exception e)
    108 		{
    109 			throw new FileSystemException(e);
    110 		}
    111 	}
    112
    113 }