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