annotate src/org/eclipse/jetty/server/HttpWriter.java @ 1039:a7319f14ba1e

remove Buffer.isImmutable()
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2016 22:55:28 -0600
parents 8fef34f665e7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
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
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
37 public final class HttpWriter extends Writer
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 {
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
39 private static final int MAX_OUTPUT_CHARS = 512;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
40
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
41 private static final int WRITE_CONV = 0;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
42 private static final int WRITE_ISO1 = 1;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
43 private static final int WRITE_UTF8 = 2;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
44
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
45 private final AbstractHttpConnection.Output _out;
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
46 private int _writeMode;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
47 private int _surrogate;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
49 public HttpWriter(AbstractHttpConnection.Output out)
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
50 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
51 _out = out;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
52 _surrogate = 0; // AS lastUTF16CodePoint
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
53 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
55 public void setCharacterEncoding(String encoding)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
56 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
57 if (encoding == null || StringUtil.__ISO_8859_1.equalsIgnoreCase(encoding))
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
58 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
59 _writeMode = WRITE_ISO1;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
60 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
61 else if (StringUtil.__UTF8.equalsIgnoreCase(encoding))
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
62 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
63 _writeMode = WRITE_UTF8;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
64 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
65 else
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
66 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
67 _writeMode = WRITE_CONV;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
68 if (_out._characterEncoding == null || !_out._characterEncoding.equalsIgnoreCase(encoding))
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
69 _out._converter = null; // Set lazily in getConverter()
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
70 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
71
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
72 _out._characterEncoding = encoding;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
73 if (_out._bytes==null)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
74 _out._bytes = new ByteArrayOutputStream2(MAX_OUTPUT_CHARS);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
75 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
77 @Override
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
78 public void close() throws IOException
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
79 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
80 _out.close();
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
81 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
83 @Override
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
84 public void flush() throws IOException
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
85 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
86 _out.flush();
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
87 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
89 @Override
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
90 public void write (String s,int offset, int length) throws IOException
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
91 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
92 while (length > MAX_OUTPUT_CHARS)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
93 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
94 write(s, offset, MAX_OUTPUT_CHARS);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
95 offset += MAX_OUTPUT_CHARS;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
96 length -= MAX_OUTPUT_CHARS;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
97 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
99 if (_out._chars==null)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
100 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
101 _out._chars = new char[MAX_OUTPUT_CHARS];
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
102 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
103 char[] chars = _out._chars;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
104 s.getChars(offset, offset + length, chars, 0);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
105 write(chars, 0, length);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
106 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
108 @Override
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
109 public void write (char[] s,int offset, int length) throws IOException
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
110 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
111 while (length > 0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
112 {
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
113 _out._bytes.reset();
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
114 int chars = length>MAX_OUTPUT_CHARS?MAX_OUTPUT_CHARS:length;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
115
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
116 switch (_writeMode)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
117 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
118 case WRITE_CONV:
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
119 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
120 Writer converter = getConverter();
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
121 converter.write(s, offset, chars);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
122 converter.flush();
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
123 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
124 break;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
126 case WRITE_ISO1:
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
127 {
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
128 byte[] buffer = _out._bytes.getBuf();
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
129 int bytes = _out._bytes.getCount();
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
130
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
131 if (chars>buffer.length-bytes)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
132 chars=buffer.length-bytes;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
134 for (int i = 0; i < chars; i++)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
135 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
136 int c = s[offset+i];
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
137 buffer[bytes++]=(byte)(c<256?c:'?'); // ISO-1 and UTF-8 match for 0 - 255
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
138 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
139 if (bytes>=0)
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
140 _out._bytes.setCount(bytes);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
142 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
143 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
145 case WRITE_UTF8:
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
146 {
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
147 byte[] buffer = _out._bytes.getBuf();
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
148 int bytes = _out._bytes.getCount();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
150 if (bytes+chars>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
151 chars=buffer.length-bytes;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
153 for (int i = 0; i < chars; i++)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
154 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
155 int code = s[offset+i];
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
157 // Do we already have a surrogate?
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
158 if(_surrogate==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
159 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
160 // No - is this char code a surrogate?
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
161 if(Character.isHighSurrogate((char)code))
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
162 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
163 _surrogate=code; // UCS-?
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
164 continue;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
165 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
166 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
167 // else handle a low surrogate
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
168 else if(Character.isLowSurrogate((char)code))
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
169 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
170 code = Character.toCodePoint((char)_surrogate, (char)code); // UCS-4
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
171 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
172 // else UCS-2
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
173 else
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
174 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
175 code=_surrogate; // UCS-2
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
176 _surrogate=0; // USED
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
177 i--;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
178 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
180 if ((code & 0xffffff80) == 0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
181 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
182 // 1b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
183 if (bytes>=buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
184 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
185 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
186 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
187 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
188 buffer[bytes++]=(byte)(code);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
189 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
190 else
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
191 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
192 if((code&0xfffff800)==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
193 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
194 // 2b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
195 if (bytes+2>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
196 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
197 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
198 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
199 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
200 buffer[bytes++]=(byte)(0xc0|(code>>6));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
201 buffer[bytes++]=(byte)(0x80|(code&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
202 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
203 else if((code&0xffff0000)==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
204 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
205 // 3b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
206 if (bytes+3>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
207 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
208 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
209 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
210 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
211 buffer[bytes++]=(byte)(0xe0|(code>>12));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
212 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
213 buffer[bytes++]=(byte)(0x80|(code&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
214 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
215 else if((code&0xff200000)==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
216 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
217 // 4b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
218 if (bytes+4>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
219 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
220 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
221 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
222 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
223 buffer[bytes++]=(byte)(0xf0|(code>>18));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
224 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
225 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
226 buffer[bytes++]=(byte)(0x80|(code&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
227 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
228 else if((code&0xf4000000)==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
229 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
230 // 5b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
231 if (bytes+5>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
232 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
233 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
234 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
235 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
236 buffer[bytes++]=(byte)(0xf8|(code>>24));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
237 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
238 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
239 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
240 buffer[bytes++]=(byte)(0x80|(code&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
241 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
242 else if((code&0x80000000)==0)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
243 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
244 // 6b
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
245 if (bytes+6>buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
246 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
247 chars=i;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
248 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
249 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
250 buffer[bytes++]=(byte)(0xfc|(code>>30));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
251 buffer[bytes++]=(byte)(0x80|((code>>24)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
252 buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
253 buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
254 buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
255 buffer[bytes++]=(byte)(0x80|(code&0x3f));
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
256 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
257 else
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
258 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
259 buffer[bytes++]=(byte)('?');
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
260 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
262 _surrogate=0; // USED
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
264 if (bytes==buffer.length)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
265 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
266 chars=i+1;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
267 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
268 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
269 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
270 }
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
271 _out._bytes.setCount(bytes);
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
272 break;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
273 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
274 default:
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
275 throw new IllegalStateException();
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
276 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
277
985
8fef34f665e7 remove HttpOutput
Franklin Schmidt <fschmidt@gmail.com>
parents: 984
diff changeset
278 _out._bytes.writeTo(_out);
984
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
279 length-=chars;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
280 offset+=chars;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
281 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
282 }
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
283
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
284 private Writer getConverter() throws IOException
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
285 {
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
286 if (_out._converter == null)
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
287 _out._converter = new OutputStreamWriter(_out._bytes, _out._characterEncoding);
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
288 return _out._converter;
7b0fa315e835 simplify HttpWriter
Franklin Schmidt <fschmidt@gmail.com>
parents: 983
diff changeset
289 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
290 }