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