| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ProxyInputStream |
|
| 2.1538461538461537;2.154 |
| 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.commons.io.input; | |
| 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 | * <p> | |
| 32 | * See the protected methods for ways in which a subclass can easily decorate | |
| 33 | * a stream with custom pre-, post- or error processing functionality. | |
| 34 | * | |
| 35 | * @version $Id: ProxyInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 36 | */ | |
| 37 | public abstract class ProxyInputStream extends FilterInputStream { | |
| 38 | ||
| 39 | /** | |
| 40 | * Constructs a new ProxyInputStream. | |
| 41 | * | |
| 42 | * @param proxy the InputStream to delegate to | |
| 43 | */ | |
| 44 | public ProxyInputStream(final InputStream proxy) { | |
| 45 | 614 | super(proxy); |
| 46 | // the proxy is stored in a protected superclass variable named 'in' | |
| 47 | 614 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Invokes the delegate's <code>read()</code> method. | |
| 51 | * @return the byte read or -1 if the end of stream | |
| 52 | * @throws IOException if an I/O error occurs | |
| 53 | */ | |
| 54 | @Override | |
| 55 | public int read() throws IOException { | |
| 56 | try { | |
| 57 | 28 | beforeRead(1); |
| 58 | 28 | final int b = in.read(); |
| 59 | 27 | afterRead(b != -1 ? 1 : -1); |
| 60 | 27 | return b; |
| 61 | 1 | } catch (final IOException e) { |
| 62 | 1 | handleIOException(e); |
| 63 | 0 | return -1; |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Invokes the delegate's <code>read(byte[])</code> method. | |
| 69 | * @param bts the buffer to read the bytes into | |
| 70 | * @return the number of bytes read or -1 if the end of stream | |
| 71 | * @throws IOException if an I/O error occurs | |
| 72 | */ | |
| 73 | @Override | |
| 74 | public int read(final byte[] bts) throws IOException { | |
| 75 | try { | |
| 76 | 1056816 | beforeRead(bts != null ? bts.length : 0); |
| 77 | 1056816 | final int n = in.read(bts); |
| 78 | 1056816 | afterRead(n); |
| 79 | 1056816 | return n; |
| 80 | 0 | } catch (final IOException e) { |
| 81 | 0 | handleIOException(e); |
| 82 | 0 | return -1; |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Invokes the delegate's <code>read(byte[], int, int)</code> method. | |
| 88 | * @param bts the buffer to read the bytes into | |
| 89 | * @param off The start offset | |
| 90 | * @param len The number of bytes to read | |
| 91 | * @return the number of bytes read or -1 if the end of stream | |
| 92 | * @throws IOException if an I/O error occurs | |
| 93 | */ | |
| 94 | @Override | |
| 95 | public int read(final byte[] bts, final int off, final int len) throws IOException { | |
| 96 | try { | |
| 97 | 35 | beforeRead(len); |
| 98 | 35 | final int n = in.read(bts, off, len); |
| 99 | 35 | afterRead(n); |
| 100 | 35 | return n; |
| 101 | 0 | } catch (final IOException e) { |
| 102 | 0 | handleIOException(e); |
| 103 | 0 | return -1; |
| 104 | } | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Invokes the delegate's <code>skip(long)</code> method. | |
| 109 | * @param ln the number of bytes to skip | |
| 110 | * @return the actual number of bytes skipped | |
| 111 | * @throws IOException if an I/O error occurs | |
| 112 | */ | |
| 113 | @Override | |
| 114 | public long skip(final long ln) throws IOException { | |
| 115 | try { | |
| 116 | 2 | return in.skip(ln); |
| 117 | 0 | } catch (final IOException e) { |
| 118 | 0 | handleIOException(e); |
| 119 | 0 | return 0; |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Invokes the delegate's <code>available()</code> method. | |
| 125 | * @return the number of available bytes | |
| 126 | * @throws IOException if an I/O error occurs | |
| 127 | */ | |
| 128 | @Override | |
| 129 | public int available() throws IOException { | |
| 130 | try { | |
| 131 | 52 | return super.available(); |
| 132 | 1 | } catch (final IOException e) { |
| 133 | 1 | handleIOException(e); |
| 134 | 0 | return 0; |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Invokes the delegate's <code>close()</code> method. | |
| 140 | * @throws IOException if an I/O error occurs | |
| 141 | */ | |
| 142 | @Override | |
| 143 | public void close() throws IOException { | |
| 144 | try { | |
| 145 | 377 | in.close(); |
| 146 | 1 | } catch (final IOException e) { |
| 147 | 1 | handleIOException(e); |
| 148 | 376 | } |
| 149 | 376 | } |
| 150 | ||
| 151 | /** | |
| 152 | * Invokes the delegate's <code>mark(int)</code> method. | |
| 153 | * @param readlimit read ahead limit | |
| 154 | */ | |
| 155 | @Override | |
| 156 | public synchronized void mark(final int readlimit) { | |
| 157 | 1 | in.mark(readlimit); |
| 158 | 1 | } |
| 159 | ||
| 160 | /** | |
| 161 | * Invokes the delegate's <code>reset()</code> method. | |
| 162 | * @throws IOException if an I/O error occurs | |
| 163 | */ | |
| 164 | @Override | |
| 165 | public synchronized void reset() throws IOException { | |
| 166 | try { | |
| 167 | 1 | in.reset(); |
| 168 | 0 | } catch (final IOException e) { |
| 169 | 0 | handleIOException(e); |
| 170 | 1 | } |
| 171 | 1 | } |
| 172 | ||
| 173 | /** | |
| 174 | * Invokes the delegate's <code>markSupported()</code> method. | |
| 175 | * @return true if mark is supported, otherwise false | |
| 176 | */ | |
| 177 | @Override | |
| 178 | public boolean markSupported() { | |
| 179 | 4 | return in.markSupported(); |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * Invoked by the read methods before the call is proxied. The number | |
| 184 | * of bytes that the caller wanted to read (1 for the {@link #read()} | |
| 185 | * method, buffer length for {@link #read(byte[])}, etc.) is given as | |
| 186 | * an argument. | |
| 187 | * <p> | |
| 188 | * Subclasses can override this method to add common pre-processing | |
| 189 | * functionality without having to override all the read methods. | |
| 190 | * The default implementation does nothing. | |
| 191 | * <p> | |
| 192 | * Note this method is <em>not</em> called from {@link #skip(long)} or | |
| 193 | * {@link #reset()}. You need to explicitly override those methods if | |
| 194 | * you want to add pre-processing steps also to them. | |
| 195 | * | |
| 196 | * @since 2.0 | |
| 197 | * @param n number of bytes that the caller asked to be read | |
| 198 | * @throws IOException if the pre-processing fails | |
| 199 | */ | |
| 200 | protected void beforeRead(final int n) throws IOException { | |
| 201 | 1056879 | } |
| 202 | ||
| 203 | /** | |
| 204 | * Invoked by the read methods after the proxied call has returned | |
| 205 | * successfully. The number of bytes returned to the caller (or -1 if | |
| 206 | * the end of stream was reached) is given as an argument. | |
| 207 | * <p> | |
| 208 | * Subclasses can override this method to add common post-processing | |
| 209 | * functionality without having to override all the read methods. | |
| 210 | * The default implementation does nothing. | |
| 211 | * <p> | |
| 212 | * Note this method is <em>not</em> called from {@link #skip(long)} or | |
| 213 | * {@link #reset()}. You need to explicitly override those methods if | |
| 214 | * you want to add post-processing steps also to them. | |
| 215 | * | |
| 216 | * @since 2.0 | |
| 217 | * @param n number of bytes read, or -1 if the end of stream was reached | |
| 218 | * @throws IOException if the post-processing fails | |
| 219 | */ | |
| 220 | protected void afterRead(final int n) throws IOException { | |
| 221 | 8274 | } |
| 222 | ||
| 223 | /** | |
| 224 | * Handle any IOExceptions thrown. | |
| 225 | * <p> | |
| 226 | * This method provides a point to implement custom exception | |
| 227 | * handling. The default behaviour is to re-throw the exception. | |
| 228 | * @param e The IOException thrown | |
| 229 | * @throws IOException if an I/O error occurs | |
| 230 | * @since 2.0 | |
| 231 | */ | |
| 232 | protected void handleIOException(final IOException e) throws IOException { | |
| 233 | 0 | throw e; |
| 234 | } | |
| 235 | ||
| 236 | } |