Coverage Report - org.apache.xmlrpc.util.LimitedInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
LimitedInputStream
54% 
80% 
2
 
 1  
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.util;
 17  
 
 18  
 import java.io.InputStream;
 19  
 import java.io.IOException;
 20  
 
 21  
 /** A filtering {@link java.io.InputStream} for proper handling of
 22  
  * the <code>Content-Length</code> header: It guarantees to return
 23  
  * at most a given number of bytes.
 24  
  */
 25  
 public class LimitedInputStream extends InputStream {
 26  
     // bytes remaining to be read from the input stream. This is
 27  
     // initialized from CONTENT_LENGTH (or getContentLength()).
 28  
     // This is used in order to correctly return a -1 when all the
 29  
     // data POSTed was read. If this is left to -1, content length is
 30  
     // assumed as unknown and the standard InputStream methods will be used
 31  
     private long available;
 32  
     private long markedAvailable;
 33  
     private InputStream in;
 34  
 
 35  
     /** Creates a new instance, reading from the given input stream
 36  
      * and returning at most the given number of bytes.
 37  
      * @param pIn Input stream being read.
 38  
      * @param pAvailable Number of bytes available in <code>pIn</code>.
 39  
      */
 40  186
     public LimitedInputStream(InputStream pIn, int pAvailable) {
 41  186
                 in = pIn;
 42  186
         available = pAvailable;
 43  186
     }
 44  
 
 45  
     public int read() throws IOException {
 46  5738
         if (available > 0) {
 47  5738
             available--;
 48  5738
             return in.read();
 49  
         }
 50  0
         return -1;
 51  
     }
 52  
 
 53  
     public int read(byte b[], int off, int len) throws IOException {
 54  372
         if (available > 0) {
 55  186
             if (len > available) {
 56  
                 // shrink len
 57  186
                 len = (int) available;
 58  
             }
 59  186
             int read = in.read(b, off, len);
 60  186
             if (read == -1) {
 61  0
                 available = 0;
 62  
             } else {
 63  186
                 available -= read;
 64  
             }
 65  186
             return read;
 66  
         }
 67  186
         return -1;
 68  
     }
 69  
 
 70  
     public long skip(long n) throws IOException {
 71  0
         long skip = in.skip(n);
 72  0
         if (available > 0) {
 73  0
             available -= skip;
 74  
         }
 75  0
         return skip;
 76  
     }
 77  
 
 78  
     public void mark(int readlimit) {
 79  0
         in.mark(readlimit);
 80  0
         markedAvailable = available;
 81  0
     }
 82  
 
 83  
     public void reset() throws IOException {
 84  0
         in.reset();
 85  0
         available = markedAvailable;
 86  0
     }
 87  
 
 88  
     public boolean markSupported() {
 89  0
         return true;
 90  
     }
 91  
 }