001    /*
002     * Copyright (c) 2005 Henri Sivonen
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.iki.hsivonen.io;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    
028    /**
029     * @version $Id: ObservableInputStream.java,v 1.2 2005/07/24 07:35:24 hsivonen Exp $
030     * @author hsivonen
031     */
032    public class ObservableInputStream extends InputStream {
033    
034        private StreamObserver observer;
035    
036        private InputStream delegate;
037    
038        /**
039         *  
040         */
041        public ObservableInputStream(InputStream delegate, StreamObserver obs) {
042            this.delegate = delegate;
043            this.observer = obs;
044        }
045    
046        /**
047         * @see java.io.InputStream#available()
048         */
049        public int available() throws IOException {
050            try {
051                return delegate.available();
052            } catch (RuntimeException e) {
053                observer.exceptionOccurred(e);
054                throw e;
055            } catch (IOException e) {
056                observer.exceptionOccurred(e);
057                throw e;
058            }
059        }
060    
061        /**
062         * @see java.io.InputStream#close()
063         */
064        public void close() throws IOException {
065            try {
066                observer.closeCalled();
067                delegate.close();
068            } catch (RuntimeException e) {
069                observer.exceptionOccurred(e);
070                throw e;
071            } catch (IOException e) {
072                observer.exceptionOccurred(e);
073                throw e;
074            }
075        }
076    
077        /**
078         * @see java.io.InputStream#mark(int)
079         */
080        public void mark(int arg0) {
081            try {
082                delegate.mark(arg0);
083            } catch (RuntimeException e) {
084                observer.exceptionOccurred(e);
085                throw e;
086            }
087        }
088    
089        /**
090         * @see java.io.InputStream#markSupported()
091         */
092        public boolean markSupported() {
093            try {
094                return delegate.markSupported();
095            } catch (RuntimeException e) {
096                observer.exceptionOccurred(e);
097                throw e;
098            }
099        }
100    
101        /**
102         * @return
103         * @throws java.io.IOException
104         */
105        public int read() throws IOException {
106            try {
107                return delegate.read();
108            } catch (RuntimeException e) {
109                observer.exceptionOccurred(e);
110                throw e;
111            } catch (IOException e) {
112                observer.exceptionOccurred(e);
113                throw e;
114            }
115        }
116    
117        /**
118         * @see java.io.InputStream#read(byte[])
119         */
120        public int read(byte[] arg0) throws IOException {
121            try {
122                return delegate.read(arg0);
123            } catch (RuntimeException e) {
124                observer.exceptionOccurred(e);
125                throw e;
126            } catch (IOException e) {
127                observer.exceptionOccurred(e);
128                throw e;
129            }
130        }
131    
132        /**
133         * @see java.io.InputStream#read(byte[], int, int)
134         */
135        public int read(byte[] arg0, int arg1, int arg2) throws IOException {
136            try {
137                return delegate.read(arg0, arg1, arg2);
138            } catch (RuntimeException e) {
139                observer.exceptionOccurred(e);
140                throw e;
141            } catch (IOException e) {
142                observer.exceptionOccurred(e);
143                throw e;
144            }
145        }
146    
147        /**
148         * @see java.io.InputStream#reset()
149         */
150        public void reset() throws IOException {
151            try {
152                delegate.reset();
153            } catch (RuntimeException e) {
154                observer.exceptionOccurred(e);
155                throw e;
156            } catch (IOException e) {
157                observer.exceptionOccurred(e);
158                throw e;
159            }
160        }
161    
162        /**
163         * @see java.io.InputStream#skip(long)
164         */
165        public long skip(long arg0) throws IOException {
166            try {
167                return delegate.skip(arg0);
168            } catch (RuntimeException e) {
169                observer.exceptionOccurred(e);
170                throw e;
171            } catch (IOException e) {
172                observer.exceptionOccurred(e);
173                throw e;
174            }
175        }
176        
177        
178        /**
179         * @see java.lang.Object#finalize()
180         */
181        protected void finalize() throws Throwable {
182            observer.finalizerCalled();
183            super.finalize();
184        }
185    }