LimitedInputStream.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.util;
00020 
00021 import java.io.InputStream;
00022 import java.io.IOException;
00023 
00028 public class LimitedInputStream extends InputStream {
00029     // bytes remaining to be read from the input stream. This is
00030     // initialized from CONTENT_LENGTH (or getContentLength()).
00031     // This is used in order to correctly return a -1 when all the
00032     // data POSTed was read. If this is left to -1, content length is
00033     // assumed as unknown and the standard InputStream methods will be used
00034     private long available;
00035     private long markedAvailable;
00036     private InputStream in;
00037 
00043     public LimitedInputStream(InputStream pIn, int pAvailable) {
00044                 in = pIn;
00045         available = pAvailable;
00046     }
00047 
00048     public int read() throws IOException {
00049         if (available > 0) {
00050             available--;
00051             return in.read();
00052         }
00053         return -1;
00054     }
00055 
00056     public int read(byte b[], int off, int len) throws IOException {
00057         if (available > 0) {
00058             if (len > available) {
00059                 // shrink len
00060                 len = (int) available;
00061             }
00062             int read = in.read(b, off, len);
00063             if (read == -1) {
00064                 available = 0;
00065             } else {
00066                 available -= read;
00067             }
00068             return read;
00069         }
00070         return -1;
00071     }
00072 
00073     public long skip(long n) throws IOException {
00074         long skip = in.skip(n);
00075         if (available > 0) {
00076             available -= skip;
00077         }
00078         return skip;
00079     }
00080 
00081     public void mark(int readlimit) {
00082         in.mark(readlimit);
00083         markedAvailable = available;
00084     }
00085 
00086     public void reset() throws IOException {
00087         in.reset();
00088         available = markedAvailable;
00089     }
00090 
00091     public boolean markSupported() {
00092         return true;
00093     }
00094 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49