View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v 1.6 2003/01/28 04:40:20 jsdever Exp $ 3 * $Revision: 1.6 $ 4 * $Date: 2003/01/28 04:40:20 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import java.io.FilterInputStream; 67 import java.io.IOException; 68 import java.io.InputStream; 69 70 /*** 71 * Cuts the wrapped InputStream off after a specified number of bytes. 72 * 73 * @author Ortwin Gl�ck 74 * @author Eric Johnson 75 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 76 * @since 2.0 77 */ 78 79 public class ContentLengthInputStream extends FilterInputStream { 80 81 /*** 82 * The maximum number of bytes that can be read from the stream. Subsequent 83 * read operations will return -1. 84 */ 85 private int contentLength; 86 87 /*** The current position */ 88 private int pos = 0; 89 90 /*** True if the stream is closed. */ 91 private boolean closed = false; 92 93 /*** 94 * Creates a new length limited stream 95 * 96 * @param in The stream to wrap 97 * @param contentLength The maximum number of bytes that can be read from 98 * the stream. Subsequent read operations will return -1. 99 */ 100 public ContentLengthInputStream(InputStream in, int contentLength) { 101 super(in); 102 this.contentLength = contentLength; 103 } 104 105 /*** 106 * <p>Reads until the end of the known length of content.</p> 107 * 108 * <p>Does not close the underlying socket input, but instead leaves it 109 * primed to parse the next response.</p> 110 * @throws IOException If an IO problem occurs. 111 */ 112 public void close() throws IOException { 113 if (!closed) { 114 try { 115 ChunkedInputStream.exhaustInputStream(this); 116 } finally { 117 // close after above so that we don't throw an exception trying 118 // to read after closed! 119 closed = true; 120 } 121 } 122 } 123 124 125 /*** 126 * Read the next byte from the stream 127 * @return The next byte or -1 if the end of stream has been reached. 128 * @throws IOException If an IO problem occurs 129 * @see java.io.InputStream#read() 130 */ 131 public int read() throws IOException { 132 if (closed) { 133 throw new IOException("Attempted read from closed stream."); 134 } 135 136 if (pos >= contentLength) { 137 return -1; 138 } 139 pos++; 140 return super.read(); 141 } 142 143 /*** 144 * Does standard {@link InputStream#read(byte[], int, int)} behavior, but 145 * also notifies the watcher when the contents have been consumed. 146 * 147 * @param b The byte array to fill. 148 * @param off Start filling at this position. 149 * @param len The number of bytes to attempt to read. 150 * @return The number of bytes read, or -1 if the end of content has been 151 * reached. 152 * 153 * @throws java.io.IOException Should an error occur on the wrapped stream. 154 */ 155 public int read (byte[] b, int off, int len) throws java.io.IOException { 156 if (closed) { 157 throw new IOException("Attempted read from closed stream."); 158 } 159 160 if (pos >= contentLength) { 161 return -1; 162 } 163 164 if (pos + len > contentLength) { 165 len = contentLength - pos; 166 } 167 int count = super.read(b, off, len); 168 pos += count; 169 return count; 170 } 171 172 173 /*** 174 * Read more bytes from the stream. 175 * @param b The byte array to put the new data in. 176 * @return The number of bytes read into the buffer. 177 * @throws IOException If an IO problem occurs 178 * @see java.io.InputStream#read(byte[]) 179 */ 180 public int read(byte[] b) throws IOException { 181 return read(b, 0, b.length); 182 } 183 184 }

This page was automatically generated by Maven