KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > upload > MultipartRequestInputStream


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.upload;
4
5 import jodd.io.FastByteArrayOutputStream;
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStream JavaDoc;
11
12 /**
13  * Extended input stream based on buffered requests input stream.
14  * It provides some more functions that might be useful when working
15  * with uploaded fies.
16  */

17 public class MultipartRequestInputStream extends BufferedInputStream JavaDoc {
18
19     public MultipartRequestInputStream(InputStream JavaDoc in) {
20         super(in);
21     }
22
23     public byte readByte() throws IOException JavaDoc {
24         int i = super.read();
25         if (i == -1) {
26             throw new IOException JavaDoc("Unable to read data from HTTP request.");
27         }
28         return (byte) i;
29     }
30
31     public void skipBytes(int i) throws IOException JavaDoc {
32         long len = super.skip(i);
33         if (len != i) {
34             throw new IOException JavaDoc("Unable to skip data in HTTP request.");
35         }
36     }
37
38
39     // ---------------------------------------------------------------- boundary
40

41     protected byte[] boundary;
42
43     /**
44      * Reads boundary from the input stream.
45      */

46     public byte[] readBoundary() throws IOException JavaDoc {
47         FastByteArrayOutputStream boundaryOutput = new FastByteArrayOutputStream();
48         byte b;
49         while ((b = readByte()) != '\r') {
50             boundaryOutput.write(b);
51         }
52         if (boundaryOutput.size() == 0) {
53             throw new IOException JavaDoc("Problems with parsing request: invalid boundary.");
54         }
55         skipBytes(1);
56         boundary = new byte[boundaryOutput.size() + 2];
57         System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2);
58         boundary[0] = '\r';
59         boundary[1] = '\n';
60         return boundary;
61     }
62
63
64
65     // ---------------------------------------------------------------- data header
66

67     protected FileUploadHeader lastHeader;
68
69     public FileUploadHeader getLastHeader() {
70         return lastHeader;
71     }
72
73     /**
74      * Reads data header from the input stream. When there is no more
75      * headers (i.e. end of stream reached), returns <code>null</code>
76      */

77     public FileUploadHeader readDataHeader(String JavaDoc encoding) throws IOException JavaDoc {
78         String JavaDoc dataHeader = readDataHeaderString(encoding);
79         if (dataHeader != null) {
80             lastHeader = new FileUploadHeader(dataHeader);
81         } else {
82             lastHeader = null;
83         }
84         return lastHeader;
85     }
86
87
88     protected String JavaDoc readDataHeaderString(String JavaDoc encoding) throws IOException JavaDoc {
89         FastByteArrayOutputStream data = new FastByteArrayOutputStream();
90         byte b;
91         while (true) {
92             // end marker byte on offset +0 and +2 must be 13
93
if ((b = readByte()) != '\r') {
94                 data.write(b);
95                 continue;
96             }
97             mark(4);
98             skipBytes(1);
99             int i = read();
100             if (i == -1) {
101                 // reached end of stream
102
return null;
103             }
104             if (i == '\r') {
105                 reset();
106                 break;
107             }
108             reset();
109             data.write(b);
110         }
111         skipBytes(3);
112         if (encoding != null) {
113             return data.toString(encoding);
114         } else {
115             return data.toString();
116         }
117     }
118
119
120     // ---------------------------------------------------------------- copy
121

122     /**
123      * Copies bytes from this stream to some output until boundary is
124      * reached. Returns number of copied bytes. It will throw an exception
125      * for any unregular behaviour.
126      */

127     public int copyAll(OutputStream out) throws IOException JavaDoc {
128         int count = 0;
129         while (true) {
130             byte b = readByte();
131             if (isBoundary(b)) {
132                 break;
133             }
134             out.write(b);
135             count++;
136         }
137         return count;
138     }
139
140     /**
141      * Copies max or less number of bytes to output stream. Usefull for determining
142      * if uploaded file is larger then expected.
143      */

144     public int copyMax(OutputStream out, int maxBytes) throws IOException JavaDoc {
145         int count = 0;
146         while (true) {
147             byte b = readByte();
148             if (isBoundary(b)) {
149                 break;
150             }
151             out.write(b);
152             count++;
153             if (count == maxBytes) {
154                 return count;
155             }
156         }
157         return count;
158     }
159
160     /**
161      * Skips to the boundary and returns total number of bytes skipped.
162      */

163     public int skipToBoundary() throws IOException JavaDoc {
164         int count = 0;
165         while (true) {
166             byte b = readByte();
167             count++;
168             if (isBoundary(b)) {
169                 break;
170             }
171         }
172         return count;
173     }
174
175     /**
176      * Checks if the current byte (i.e. one that was readed last) represents
177      * the very first byte of the boundary.
178      */

179     public boolean isBoundary(byte b) throws IOException JavaDoc {
180         int boundaryLen = boundary.length;
181         mark(boundaryLen + 1);
182         int bpos = 0;
183         while (b == boundary[bpos]) {
184             b = readByte();
185             bpos++;
186             if (bpos == boundaryLen) {
187                 return true; // boundary found!
188
}
189         }
190         reset();
191         return false;
192     }
193 }
194
Popular Tags