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