KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > ContentLengthInputStream


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v 1.6.2.1 2004/02/22 18:21:13 olegk Exp $
3  * $Revision: 1.6.2.1 $
4  * $Date: 2004/02/22 18:21:13 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.io.FilterInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37
38 /**
39  * Cuts the wrapped InputStream off after a specified number of bytes.
40  *
41  * @author Ortwin Glück
42  * @author Eric Johnson
43  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
44  * @since 2.0
45  */

46
47 public class ContentLengthInputStream extends FilterInputStream JavaDoc {
48     
49     /**
50      * The maximum number of bytes that can be read from the stream. Subsequent
51      * read operations will return -1.
52      */

53     private int contentLength;
54
55     /** The current position */
56     private int pos = 0;
57
58     /** True if the stream is closed. */
59     private boolean closed = false;
60
61     /**
62      * Creates a new length limited stream
63      *
64      * @param in The stream to wrap
65      * @param contentLength The maximum number of bytes that can be read from
66      * the stream. Subsequent read operations will return -1.
67      */

68     public ContentLengthInputStream(InputStream JavaDoc in, int contentLength) {
69         super(in);
70         this.contentLength = contentLength;
71     }
72
73     /**
74      * <p>Reads until the end of the known length of content.</p>
75      *
76      * <p>Does not close the underlying socket input, but instead leaves it
77      * primed to parse the next response.</p>
78      * @throws IOException If an IO problem occurs.
79      */

80     public void close() throws IOException JavaDoc {
81         if (!closed) {
82             try {
83                 ChunkedInputStream.exhaustInputStream(this);
84             } finally {
85                 // close after above so that we don't throw an exception trying
86
// to read after closed!
87
closed = true;
88             }
89         }
90     }
91
92
93     /**
94      * Read the next byte from the stream
95      * @return The next byte or -1 if the end of stream has been reached.
96      * @throws IOException If an IO problem occurs
97      * @see java.io.InputStream#read()
98      */

99     public int read() throws IOException JavaDoc {
100         if (closed) {
101             throw new IOException JavaDoc("Attempted read from closed stream.");
102         }
103
104         if (pos >= contentLength) {
105             return -1;
106         }
107         pos++;
108         return super.read();
109     }
110
111     /**
112      * Does standard {@link InputStream#read(byte[], int, int)} behavior, but
113      * also notifies the watcher when the contents have been consumed.
114      *
115      * @param b The byte array to fill.
116      * @param off Start filling at this position.
117      * @param len The number of bytes to attempt to read.
118      * @return The number of bytes read, or -1 if the end of content has been
119      * reached.
120      *
121      * @throws java.io.IOException Should an error occur on the wrapped stream.
122      */

123     public int read (byte[] b, int off, int len) throws java.io.IOException JavaDoc {
124         if (closed) {
125             throw new IOException JavaDoc("Attempted read from closed stream.");
126         }
127
128         if (pos >= contentLength) {
129             return -1;
130         }
131
132         if (pos + len > contentLength) {
133             len = contentLength - pos;
134         }
135         int count = super.read(b, off, len);
136         pos += count;
137         return count;
138     }
139
140
141     /**
142      * Read more bytes from the stream.
143      * @param b The byte array to put the new data in.
144      * @return The number of bytes read into the buffer.
145      * @throws IOException If an IO problem occurs
146      * @see java.io.InputStream#read(byte[])
147      */

148     public int read(byte[] b) throws IOException JavaDoc {
149         return read(b, 0, b.length);
150     }
151
152 }
153
Popular Tags