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