Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 251e3d0fd4c59afa229dfcc9169c37ca42f9e228 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;

/** OutputWriter.
 * A writer that can wrap a {@link HttpOutput} stream and provide
 * character encodings.
 *
 * The UTF-8 encoding is done by this class and no additional
 * buffers or Writers are used.
 * The UTF-8 code was inspired by http://javolution.org
 */
public class Utf8HttpWriter extends HttpWriter
{
    int _surrogate=0;

    /* ------------------------------------------------------------ */
    public Utf8HttpWriter(HttpOutput out)
    {
        super(out);
    }

    /* ------------------------------------------------------------ */
    @Override
    public void write (char[] s,int offset, int length) throws IOException
    {
        HttpOutput out = _out;
        if (length==0)
        {
            if (_out.isAllContentWritten())
                close();
        }
        
        while (length > 0)
        {
            _bytes.reset();
            int chars = length>MAX_OUTPUT_CHARS?MAX_OUTPUT_CHARS:length;

            byte[] buffer=_bytes.getBuf();
            int bytes=_bytes.getCount();

            if (bytes+chars>buffer.length)
                chars=buffer.length-bytes;

            for (int i = 0; i < chars; i++)
            {
                int code = s[offset+i];

                // Do we already have a surrogate?
                if(_surrogate==0)
                {
                    // No - is this char code a surrogate?
                    if(Character.isHighSurrogate((char)code))
                    {
                        _surrogate=code; // UCS-?
                        continue;
                    }
                }
                // else handle a low surrogate
                else if(Character.isLowSurrogate((char)code))
                {
                    code = Character.toCodePoint((char)_surrogate, (char)code); // UCS-4
                }
                // else UCS-2
                else
                {
                    code=_surrogate; // UCS-2
                    _surrogate=0; // USED
                    i--;
                }

                if ((code & 0xffffff80) == 0)
                {
                    // 1b
                    if (bytes>=buffer.length)
                    {
                        chars=i;
                        break;
                    }
                    buffer[bytes++]=(byte)(code);
                }
                else
                {
                    if((code&0xfffff800)==0)
                    {
                        // 2b
                        if (bytes+2>buffer.length)
                        {
                            chars=i;
                            break;
                        }
                        buffer[bytes++]=(byte)(0xc0|(code>>6));
                        buffer[bytes++]=(byte)(0x80|(code&0x3f));
                    }
                    else if((code&0xffff0000)==0)
                    {
                        // 3b
                        if (bytes+3>buffer.length)
                        {
                            chars=i;
                            break;
                        }
                        buffer[bytes++]=(byte)(0xe0|(code>>12));
                        buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
                        buffer[bytes++]=(byte)(0x80|(code&0x3f));
                    }
                    else if((code&0xff200000)==0)
                    {
                        // 4b
                        if (bytes+4>buffer.length)
                        {
                            chars=i;
                            break;
                        }
                        buffer[bytes++]=(byte)(0xf0|(code>>18));
                        buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
                        buffer[bytes++]=(byte)(0x80|(code&0x3f));
                    }
                    else if((code&0xf4000000)==0)
                    {
                        // 5b
                        if (bytes+5>buffer.length)
                        {
                            chars=i;
                            break;
                        }
                        buffer[bytes++]=(byte)(0xf8|(code>>24));
                        buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
                        buffer[bytes++]=(byte)(0x80|(code&0x3f));
                    }
                    else if((code&0x80000000)==0)
                    {
                        // 6b
                        if (bytes+6>buffer.length)
                        {
                            chars=i;
                            break;
                        }
                        buffer[bytes++]=(byte)(0xfc|(code>>30));
                        buffer[bytes++]=(byte)(0x80|((code>>24)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>18)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>12)&0x3f));
                        buffer[bytes++]=(byte)(0x80|((code>>6)&0x3f));
                        buffer[bytes++]=(byte)(0x80|(code&0x3f));
                    }
                    else
                    {
                        buffer[bytes++]=(byte)('?');
                    }

                    _surrogate=0; // USED

                    if (bytes==buffer.length)
                    {
                        chars=i+1;
                        break;
                    }
                }
            }
            _bytes.setCount(bytes);

            _bytes.writeTo(out);
            length-=chars;
            offset+=chars;
        }
    }
}

Back to the top