KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > core > CmsMultipartInputStreamHandler


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/core/CmsMultipartInputStreamHandler.java,v $
3 * Date : $Date: 2005/05/17 13:47:28 $
4 * Version: $Revision: 1.1 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29 package com.opencms.core;
30
31 import java.io.IOException JavaDoc;
32
33 import javax.servlet.ServletInputStream JavaDoc;
34
35 /**
36  * A class to aid in reading multipart/form-data from a ServletInputStream.
37  * <p>
38  * It keeps track of how many bytes have been read and detects when the
39  * Content-Length limit has been reached. This is necessary since some
40  * servlet engines are slow to notice the end of stream.
41  *
42  * @author Michael Emmerich
43  * @author Alexander Lucas
44  *
45  * @deprecated Will not be supported past the OpenCms 6 release.
46  */

47 class CmsMultipartInputStreamHandler {
48     
49     private ServletInputStream JavaDoc m_in;
50     private int m_totalExpected;
51     private int m_totalRead;
52     private int m_newLine;
53
54     /**
55      * Constructor, creates a new CmsMultipartInputStreamHandler.<p>
56      *
57      * @param in An input stream
58      * @param totalExpected Number of bytes expected to be read
59      */

60     public CmsMultipartInputStreamHandler(ServletInputStream JavaDoc in, int totalExpected) {
61         m_in = in;
62         m_totalExpected = totalExpected;
63     }
64
65     /** A pass-through to ServletInputStream.read() that keeps track
66      * how many bytes have been read and stops reading when the
67      * Content-Length limit has been reached.
68      *
69      * @return value of the next byte or -1 if no byte could be read.
70      * @throws IOException Throws IOException if any error with the input stream occurs.
71      */

72     public int read() throws IOException JavaDoc {
73         if (m_totalRead >= m_totalExpected) {
74             return -1;
75         } else {
76             int result = m_in.read();
77             if (result > -1) {
78                 m_totalRead++;
79             }
80             return result;
81         }
82     }
83
84     /** Reads the next line of input. Returns null to indicate the end
85      * of stream.
86      *
87      * @return Line of input.
88      * @throws IOException Throws IOException if any error with the input stream occurs.
89      */

90     public String JavaDoc readLine() throws IOException JavaDoc {
91         byte[] buf = new byte[64 * 1024];
92         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
93         int result;
94
95         // loop only if the buffer was filled
96
do {
97
98             // this.readLine() does +=
99
result = this.readLine(buf, 0, buf.length);
100             if (result != -1) {
101                 sbuf.append(new String JavaDoc(buf, 0, result, "ISO-8859-1"));
102             }
103         } while(result == buf.length);
104         if (sbuf.length() == 0) {
105             // nothing read, must be at the end of stream
106
return null;
107         }
108         // cut off the trailing newline
109
if (m_newLine == 0) {
110             m_newLine = (result > 1 && (buf[result - 2] == '\r' || buf[result - 2] == '\n')) ? 2 : 1;
111         }
112         buf = null;
113         sbuf.setLength(sbuf.length() - m_newLine);
114         return sbuf.toString();
115     }
116
117     /** A pass-through to ServletInputStream.readLine() that keeps track
118      * how many bytes have been read and stops reading when the
119      * Content-Length limit has been reached.
120      *
121      * @param b Array of bytes.
122      * @param off Read offset.
123      * @param len Length of byte buffer.
124      * @return Number of bytes read.
125      * @throws IOException Throws IOException if any error with the input stream occurs.
126      */

127     public int readLine(byte[] b, int off, int len) throws IOException JavaDoc {
128         if (m_totalRead >= m_totalExpected) {
129             return -1;
130         } else {
131             int result = m_in.readLine(b, off, len);
132             if (result > 0) {
133                 m_totalRead += result;
134             }
135             return result;
136         }
137     }
138 }
139
Popular Tags