KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > web > MultipartInputStream


1 /***
2 * jwma Java WebMail
3 * Copyright (c) 2001 Dieter Wimberger
4 *
5 * jwma is free software; you can distribute and use this source
6 * under the terms of the BSD-style license received along with
7 * the distribution.
8 ***/

9 package org.lucane.server.web;
10
11 import java.io.FilterInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15
16 import javax.activation.DataSource JavaDoc;
17
18 /**
19  * Class that provides a MultipartInputStream by wrapping
20  * an existant InputStream.<br>
21  * It implements size limit checking and serves as a
22  * <code>DataSource</code> for handling with
23  * Mail API (or other JAF aware) classes.
24  *
25  * @author Dieter Wimberger
26  * @version 0.9.5 04/05/2001
27  */

28 public class MultipartInputStream
29     extends FilterInputStream JavaDoc
30     implements DataSource JavaDoc {
31
32     //instance attributes
33
private int m_Limit;
34     private String JavaDoc m_ContentType;
35     private int m_BytesRead=0;
36     
37     /**
38      * Constructs a <code>MultipartInputStream</code> instance.
39      *
40      * @param in the InputStream this instance reads from.
41      * @param ctype the content type of the incoming request as <code>
42      * String</code>. Important because it contains the parts border!
43      * @param readlimit the maximum size of the complete request data as
44      * <code>int</code>.
45      *
46      * @return the newly created <code>MultipartInputStream</code> instance.
47      */

48      public MultipartInputStream(InputStream JavaDoc in, String JavaDoc ctype, int readlimit) {
49         super((InputStream JavaDoc)in);
50         m_ContentType=ctype;
51         m_Limit=readlimit;
52      }//constructor
53

54     /**
55      * Returns the name of this instance as <code>String</code>.
56      * <p>(DataSource implementation)
57      *
58      * @return the name as <code>String</code>
59      */

60      public String JavaDoc getName() {
61         return("form_data");
62      }//getName
63

64     /**
65      * Returns the content type of this instance as
66      * <code>String</code>.
67      * <p>(DataSource implementation)
68      * <p><i><b>Note:</b>the <code>String</code> contains the parts border!</i>
69      *
70      * @return the content type as <code>String</code>
71      */

72      public String JavaDoc getContentType() {
73         return m_ContentType;
74      }//getContentType
75

76     /**
77      * Returns this <code>MultipartInputStream</code> instance as
78      * <code>InputStream</code>.
79      * <p>(DataSource implementation)
80      *
81      * @return this instance as <code>InputStream</code>.
82      * @throws IOException if impossible.
83      */

84      public InputStream JavaDoc getInputStream()
85             throws IOException JavaDoc {
86             
87         return (InputStream JavaDoc)this;
88      }//getInputStream
89

90     /**
91      * Throws an IOException in this implementation, because
92      * this instance represents a read-only <code>DataSource</code>.
93      * <p>(DataSource implementation)
94      *
95      * @return an <code>OutputStream</code> instance for writing
96      * to this <code>DataSource</code>.
97      * @throws IOException if impossible.
98      */

99      public OutputStream JavaDoc getOutputStream()
100             throws IOException JavaDoc {
101         
102         throw new IOException JavaDoc("Cannot output to this source.");
103      }//getOutputStream
104

105     /**
106      * Reads the next byte of data from the input stream. The value byte is
107      * returned as an <code>int</code> in the range <code>0</code> to
108      * <code>255</code>. If no byte is available because the end of the stream
109      * has been reached, the value <code>-1</code> is returned. This method
110      * blocks until input data is available, the end of the stream is detected,
111      * or an exception is thrown.
112      *
113      * @return the next byte of data, or <code>-1</code> if the end of the
114      * stream is reached.
115      * @exception IOException if an I/O error occurs.
116      */

117      public int read() throws IOException JavaDoc {
118         m_BytesRead++;
119         checkLimit();
120         return super.read();
121      }//read
122

123     /**
124      * Reads up to <code>len</code> bytes of data from this input stream
125      * into an array of bytes. This method blocks until some input is
126      * available.
127      * <p>
128      * This method simply performs <code>in.read(b, off, len)</code>
129      * and returns the result.
130      *
131      * @param b the buffer into which the data is read.
132      * @param off the start offset of the data.
133      * @param len the maximum number of bytes read.
134      * @return the total number of bytes read into the buffer, or
135      * <code>-1</code> if there is no more data because the end of
136      * the stream has been reached.
137      * @exception IOException if an I/O error occurs.
138      * @see java.io.FilterInputStream#in
139      */

140      public int read(byte b[],int off , int len)
141         throws IOException JavaDoc {
142         m_BytesRead+=len;
143         checkLimit();
144         return super.read(b,off,len);
145      }//read
146

147     /**
148      * Checks if the size limit is exceeded, throwing
149      * an IOException if so.
150      *
151      * @throws IOException if the limit is exceeded.
152      */

153      private void checkLimit()
154             throws IOException JavaDoc {
155         
156         if(m_BytesRead>m_Limit) {
157             throw new IOException JavaDoc("Input limit exceeded.");
158         }
159      }//checkLimit
160

161 }//class MultipartInputStream
162
Popular Tags