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 */
017package org.apache.commons.vfs2.provider.ram;
018
019import java.io.IOException;
020import java.io.OutputStream;
021
022import org.apache.commons.vfs2.FileSystemException;
023
024/**
025 * OutputStream to a RamFile.
026 */
027public class RamFileOutputStream extends OutputStream {
028
029    /**
030     * File.
031     */
032    protected RamFileObject file;
033
034    /**
035     * buffer.
036     */
037    protected byte[] buffer1 = new byte[1];
038
039    /** File is open or closed */
040    protected boolean closed = false;
041
042    private IOException exception;
043
044    /**
045     * @param file The base file.
046     */
047    public RamFileOutputStream(final RamFileObject file) {
048        super();
049        this.file = file;
050    }
051
052    /*
053     * (non-Javadoc)
054     *
055     * @see java.io.DataOutput#write(byte[], int, int)
056     */
057    @Override
058    public void write(final byte[] b, final int off, final int len) throws IOException {
059        final RamFileData data = this.file.getData();
060        final int size = data.size();
061        final int newSize = size + len;
062        // Store the Exception in order to notify the client again on close()
063        try {
064            this.file.resize(newSize);
065        } catch (final IOException e) {
066            this.exception = e;
067            throw e;
068        }
069        System.arraycopy(b, off, data.getContent(), size, len);
070    }
071
072    /*
073     * (non-Javadoc)
074     *
075     * @see java.io.DataOutput#write(int)
076     */
077    @Override
078    public void write(final int b) throws IOException {
079        buffer1[0] = (byte) b;
080        this.write(buffer1);
081    }
082
083    @Override
084    public void flush() throws IOException {
085    }
086
087    @Override
088    public void close() throws IOException {
089        if (closed) {
090            return;
091        }
092        // Notify on close that there was an IOException while writing
093        if (exception != null) {
094            throw exception;
095        }
096        try {
097            this.closed = true;
098            // Close the
099            this.file.endOutput();
100        } catch (final Exception e) {
101            throw new FileSystemException(e);
102        }
103    }
104
105}