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