001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.vfs2.RandomAccessContent;
023
024/**
025 * A RandomAccessContent that provides end-of-stream monitoring.
026 */
027public class MonitorRandomAccessContent implements RandomAccessContent
028{
029    private final RandomAccessContent content;
030    private boolean finished;
031
032    public MonitorRandomAccessContent(final RandomAccessContent content)
033    {
034        this.content = content;
035    }
036
037    /**
038     * Called after this stream is closed.
039     *
040     * @throws IOException if subclass throws it.
041     */
042    @SuppressWarnings("unused") // IOException is needed because subclasses may need to throw it
043    protected void onClose() throws IOException
044    {
045    }
046
047    /**
048     * Closes this content.
049     * @throws IOException if an error occurs.
050     */
051    @Override
052    public void close() throws IOException
053    {
054        if (finished)
055        {
056            return;
057        }
058
059        // Close the output stream
060        IOException exc = null;
061        try
062        {
063            content.close();
064        }
065        catch (final IOException ioe)
066        {
067            exc = ioe;
068        }
069
070        // Notify of end of output
071        exc = null;
072        try
073        {
074            onClose();
075        }
076        catch (final IOException ioe)
077        {
078            exc = ioe;
079        }
080
081        finished = true;
082
083        if (exc != null)
084        {
085            throw exc;
086        }
087    }
088
089    @Override
090    public long getFilePointer() throws IOException
091    {
092        return content.getFilePointer();
093    }
094
095    @Override
096    public void seek(final long pos) throws IOException
097    {
098        content.seek(pos);
099    }
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}