Coverage Report - org.apache.commons.io.input.BoundedInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
BoundedInputStream
52%
23/44
55%
11/20
2
 
 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 static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 
 24  
 /**
 25  
  * This is a stream that will only supply bytes up to a certain length - if its
 26  
  * position goes above that, it will stop.
 27  
  * <p>
 28  
  * This is useful to wrap ServletInputStreams. The ServletInputStream will block
 29  
  * if you try to read content from it that isn't there, because it doesn't know
 30  
  * whether the content hasn't arrived yet or whether the content has finished.
 31  
  * So, one of these, initialized with the Content-length sent in the
 32  
  * ServletInputStream's header, will stop it blocking, providing it's been sent
 33  
  * with a correct content length.
 34  
  *
 35  
  * @version $Id: BoundedInputStream.java 1586342 2014-04-10 15:36:29Z ggregory $
 36  
  * @since 2.0
 37  
  */
 38  
 public class BoundedInputStream extends InputStream {
 39  
 
 40  
     /** the wrapped input stream */
 41  
     private final InputStream in;
 42  
 
 43  
     /** the max length to provide */
 44  
     private final long max;
 45  
 
 46  
     /** the number of bytes already returned */
 47  8
     private long pos = 0;
 48  
 
 49  
     /** the marked position */
 50  8
     private long mark = EOF;
 51  
 
 52  
     /** flag if close shoud be propagated */
 53  8
     private boolean propagateClose = true;
 54  
 
 55  
     /**
 56  
      * Creates a new <code>BoundedInputStream</code> that wraps the given input
 57  
      * stream and limits it to a certain size.
 58  
      *
 59  
      * @param in The wrapped input stream
 60  
      * @param size The maximum number of bytes to return
 61  
      */
 62  8
     public BoundedInputStream(final InputStream in, final long size) {
 63  
         // Some badly designed methods - eg the servlet API - overload length
 64  
         // such that "-1" means stream finished
 65  8
         this.max = size;
 66  8
         this.in = in;
 67  8
     }
 68  
 
 69  
     /**
 70  
      * Creates a new <code>BoundedInputStream</code> that wraps the given input
 71  
      * stream and is unlimited.
 72  
      *
 73  
      * @param in The wrapped input stream
 74  
      */
 75  
     public BoundedInputStream(final InputStream in) {
 76  1
         this(in, EOF);
 77  1
     }
 78  
 
 79  
     /**
 80  
      * Invokes the delegate's <code>read()</code> method if
 81  
      * the current position is less than the limit.
 82  
      * @return the byte read or -1 if the end of stream or
 83  
      * the limit has been reached.
 84  
      * @throws IOException if an I/O error occurs
 85  
      */
 86  
     @Override
 87  
     public int read() throws IOException {
 88  30
         if (max >= 0 && pos >= max) {
 89  2
             return EOF;
 90  
         }
 91  28
         final int result = in.read();
 92  28
         pos++;
 93  28
         return result;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Invokes the delegate's <code>read(byte[])</code> method.
 98  
      * @param b the buffer to read the bytes into
 99  
      * @return the number of bytes read or -1 if the end of stream or
 100  
      * the limit has been reached.
 101  
      * @throws IOException if an I/O error occurs
 102  
      */
 103  
     @Override
 104  
     public int read(final byte[] b) throws IOException {
 105  9
         return this.read(b, 0, b.length);
 106  
     }
 107  
 
 108  
     /**
 109  
      * Invokes the delegate's <code>read(byte[], int, int)</code> method.
 110  
      * @param b the buffer to read the bytes into
 111  
      * @param off The start offset
 112  
      * @param len The number of bytes to read
 113  
      * @return the number of bytes read or -1 if the end of stream or
 114  
      * the limit has been reached.
 115  
      * @throws IOException if an I/O error occurs
 116  
      */
 117  
     @Override
 118  
     public int read(final byte[] b, final int off, final int len) throws IOException {
 119  9
         if (max>=0 && pos>=max) {
 120  3
             return EOF;
 121  
         }
 122  6
         final long maxRead = max>=0 ? Math.min(len, max-pos) : len;
 123  6
         final int bytesRead = in.read(b, off, (int)maxRead);
 124  
 
 125  6
         if (bytesRead==EOF) {
 126  2
             return EOF;
 127  
         }
 128  
 
 129  4
         pos+=bytesRead;
 130  4
         return bytesRead;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Invokes the delegate's <code>skip(long)</code> method.
 135  
      * @param n the number of bytes to skip
 136  
      * @return the actual number of bytes skipped
 137  
      * @throws IOException if an I/O error occurs
 138  
      */
 139  
     @Override
 140  
     public long skip(final long n) throws IOException {
 141  0
         final long toSkip = max>=0 ? Math.min(n, max-pos) : n;
 142  0
         final long skippedBytes = in.skip(toSkip);
 143  0
         pos+=skippedBytes;
 144  0
         return skippedBytes;
 145  
     }
 146  
 
 147  
     /**
 148  
      * {@inheritDoc}
 149  
      */
 150  
     @Override
 151  
     public int available() throws IOException {
 152  0
         if (max>=0 && pos>=max) {
 153  0
             return 0;
 154  
         }
 155  0
         return in.available();
 156  
     }
 157  
 
 158  
     /**
 159  
      * Invokes the delegate's <code>toString()</code> method.
 160  
      * @return the delegate's <code>toString()</code>
 161  
      */
 162  
     @Override
 163  
     public String toString() {
 164  0
         return in.toString();
 165  
     }
 166  
 
 167  
     /**
 168  
      * Invokes the delegate's <code>close()</code> method
 169  
      * if {@link #isPropagateClose()} is {@code true}.
 170  
      * @throws IOException if an I/O error occurs
 171  
      */
 172  
     @Override
 173  
     public void close() throws IOException {
 174  0
         if (propagateClose) {
 175  0
             in.close();
 176  
         }
 177  0
     }
 178  
 
 179  
     /**
 180  
      * Invokes the delegate's <code>reset()</code> method.
 181  
      * @throws IOException if an I/O error occurs
 182  
      */
 183  
     @Override
 184  
     public synchronized void reset() throws IOException {
 185  0
         in.reset();
 186  0
         pos = mark;
 187  0
     }
 188  
 
 189  
     /**
 190  
      * Invokes the delegate's <code>mark(int)</code> method.
 191  
      * @param readlimit read ahead limit
 192  
      */
 193  
     @Override
 194  
     public synchronized void mark(final int readlimit) {
 195  0
         in.mark(readlimit);
 196  0
         mark = pos;
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Invokes the delegate's <code>markSupported()</code> method.
 201  
      * @return true if mark is supported, otherwise false
 202  
      */
 203  
     @Override
 204  
     public boolean markSupported() {
 205  0
         return in.markSupported();
 206  
     }
 207  
 
 208  
     /**
 209  
      * Indicates whether the {@link #close()} method
 210  
      * should propagate to the underling {@link InputStream}.
 211  
      *
 212  
      * @return {@code true} if calling {@link #close()}
 213  
      * propagates to the <code>close()</code> method of the
 214  
      * underlying stream or {@code false} if it does not.
 215  
      */
 216  
     public boolean isPropagateClose() {
 217  0
         return propagateClose;
 218  
     }
 219  
 
 220  
     /**
 221  
      * Set whether the {@link #close()} method
 222  
      * should propagate to the underling {@link InputStream}.
 223  
      *
 224  
      * @param propagateClose {@code true} if calling
 225  
      * {@link #close()} propagates to the <code>close()</code>
 226  
      * method of the underlying stream or
 227  
      * {@code false} if it does not.
 228  
      */
 229  
     public void setPropagateClose(final boolean propagateClose) {
 230  0
         this.propagateClose = propagateClose;
 231  0
     }
 232  
 }