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.tika.io;
18  
19  import java.io.FilterInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  /**
24   * A Proxy stream which acts as expected, that is it passes the method
25   * calls on to the proxied stream and doesn't change which methods are
26   * being called.
27   * <p>
28   * It is an alternative base class to FilterInputStream
29   * to increase reusability, because FilterInputStream changes the
30   * methods being called, such as read(byte[]) to read(byte[], int, int).
31   *
32   * @author Stephen Colebourne
33   * @version $Id: ProxyInputStream.java 778043 2009-05-23 22:07:14Z jukka $
34   */
35  public abstract class ProxyInputStream extends FilterInputStream {
36  
37      /**
38       * Constructs a new ProxyInputStream.
39       *
40       * @param proxy  the InputStream to delegate to
41       */
42      public ProxyInputStream(InputStream proxy) {
43          super(proxy);
44          // the proxy is stored in a protected superclass variable named 'in'
45      }
46  
47      /**
48       * Invokes the delegate's <code>read()</code> method.
49       * @return the byte read or -1 if the end of stream
50       * @throws IOException if an I/O error occurs
51       */
52      @Override
53      public int read() throws IOException {
54          try {
55              return in.read();
56          } catch (IOException e) {
57              handleIOException(e);
58              return -1;
59          }
60      }
61  
62      /**
63       * Invokes the delegate's <code>read(byte[])</code> method.
64       * @param bts the buffer to read the bytes into
65       * @return the number of bytes read or -1 if the end of stream
66       * @throws IOException if an I/O error occurs
67       */
68      @Override
69      public int read(byte[] bts) throws IOException {
70          try {
71              return in.read(bts);
72          } catch (IOException e) {
73              handleIOException(e);
74              return -1;
75          }
76      }
77  
78      /**
79       * Invokes the delegate's <code>read(byte[], int, int)</code> method.
80       * @param bts the buffer to read the bytes into
81       * @param off The start offset
82       * @param len The number of bytes to read
83       * @return the number of bytes read or -1 if the end of stream
84       * @throws IOException if an I/O error occurs
85       */
86      @Override
87      public int read(byte[] bts, int off, int len) throws IOException {
88          try {
89              return in.read(bts, off, len);
90          } catch (IOException e) {
91              handleIOException(e);
92              return -1;
93          }
94      }
95  
96      /**
97       * Invokes the delegate's <code>skip(long)</code> method.
98       * @param ln the number of bytes to skip
99       * @return the number of bytes to skipped or -1 if the end of stream
100      * @throws IOException if an I/O error occurs
101      */
102     @Override
103     public long skip(long ln) throws IOException {
104         try {
105             return in.skip(ln);
106         } catch (IOException e) {
107             handleIOException(e);
108             return 0;
109         }
110     }
111 
112     /**
113      * Invokes the delegate's <code>available()</code> method.
114      * @return the number of available bytes
115      * @throws IOException if an I/O error occurs
116      */
117     @Override
118     public int available() throws IOException {
119         try {
120             return super.available();
121         } catch (IOException e) {
122             handleIOException(e);
123             return 0;
124         }
125     }
126 
127     /**
128      * Invokes the delegate's <code>close()</code> method.
129      * @throws IOException if an I/O error occurs
130      */
131     @Override
132     public void close() throws IOException {
133         try {
134             in.close();
135         } catch (IOException e) {
136             handleIOException(e);
137         }
138     }
139 
140     /**
141      * Invokes the delegate's <code>mark(int)</code> method.
142      * @param readlimit read ahead limit
143      */
144     @Override
145     public synchronized void mark(int readlimit) {
146         in.mark(readlimit);
147     }
148 
149     /**
150      * Invokes the delegate's <code>reset()</code> method.
151      * @throws IOException if an I/O error occurs
152      */
153     @Override
154     public synchronized void reset() throws IOException {
155         try {
156             in.reset();
157         } catch (IOException e) {
158             handleIOException(e);
159         }
160     }
161 
162     /**
163      * Invokes the delegate's <code>markSupported()</code> method.
164      * @return true if mark is supported, otherwise false
165      */
166     @Override
167     public boolean markSupported() {
168         return in.markSupported();
169     }
170 
171 
172     /**
173      * Handle any IOExceptions thrown.
174      * <p>
175      * This method provides a point to implement custom exception
176      * handling. The default behaviour is to re-throw the exception.
177      * @param e The IOException thrown
178      * @throws IOException if an I/O error occurs
179      */
180     protected void handleIOException(IOException e) throws IOException {
181         throw e;
182     }
183 
184 }