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.  This implementation does nothing.
039     */
040    @SuppressWarnings("unused") // IOException is needed because subclasses may need to throw it
041    protected void onClose() throws IOException
042    {
043    }
044
045    /**
046     * Closes this content.
047     * @throws IOException if an error occurs.
048     */
049    @Override
050    public void close() throws IOException
051    {
052        if (finished)
053        {
054            return;
055        }
056
057        // Close the output stream
058        IOException exc = null;
059        try
060        {
061            content.close();
062        }
063        catch (final IOException ioe)
064        {
065            exc = ioe;
066        }
067
068        // Notify of end of output
069        exc = null;
070        try
071        {
072            onClose();
073        }
074        catch (final IOException ioe)
075        {
076            exc = ioe;
077        }
078
079        finished = true;
080
081        if (exc != null)
082        {
083            throw exc;
084        }
085    }
086
087    @Override
088    public long getFilePointer() throws IOException
089    {
090        return content.getFilePointer();
091    }
092
093    @Override
094    public void seek(final long pos) throws IOException
095    {
096        content.seek(pos);
097    }
098
099    @Override
100    public long length() throws IOException
101    {
102        return content.length();
103    }
104
105    @Override
106    public void write(final int b) throws IOException
107    {
108        content.write(b);
109    }
110
111    @Override
112    public void write(final byte[] b) throws IOException
113    {
114        content.write(b);
115    }
116
117    @Override
118    public void write(final byte[] b, final int off, final int len) throws IOException
119    {
120        content.write(b, off, len);
121    }
122
123    @Override
124    public void writeBoolean(final boolean v) throws IOException
125    {
126        content.writeBoolean(v);
127    }
128
129    @Override
130    public void writeByte(final int v) throws IOException
131    {
132        content.writeByte(v);
133    }
134
135    @Override
136    public void writeShort(final int v) throws IOException
137    {
138        content.writeShort(v);
139    }
140
141    @Override
142    public void writeChar(final int v) throws IOException
143    {
144        content.writeChar(v);
145    }
146
147    @Override
148    public void writeInt(final int v) throws IOException
149    {
150        content.writeInt(v);
151    }
152
153    @Override
154    public void writeLong(final long v) throws IOException
155    {
156        content.writeLong(v);
157    }
158
159    @Override
160    public void writeFloat(final float v) throws IOException
161    {
162        content.writeFloat(v);
163    }
164
165    @Override
166    public void writeDouble(final double v) throws IOException
167    {
168        content.writeDouble(v);
169    }
170
171    @Override
172    public void writeBytes(final String s) throws IOException
173    {
174        content.writeBytes(s);
175    }
176
177    @Override
178    public void writeChars(final String s) throws IOException
179    {
180        content.writeChars(s);
181    }
182
183    @Override
184    public void writeUTF(final String str) throws IOException
185    {
186        content.writeUTF(str);
187    }
188
189    @Override
190    public void readFully(final byte[] b) throws IOException
191    {
192        content.readFully(b);
193    }
194
195    @Override
196    public void readFully(final byte[] b, final int off, final int len) throws IOException
197    {
198        content.readFully(b, off, len);
199    }
200
201    @Override
202    public int skipBytes(final int n) throws IOException
203    {
204        return content.skipBytes(n);
205    }
206
207    @Override
208    public void setLength(final long newLength) throws IOException
209    {
210        content.setLength(newLength);
211    }
212
213    @Override
214    public boolean readBoolean() throws IOException
215    {
216        return content.readBoolean();
217    }
218
219    @Override
220    public byte readByte() throws IOException
221    {
222        return content.readByte();
223    }
224
225    @Override
226    public int readUnsignedByte() throws IOException
227    {
228        return content.readUnsignedByte();
229    }
230
231    @Override
232    public short readShort() throws IOException
233    {
234        return content.readShort();
235    }
236
237    @Override
238    public int readUnsignedShort() throws IOException
239    {
240        return content.readUnsignedShort();
241    }
242
243    @Override
244    public char readChar() throws IOException
245    {
246        return content.readChar();
247    }
248
249    @Override
250    public int readInt() throws IOException
251    {
252        return content.readInt();
253    }
254
255    @Override
256    public long readLong() throws IOException
257    {
258        return content.readLong();
259    }
260
261    @Override
262    public float readFloat() throws IOException
263    {
264        return content.readFloat();
265    }
266
267    @Override
268    public double readDouble() throws IOException
269    {
270        return content.readDouble();
271    }
272
273    @Override
274    public String readLine() throws IOException
275    {
276        return content.readLine();
277    }
278
279    @Override
280    public String readUTF() throws IOException
281    {
282        return content.readUTF();
283    }
284
285    @Override
286    public InputStream getInputStream() throws IOException
287    {
288        return content.getInputStream();
289    }
290
291}