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.util;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.vfs2.RandomAccessContent;
23  
24  /**
25   * A RandomAccessContent that provides end-of-stream monitoring.
26   */
27  public class MonitorRandomAccessContent implements RandomAccessContent {
28  
29      private final RandomAccessContent content;
30      private boolean finished;
31  
32      public MonitorRandomAccessContent(final RandomAccessContent content) {
33          this.content = content;
34      }
35  
36      /**
37       * Called after this stream is closed.
38       *
39       * @throws IOException if subclass throws it.
40       */
41      @SuppressWarnings("unused") // IOException is needed because subclasses may need to throw it
42      protected void onClose() throws IOException {
43      }
44  
45      /**
46       * Closes this content.
47       *
48       * @throws IOException if an error occurs.
49       */
50      @Override
51      public void close() throws IOException {
52          if (finished) {
53              return;
54          }
55  
56          // Close the output stream
57          IOException exc;
58          try {
59              content.close();
60          } catch (final IOException ioe) {
61              exc = ioe;
62          }
63  
64          // Notify of end of output
65          exc = null;
66          try {
67              onClose();
68          } catch (final IOException ioe) {
69              exc = ioe;
70          }
71  
72          finished = true;
73  
74          if (exc != null) {
75              throw exc;
76          }
77      }
78  
79      @Override
80      public long getFilePointer() throws IOException {
81          return content.getFilePointer();
82      }
83  
84      @Override
85      public void seek(final long pos) throws IOException {
86          content.seek(pos);
87      }
88  
89      @Override
90      public long length() throws IOException {
91          return content.length();
92      }
93  
94      @Override
95      public void write(final int b) throws IOException {
96          content.write(b);
97      }
98  
99      @Override
100     public void write(final byte[] b) throws IOException {
101         content.write(b);
102     }
103 
104     @Override
105     public void write(final byte[] b, final int off, final int len) throws IOException {
106         content.write(b, off, len);
107     }
108 
109     @Override
110     public void writeBoolean(final boolean v) throws IOException {
111         content.writeBoolean(v);
112     }
113 
114     @Override
115     public void writeByte(final int v) throws IOException {
116         content.writeByte(v);
117     }
118 
119     @Override
120     public void writeShort(final int v) throws IOException {
121         content.writeShort(v);
122     }
123 
124     @Override
125     public void writeChar(final int v) throws IOException {
126         content.writeChar(v);
127     }
128 
129     @Override
130     public void writeInt(final int v) throws IOException {
131         content.writeInt(v);
132     }
133 
134     @Override
135     public void writeLong(final long v) throws IOException {
136         content.writeLong(v);
137     }
138 
139     @Override
140     public void writeFloat(final float v) throws IOException {
141         content.writeFloat(v);
142     }
143 
144     @Override
145     public void writeDouble(final double v) throws IOException {
146         content.writeDouble(v);
147     }
148 
149     @Override
150     public void writeBytes(final String s) throws IOException {
151         content.writeBytes(s);
152     }
153 
154     @Override
155     public void writeChars(final String s) throws IOException {
156         content.writeChars(s);
157     }
158 
159     @Override
160     public void writeUTF(final String str) throws IOException {
161         content.writeUTF(str);
162     }
163 
164     @Override
165     public void readFully(final byte[] b) throws IOException {
166         content.readFully(b);
167     }
168 
169     @Override
170     public void readFully(final byte[] b, final int off, final int len) throws IOException {
171         content.readFully(b, off, len);
172     }
173 
174     @Override
175     public int skipBytes(final int n) throws IOException {
176         return content.skipBytes(n);
177     }
178 
179     @Override
180     public void setLength(final long newLength) throws IOException {
181         content.setLength(newLength);
182     }
183 
184     @Override
185     public boolean readBoolean() throws IOException {
186         return content.readBoolean();
187     }
188 
189     @Override
190     public byte readByte() throws IOException {
191         return content.readByte();
192     }
193 
194     @Override
195     public int readUnsignedByte() throws IOException {
196         return content.readUnsignedByte();
197     }
198 
199     @Override
200     public short readShort() throws IOException {
201         return content.readShort();
202     }
203 
204     @Override
205     public int readUnsignedShort() throws IOException {
206         return content.readUnsignedShort();
207     }
208 
209     @Override
210     public char readChar() throws IOException {
211         return content.readChar();
212     }
213 
214     @Override
215     public int readInt() throws IOException {
216         return content.readInt();
217     }
218 
219     @Override
220     public long readLong() throws IOException {
221         return content.readLong();
222     }
223 
224     @Override
225     public float readFloat() throws IOException {
226         return content.readFloat();
227     }
228 
229     @Override
230     public double readDouble() throws IOException {
231         return content.readDouble();
232     }
233 
234     @Override
235     public String readLine() throws IOException {
236         return content.readLine();
237     }
238 
239     @Override
240     public String readUTF() throws IOException {
241         return content.readUTF();
242     }
243 
244     @Override
245     public InputStream getInputStream() throws IOException {
246         return content.getInputStream();
247     }
248 
249 }