Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/HttpWriter.java @ 998:bf176925e00f
remove Request.getReader()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 23:01:00 -0600 |
parents | 8fef34f665e7 |
children |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.OutputStreamWriter; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.Writer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
983
23ec25435b8c
simplify AbstractGenerator
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
25 import org.eclipse.jetty.http.HttpGenerator; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import org.eclipse.jetty.util.ByteArrayOutputStream2; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import org.eclipse.jetty.util.StringUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 /** OutputWriter. |
985 | 30 * A writer that can wrap a {@link AbstractHttpConnection.Output} stream and provide |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 * character encodings. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 * The UTF-8 encoding is done by this class and no additional |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 * buffers or Writers are used. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 * The UTF-8 code was inspired by http://javolution.org |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 */ |
984 | 37 public final class HttpWriter extends Writer |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 { |
984 | 39 private static final int MAX_OUTPUT_CHARS = 512; |
40 | |
41 private static final int WRITE_CONV = 0; | |
42 private static final int WRITE_ISO1 = 1; | |
43 private static final int WRITE_UTF8 = 2; | |
44 | |
985 | 45 private final AbstractHttpConnection.Output _out; |
984 | 46 private int _writeMode; |
47 private int _surrogate; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
985 | 49 public HttpWriter(AbstractHttpConnection.Output out) |
984 | 50 { |
51 _out = out; | |
52 _surrogate = 0; // AS lastUTF16CodePoint | |
53 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 |
984 | 55 public void setCharacterEncoding(String encoding) |
56 { | |
57 if (encoding == null || StringUtil.__ISO_8859_1.equalsIgnoreCase(encoding)) | |
58 { | |
59 _writeMode = WRITE_ISO1; | |
60 } | |
61 else if (StringUtil.__UTF8.equalsIgnoreCase(encoding)) | |
62 { | |
63 _writeMode = WRITE_UTF8; | |
64 } | |
65 else | |
66 { | |
67 _writeMode = WRITE_CONV; | |
68 if (_out._characterEncoding == null || !_out._characterEncoding.equalsIgnoreCase(encoding)) | |
69 _out._converter = null; // Set lazily in getConverter() | |
70 } | |
71 | |
72 _out._characterEncoding = encoding; | |
73 if (_out._bytes==null) | |
74 _out._bytes = new ByteArrayOutputStream2(MAX_OUTPUT_CHARS); | |
75 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 |
984 | 77 @Override |
78 public void close() throws IOException | |
79 { | |
80 _out.close(); | |
81 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 |
984 | 83 @Override |
84 public void flush() throws IOException | |
85 { | |
86 _out.flush(); | |
87 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 |
984 | 89 @Override |
90 public void write (String s,int offset, int length) throws IOException | |
91 { | |
92 while (length > MAX_OUTPUT_CHARS) | |
93 { | |
94 write(s, offset, MAX_OUTPUT_CHARS); | |
95 offset += MAX_OUTPUT_CHARS; | |
96 length -= MAX_OUTPUT_CHARS; | |
97 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
98 |
984 | 99 if (_out._chars==null) |
100 { | |
101 _out._chars = new char[MAX_OUTPUT_CHARS]; | |
102 } | |
103 char[] chars = _out._chars; | |
104 s.getChars(offset, offset + length, chars, 0); | |
105 write(chars, 0, length); | |
106 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 |
984 | 108 @Override |
109 public void write (char[] s,int offset, int length) throws IOException | |
110 { | |
111 while (length > 0) | |
112 { | |
985 | 113 _out._bytes.reset(); |
984 | 114 int chars = length>MAX_OUTPUT_CHARS?MAX_OUTPUT_CHARS:length; |
115 | |
116 switch (_writeMode) | |
117 { | |
118 case WRITE_CONV: | |
119 { | |
120 Writer converter = getConverter(); | |
121 converter.write(s, offset, chars); | |
122 converter.flush(); | |
123 } | |
124 break; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
125 |
984 | 126 case WRITE_ISO1: |
127 { | |
985 | 128 byte[] buffer = _out._bytes.getBuf(); |
129 int bytes = _out._bytes.getCount(); | |
984 | 130 |
131 if (chars>buffer.length-bytes) | |
132 chars=buffer.length-bytes; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
984 | 134 for (int i = 0; i < chars; i++) |
135 { | |
136 int c = s[offset+i]; | |
137 buffer[bytes++]=(byte)(c<256?c:'?'); // ISO-1 and UTF-8 match for 0 - 255 | |
138 } | |
139 if (bytes>=0) | |
985 | 140 _out._bytes.setCount(bytes); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
141 |
984 | 142 break; |
143 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
144 |
984 | 145 case WRITE_UTF8: |
146 { | |
985 | 147 byte[] buffer = _out._bytes.getBuf(); |
148 int bytes = _out._bytes.getCount(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
149 |
984 | 150 if (bytes+chars>buffer.length) |
151 chars=buffer.length-bytes; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
984 | 153 for (int i = 0; i < chars; i++) |
154 { | |
155 int code = s[offset+i]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
156 |
984 | 157 // Do we already have a surrogate? |
158 if(_surrogate==0) | |
159 { | |
160 // No - is this char code a surrogate? | |
161 if(Character.isHighSurrogate((char)code)) | |
162 { | |
163 _surrogate=code; // UCS-? | |
164 continue; | |
165 } | |
166 } | |
167 // else handle a low surrogate | |
168 else if(Character.isLowSurrogate((char)code)) | |
169 { | |
170 code = Character.toCodePoint((char)_surrogate, (char)code); // UCS-4 | |
171 } | |
172 // else UCS-2 | |
173 else | |
174 { | |
175 code=_surrogate; // UCS-2 | |
176 _surrogate=0; // USED | |
177 i--; | |
178 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
179 |
984 | 180 if ((code & 0xffffff80) == 0) |
181 { | |
182 // 1b | |
183 if (bytes>=buffer.length) | |
184 { | |
185 chars=i; | |
186 break; | |
187 } | |
188 buffer[bytes++]=(byte)(code); | |
189 } | |
190 else | |
191 { | |
192 if((code&0xfffff800)==0) | |
193 { | |
194 // 2b | |
195 if (bytes+2>buffer.length) | |
196 { | |
197 chars=i; | |
198 break; | |
199 } | |
200 buffer[bytes++]=(byte)(0xc0|(code>>6)); | |
201 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
202 } | |
203 else if((code&0xffff0000)==0) | |
204 { | |
205 // 3b | |
206 if (bytes+3>buffer.length) | |
207 { | |
208 chars=i; | |
209 break; | |
210 } | |
211 buffer[bytes++]=(byte)(0xe0|(code>>12)); | |
212 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
213 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
214 } | |
215 else if((code&0xff200000)==0) | |
216 { | |
217 // 4b | |
218 if (bytes+4>buffer.length) | |
219 { | |
220 chars=i; | |
221 break; | |
222 } | |
223 buffer[bytes++]=(byte)(0xf0|(code>>18)); | |
224 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
225 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
226 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
227 } | |
228 else if((code&0xf4000000)==0) | |
229 { | |
230 // 5b | |
231 if (bytes+5>buffer.length) | |
232 { | |
233 chars=i; | |
234 break; | |
235 } | |
236 buffer[bytes++]=(byte)(0xf8|(code>>24)); | |
237 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f)); | |
238 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
239 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
240 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
241 } | |
242 else if((code&0x80000000)==0) | |
243 { | |
244 // 6b | |
245 if (bytes+6>buffer.length) | |
246 { | |
247 chars=i; | |
248 break; | |
249 } | |
250 buffer[bytes++]=(byte)(0xfc|(code>>30)); | |
251 buffer[bytes++]=(byte)(0x80|((code>>24)&0x3f)); | |
252 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f)); | |
253 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
254 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
255 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
256 } | |
257 else | |
258 { | |
259 buffer[bytes++]=(byte)('?'); | |
260 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
261 |
984 | 262 _surrogate=0; // USED |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 |
984 | 264 if (bytes==buffer.length) |
265 { | |
266 chars=i+1; | |
267 break; | |
268 } | |
269 } | |
270 } | |
985 | 271 _out._bytes.setCount(bytes); |
984 | 272 break; |
273 } | |
274 default: | |
275 throw new IllegalStateException(); | |
276 } | |
277 | |
985 | 278 _out._bytes.writeTo(_out); |
984 | 279 length-=chars; |
280 offset+=chars; | |
281 } | |
282 } | |
283 | |
284 private Writer getConverter() throws IOException | |
285 { | |
286 if (_out._converter == null) | |
287 _out._converter = new OutputStreamWriter(_out._bytes, _out._characterEncoding); | |
288 return _out._converter; | |
289 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
290 } |