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     */
017    package org.apache.commons.vfs2.util;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    
022    import org.apache.commons.vfs2.RandomAccessContent;
023    
024    /**
025     * An RandomAccessContent that provides end-of-stream monitoring.
026     *
027     * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
028     */
029    public class MonitorRandomAccessContent implements RandomAccessContent
030    {
031        private final RandomAccessContent content;
032        private boolean finished;
033    
034        public MonitorRandomAccessContent(final RandomAccessContent content)
035        {
036            this.content = content;
037        }
038    
039        /**
040         * Called after this stream is closed.  This implementation does nothing.
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        public void close() throws IOException
052        {
053            if (finished)
054            {
055                return;
056            }
057    
058            // Close the output stream
059            IOException exc = null;
060            try
061            {
062                content.close();
063            }
064            catch (final IOException ioe)
065            {
066                exc = ioe;
067            }
068    
069            // Notify of end of output
070            exc = null;
071            try
072            {
073                onClose();
074            }
075            catch (final IOException ioe)
076            {
077                exc = ioe;
078            }
079    
080            finished = true;
081    
082            if (exc != null)
083            {
084                throw exc;
085            }
086        }
087    
088        public long getFilePointer() throws IOException
089        {
090            return content.getFilePointer();
091        }
092    
093        public void seek(long pos) throws IOException
094        {
095            content.seek(pos);
096        }
097    
098        public long length() throws IOException
099        {
100            return content.length();
101        }
102    
103        public void write(int b) throws IOException
104        {
105            content.write(b);
106        }
107    
108        public void write(byte[] b) throws IOException
109        {
110            content.write(b);
111        }
112    
113        public void write(byte[] b, int off, int len) throws IOException
114        {
115            content.write(b, off, len);
116        }
117    
118        public void writeBoolean(boolean v) throws IOException
119        {
120            content.writeBoolean(v);
121        }
122    
123        public void writeByte(int v) throws IOException
124        {
125            content.writeByte(v);
126        }
127    
128        public void writeShort(int v) throws IOException
129        {
130            content.writeShort(v);
131        }
132    
133        public void writeChar(int v) throws IOException
134        {
135            content.writeChar(v);
136        }
137    
138        public void writeInt(int v) throws IOException
139        {
140            content.writeInt(v);
141        }
142    
143        public void writeLong(long v) throws IOException
144        {
145            content.writeLong(v);
146        }
147    
148        public void writeFloat(float v) throws IOException
149        {
150            content.writeFloat(v);
151        }
152    
153        public void writeDouble(double v) throws IOException
154        {
155            content.writeDouble(v);
156        }
157    
158        public void writeBytes(String s) throws IOException
159        {
160            content.writeBytes(s);
161        }
162    
163        public void writeChars(String s) throws IOException
164        {
165            content.writeChars(s);
166        }
167    
168        public void writeUTF(String str) throws IOException
169        {
170            content.writeUTF(str);
171        }
172    
173        public void readFully(byte[] b) throws IOException
174        {
175            content.readFully(b);
176        }
177    
178        public void readFully(byte[] b, int off, int len) throws IOException
179        {
180            content.readFully(b, off, len);
181        }
182    
183        public int skipBytes(int n) throws IOException
184        {
185            return content.skipBytes(n);
186        }
187    
188        public boolean readBoolean() throws IOException
189        {
190            return content.readBoolean();
191        }
192    
193        public byte readByte() throws IOException
194        {
195            return content.readByte();
196        }
197    
198        public int readUnsignedByte() throws IOException
199        {
200            return content.readUnsignedByte();
201        }
202    
203        public short readShort() throws IOException
204        {
205            return content.readShort();
206        }
207    
208        public int readUnsignedShort() throws IOException
209        {
210            return content.readUnsignedShort();
211        }
212    
213        public char readChar() throws IOException
214        {
215            return content.readChar();
216        }
217    
218        public int readInt() throws IOException
219        {
220            return content.readInt();
221        }
222    
223        public long readLong() throws IOException
224        {
225            return content.readLong();
226        }
227    
228        public float readFloat() throws IOException
229        {
230            return content.readFloat();
231        }
232    
233        public double readDouble() throws IOException
234        {
235            return content.readDouble();
236        }
237    
238        public String readLine() throws IOException
239        {
240            return content.readLine();
241        }
242    
243        public String readUTF() throws IOException
244        {
245            return content.readUTF();
246        }
247    
248        public InputStream getInputStream() throws IOException
249        {
250            return content.getInputStream();
251        }
252    }