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