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