Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/HttpWriter.java @ 984:7b0fa315e835
simplify HttpWriter
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 23:11:15 -0600 |
parents | 23ec25435b8c |
children | 8fef34f665e7 |
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. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * A writer that can wrap a {@link HttpOutput} stream and provide |
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 | |
45 private final HttpOutput _out; | |
46 private int _writeMode; | |
47 private int _surrogate; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
984 | 49 public HttpWriter(HttpOutput out) |
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 HttpOutput out = _out; | |
112 | |
113 while (length > 0) | |
114 { | |
115 out._bytes.reset(); | |
116 int chars = length>MAX_OUTPUT_CHARS?MAX_OUTPUT_CHARS:length; | |
117 | |
118 switch (_writeMode) | |
119 { | |
120 case WRITE_CONV: | |
121 { | |
122 Writer converter = getConverter(); | |
123 converter.write(s, offset, chars); | |
124 converter.flush(); | |
125 } | |
126 break; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
127 |
984 | 128 case WRITE_ISO1: |
129 { | |
130 byte[] buffer=out._bytes.getBuf(); | |
131 int bytes=out._bytes.getCount(); | |
132 | |
133 if (chars>buffer.length-bytes) | |
134 chars=buffer.length-bytes; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
135 |
984 | 136 for (int i = 0; i < chars; i++) |
137 { | |
138 int c = s[offset+i]; | |
139 buffer[bytes++]=(byte)(c<256?c:'?'); // ISO-1 and UTF-8 match for 0 - 255 | |
140 } | |
141 if (bytes>=0) | |
142 out._bytes.setCount(bytes); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 |
984 | 144 break; |
145 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
146 |
984 | 147 case WRITE_UTF8: |
148 { | |
149 byte[] buffer=out._bytes.getBuf(); | |
150 int bytes=out._bytes.getCount(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
151 |
984 | 152 if (bytes+chars>buffer.length) |
153 chars=buffer.length-bytes; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
154 |
984 | 155 for (int i = 0; i < chars; i++) |
156 { | |
157 int code = s[offset+i]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
158 |
984 | 159 // Do we already have a surrogate? |
160 if(_surrogate==0) | |
161 { | |
162 // No - is this char code a surrogate? | |
163 if(Character.isHighSurrogate((char)code)) | |
164 { | |
165 _surrogate=code; // UCS-? | |
166 continue; | |
167 } | |
168 } | |
169 // else handle a low surrogate | |
170 else if(Character.isLowSurrogate((char)code)) | |
171 { | |
172 code = Character.toCodePoint((char)_surrogate, (char)code); // UCS-4 | |
173 } | |
174 // else UCS-2 | |
175 else | |
176 { | |
177 code=_surrogate; // UCS-2 | |
178 _surrogate=0; // USED | |
179 i--; | |
180 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
181 |
984 | 182 if ((code & 0xffffff80) == 0) |
183 { | |
184 // 1b | |
185 if (bytes>=buffer.length) | |
186 { | |
187 chars=i; | |
188 break; | |
189 } | |
190 buffer[bytes++]=(byte)(code); | |
191 } | |
192 else | |
193 { | |
194 if((code&0xfffff800)==0) | |
195 { | |
196 // 2b | |
197 if (bytes+2>buffer.length) | |
198 { | |
199 chars=i; | |
200 break; | |
201 } | |
202 buffer[bytes++]=(byte)(0xc0|(code>>6)); | |
203 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
204 } | |
205 else if((code&0xffff0000)==0) | |
206 { | |
207 // 3b | |
208 if (bytes+3>buffer.length) | |
209 { | |
210 chars=i; | |
211 break; | |
212 } | |
213 buffer[bytes++]=(byte)(0xe0|(code>>12)); | |
214 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
215 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
216 } | |
217 else if((code&0xff200000)==0) | |
218 { | |
219 // 4b | |
220 if (bytes+4>buffer.length) | |
221 { | |
222 chars=i; | |
223 break; | |
224 } | |
225 buffer[bytes++]=(byte)(0xf0|(code>>18)); | |
226 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
227 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
228 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
229 } | |
230 else if((code&0xf4000000)==0) | |
231 { | |
232 // 5b | |
233 if (bytes+5>buffer.length) | |
234 { | |
235 chars=i; | |
236 break; | |
237 } | |
238 buffer[bytes++]=(byte)(0xf8|(code>>24)); | |
239 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f)); | |
240 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
241 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
242 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
243 } | |
244 else if((code&0x80000000)==0) | |
245 { | |
246 // 6b | |
247 if (bytes+6>buffer.length) | |
248 { | |
249 chars=i; | |
250 break; | |
251 } | |
252 buffer[bytes++]=(byte)(0xfc|(code>>30)); | |
253 buffer[bytes++]=(byte)(0x80|((code>>24)&0x3f)); | |
254 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f)); | |
255 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f)); | |
256 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f)); | |
257 buffer[bytes++]=(byte)(0x80|(code&0x3f)); | |
258 } | |
259 else | |
260 { | |
261 buffer[bytes++]=(byte)('?'); | |
262 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 |
984 | 264 _surrogate=0; // USED |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
265 |
984 | 266 if (bytes==buffer.length) |
267 { | |
268 chars=i+1; | |
269 break; | |
270 } | |
271 } | |
272 } | |
273 out._bytes.setCount(bytes); | |
274 break; | |
275 } | |
276 default: | |
277 throw new IllegalStateException(); | |
278 } | |
279 | |
280 out._bytes.writeTo(out); | |
281 length-=chars; | |
282 offset+=chars; | |
283 } | |
284 } | |
285 | |
286 private Writer getConverter() throws IOException | |
287 { | |
288 if (_out._converter == null) | |
289 _out._converter = new OutputStreamWriter(_out._bytes, _out._characterEncoding); | |
290 return _out._converter; | |
291 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 } |