KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > PostInputStream


1 /*
2  * Copyright 1999-2004 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.cocoon.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 /**
21  * The class PostInputStream is a wrapper for InputStream associated with POST message.
22  * It allows to control read operation, restricting the number of bytes read to the value returned by getContentLen() method.
23  *
24  * @author <a HREF="mailto:Kinga_Dziembowski@hp.com">Kinga Dziembowski</a>
25  * @version CVS $Id: PostInputStream.java 124685 2005-01-08 22:20:56Z antonio $
26  */

27
28 public class PostInputStream extends InputStream JavaDoc {
29
30     /**
31     * Class name
32     */

33     public static final String JavaDoc CLASS = PostInputStream.class.getName();
34
35     /** The real InputStream object */
36     private InputStream JavaDoc m_inputStream = null;
37
38     /** The length of InputStream */
39     private int m_contentLen = 0;
40
41     /** The number of bytes read */
42     protected int m_bytesRead = 0;
43
44
45     /**
46     * Creates a PostInputStream
47     */

48     public PostInputStream() {
49         super();
50     }
51     /**
52     * Creates a <code>PostInputStream</code> based on a real InputStream object with the specified
53     * post message body length. Saves its argument, the input stream
54     * <code>m_inputStream</code>, for later use.
55     *
56     * @param input the underlying input stream.
57     * @param len the post message body length.
58     * @exception IllegalArgumentException len <= 0.
59     */

60
61     public PostInputStream(final InputStream JavaDoc input, final int len) throws IllegalArgumentException JavaDoc {
62         super();
63         init(input, len );
64     }
65     /**
66     * Sets the underlying input stream and contentLen value .
67     *
68     * @param input the input stream; can not be null.
69     * @param len the post message body length.
70     *
71     * @throws IllegalArgumentException
72     */

73     protected void init(final InputStream JavaDoc input, final int len) throws IllegalArgumentException JavaDoc {
74         if (len <= 0) {
75             throw new IllegalArgumentException JavaDoc("contentLen <= 0 ");
76         }
77         this.m_inputStream = input;
78         this.m_contentLen = len;
79     }
80
81     /**
82     * Sets the underlying input stream and contentLen value .
83     *
84     * @param input the input stream; can not be null.
85     * @param len the post message body length.
86     *
87     * @throws IOException
88     */

89     public synchronized void setInputStream(final InputStream JavaDoc input, final int len) throws IOException JavaDoc {
90         if (m_inputStream != null) {
91             close();
92         }
93         init(input, len);
94     }
95
96     /**
97     * Returns the underlying input stream.
98     *
99     * @return inputStream the underlying InputStream.
100     */

101     public synchronized InputStream JavaDoc getInputStream() {
102         return m_inputStream;
103     }
104
105     /**
106     * Returns the post message body length.
107     *
108     * @return m_contentLen;
109     */

110
111     public int getContentLen() {
112         return( m_contentLen );
113     }
114
115     /**
116     * Reads the next byte from the input stream. If the end of the stream has been reached, this method returns -1.
117     *
118     * @return the next byte or -1 if at the end of the stream.
119     *
120     * @throws IOException
121     */

122     public synchronized int read() throws IOException JavaDoc {
123
124         checkOpen();
125         if (m_bytesRead == m_contentLen) {
126             return -1;
127         }
128         int byt = m_inputStream.read();
129         if (byt != -1) {
130            m_bytesRead++;
131         }
132         return byt;
133     }
134
135     /**
136     * Reads bytes from this byte-input stream into the specified byte array,
137     * starting at the given offset.
138     *
139     * <p> This method implements the general contract of the corresponding
140     * <code>{@link InputStream#read(byte[], int, int) read}</code> method of
141     * the <code>{@link InputStream}</code> class.
142     * This method delegetes the read operation to the underlying InputStream implementation class but it
143     * controlls the number of bytes read from the stream. In the remote situation the underlying InputStream has no knowledge of
144     * the length of the stream and the notion of the "end" is undefined. This wrapper class has a knowledge of the
145     * length of data send by the requestor by the means of contentLength. This method returns the number of bytes read and
146     * accumulates the total number of bytes read in m_bytesRead. When the m_bytesRead is equal to the specified contentLength
147     * value the method returns returns -1 to signal the end of data.
148     *
149     * @param buffer the byte array to read into; can not be null.
150     * @param offset the starting offset in the byte array.
151     * @param len the maximum number of bytes to read.
152     *
153     * @return the number of bytes read, or <code>-1</code> if the end of
154     * the stream has been reached.
155     * @exception IOException if an I/O error occurs.
156     */

157
158     public synchronized int read(byte[] buffer, int offset, int len) throws IOException JavaDoc {
159         checkOpen();
160         if (m_bytesRead == m_contentLen) {
161             return -1;
162         }
163         int available = Math.min( available(), len );
164         int num = m_inputStream.read( buffer, offset, available );
165         if (num > 0) {
166             m_bytesRead += num;
167         }
168         return num;
169     }
170
171     public synchronized int read(byte[] buffer) throws IOException JavaDoc {
172
173         return read( buffer, 0, buffer.length);
174     }
175
176
177     /**
178     * Checks to see if this stream is closed; if it is, an IOException is thrown.
179     *
180     * @throws IOException
181     */

182     protected void checkOpen() throws IOException JavaDoc {
183         if (m_inputStream == null) {
184             throw new IOException JavaDoc("InputStream closed");
185         }
186     }
187
188     /**
189          * See the general contract of the <code>skip</code>
190      * method of <code>InputStream</code>.
191      * Delegates execution to the underlying InputStream implementation class.
192      * Checks to see if this stream is closed; if it is, an IOException is thrown.
193      * @param n the number of bytes to be skipped.
194      * @return the actual number of bytes skipped.
195      * @exception IOException if an I/O error occurs.
196      */

197     public synchronized long skip(long n) throws IOException JavaDoc {
198         checkOpen();
199         if ( m_bytesRead == m_contentLen )
200         {
201             return ( 0 );
202         }
203         else
204         {
205             return ( m_inputStream.skip( n ) );
206         }
207
208     }
209
210     /**
211     * Returns the number of bytes available from this input stream that can be read without the stream blocking.
212     * Delegates execution to the underlying InputStream implementation class.
213     * @return available the number of available bytes.
214     *
215     * @throws IOException
216     */

217     public synchronized int available() throws IOException JavaDoc {
218         checkOpen();
219         int avail = m_inputStream.available();
220         return (avail == 0 ? (m_contentLen - m_bytesRead) : avail);
221     }
222
223     /**
224     * Tests if this input stream supports the <code>mark</code>
225     * and <code>reset</code> methods. The <code>markSupported</code>
226     * method of <code>BufferedInputStream</code> returns
227     * <code>false</code>.
228     *
229     * @return a <code>boolean</code> indicating if this stream type supports
230     * the <code>mark</code> and <code>reset</code> methods.
231     * @see java.io.InputStream#mark(int)
232     * @see java.io.InputStream#reset()
233     */

234     public boolean markSupported() {
235         return false;
236     }
237
238     /**
239     * Closes this input stream by closing the underlying stream and marking this one as closed.
240     *
241     * @throws IOException
242     */

243     public synchronized void close() throws IOException JavaDoc {
244         if (m_inputStream == null) {
245             return;
246         }
247         m_inputStream.close();
248         m_inputStream = null;
249         m_contentLen = 0;
250         m_bytesRead = 0;
251     }
252
253     /**
254     * Returns a String representation of this.
255     *
256     * @return string the String representation of this.
257     */

258     public String JavaDoc toString() {
259         return new StringBuffer JavaDoc(getClass().getName())
260                 .append("[inputStream=").append(m_inputStream)
261                 .append(", contentLen=").append(m_contentLen)
262                 .append("bytesRead=").append(m_bytesRead)
263                 .append("]").toString();
264     }
265 }
266
Popular Tags