| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import java.io.IOException; |
| 11 |
import java.io.OutputStream; |
| 12 |
import java.io.Writer; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public final class UTF8Writer |
| 24 |
extends Writer { |
| 25 |
private final static int MIN_BUF_LEN = 32; |
| 26 |
private final static int DEFAULT_BUF_LEN = 4000; |
| 27 |
|
| 28 |
final static int SURR1_FIRST = 0xD800; |
| 29 |
final static int SURR1_LAST = 0xDBFF; |
| 30 |
final static int SURR2_FIRST = 0xDC00; |
| 31 |
final static int SURR2_LAST = 0xDFFF; |
| 32 |
|
| 33 |
protected OutputStream _out; |
| 34 |
|
| 35 |
protected byte[] _outBuffer; |
| 36 |
|
| 37 |
final protected int _outBufferLast; |
| 38 |
|
| 39 |
protected int _outPtr; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
int _surrogate = 0; |
| 47 |
|
| 48 |
public UTF8Writer(OutputStream out) { |
| 49 |
this(out, DEFAULT_BUF_LEN); |
| 50 |
} |
| 51 |
|
| 52 |
public UTF8Writer(OutputStream out, int bufferLength) { |
| 53 |
if (bufferLength < MIN_BUF_LEN) { |
| 54 |
bufferLength = MIN_BUF_LEN; |
| 55 |
} |
| 56 |
_out = out; |
| 57 |
_outBuffer = new byte[bufferLength]; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
_outBufferLast = bufferLength - 4; |
| 62 |
_outPtr = 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public void close() throws IOException { |
| 85 |
if (_out != null) { |
| 86 |
_flushBuffer(); |
| 87 |
_outBuffer = null; |
| 88 |
_out.close(); |
| 89 |
_out = null; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (_surrogate != 0) { |
| 95 |
int code = _surrogate; |
| 96 |
|
| 97 |
_surrogate = 0; |
| 98 |
throwIllegal(code); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public void flush() throws IOException { |
| 104 |
_flushBuffer(); |
| 105 |
_out.flush(); |
| 106 |
} |
| 107 |
|
| 108 |
public void write(char[] cbuf) throws IOException { |
| 109 |
write(cbuf, 0, cbuf.length); |
| 110 |
} |
| 111 |
|
| 112 |
public void write(char[] cbuf, int off, int len) |
| 113 |
throws IOException { |
| 114 |
if (len < 2) { |
| 115 |
if (len == 1) { |
| 116 |
write(cbuf[off]); |
| 117 |
} |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
if (_surrogate > 0) { |
| 123 |
char second = cbuf[off++]; |
| 124 |
--len; |
| 125 |
write(_convertSurrogate(second)); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
int outPtr = _outPtr; |
| 130 |
byte[] outBuf = _outBuffer; |
| 131 |
int outBufLast = _outBufferLast; |
| 132 |
|
| 133 |
|
| 134 |
len += off; |
| 135 |
|
| 136 |
output_loop: |
| 137 |
for (; off < len; ) { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if (outPtr >= outBufLast) { |
| 142 |
_out.write(outBuf, 0, outPtr); |
| 143 |
outPtr = 0; |
| 144 |
} |
| 145 |
|
| 146 |
int c = cbuf[off++]; |
| 147 |
|
| 148 |
if (c < 0x80) { |
| 149 |
outBuf[outPtr++] = (byte) c; |
| 150 |
|
| 151 |
int maxInCount = (len - off); |
| 152 |
int maxOutCount = (outBufLast - outPtr); |
| 153 |
|
| 154 |
if (maxInCount > maxOutCount) { |
| 155 |
maxInCount = maxOutCount; |
| 156 |
} |
| 157 |
maxInCount += off; |
| 158 |
ascii_loop: |
| 159 |
while (true) { |
| 160 |
if (off >= maxInCount) { |
| 161 |
continue output_loop; |
| 162 |
} |
| 163 |
c = cbuf[off++]; |
| 164 |
if (c >= 0x80) { |
| 165 |
break ascii_loop; |
| 166 |
} |
| 167 |
outBuf[outPtr++] = (byte) c; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if (c < 0x800) { |
| 173 |
outBuf[outPtr++] = (byte) (0xc0 | (c >> 6)); |
| 174 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 175 |
} else { |
| 176 |
|
| 177 |
if (c < SURR1_FIRST || c > SURR2_LAST) { |
| 178 |
outBuf[outPtr++] = (byte) (0xe0 | (c >> 12)); |
| 179 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 180 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
if (c > SURR1_LAST) { |
| 185 |
_outPtr = outPtr; |
| 186 |
throwIllegal(c); |
| 187 |
} |
| 188 |
_surrogate = c; |
| 189 |
|
| 190 |
if (off >= len) { |
| 191 |
break; |
| 192 |
} |
| 193 |
c = _convertSurrogate(cbuf[off++]); |
| 194 |
if (c > 0x10FFFF) { |
| 195 |
_outPtr = outPtr; |
| 196 |
throwIllegal(c); |
| 197 |
} |
| 198 |
outBuf[outPtr++] = (byte) (0xf0 | (c >> 18)); |
| 199 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 12) & 0x3f)); |
| 200 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 201 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 202 |
} |
| 203 |
} |
| 204 |
_outPtr = outPtr; |
| 205 |
} |
| 206 |
|
| 207 |
public void write(int c) |
| 208 |
throws IOException { |
| 209 |
|
| 210 |
if (_surrogate > 0) { |
| 211 |
c = _convertSurrogate(c); |
| 212 |
|
| 213 |
} else if (c >= SURR1_FIRST && c <= SURR2_LAST) { |
| 214 |
|
| 215 |
if (c > SURR1_LAST) { |
| 216 |
throwIllegal(c); |
| 217 |
} |
| 218 |
|
| 219 |
_surrogate = c; |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
if (_outPtr >= _outBufferLast) { |
| 224 |
_flushBuffer(); |
| 225 |
} |
| 226 |
|
| 227 |
if (c < 0x80) { |
| 228 |
_outBuffer[_outPtr++] = (byte) c; |
| 229 |
} else { |
| 230 |
int ptr = _outPtr; |
| 231 |
if (c < 0x800) { |
| 232 |
_outBuffer[ptr++] = (byte) (0xc0 | (c >> 6)); |
| 233 |
_outBuffer[ptr++] = (byte) (0x80 | (c & 0x3f)); |
| 234 |
} else if (c <= 0xFFFF) { |
| 235 |
_outBuffer[ptr++] = (byte) (0xe0 | (c >> 12)); |
| 236 |
_outBuffer[ptr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 237 |
_outBuffer[ptr++] = (byte) (0x80 | (c & 0x3f)); |
| 238 |
} else { |
| 239 |
if (c > 0x10FFFF) { |
| 240 |
throwIllegal(c); |
| 241 |
} |
| 242 |
_outBuffer[ptr++] = (byte) (0xf0 | (c >> 18)); |
| 243 |
_outBuffer[ptr++] = (byte) (0x80 | ((c >> 12) & 0x3f)); |
| 244 |
_outBuffer[ptr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 245 |
_outBuffer[ptr++] = (byte) (0x80 | (c & 0x3f)); |
| 246 |
} |
| 247 |
_outPtr = ptr; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
public void write(String str) |
| 252 |
throws IOException { |
| 253 |
write(str, 0, str.length()); |
| 254 |
} |
| 255 |
|
| 256 |
public void write(String str, int off, int len) |
| 257 |
throws IOException { |
| 258 |
if (len < 2) { |
| 259 |
if (len == 1) { |
| 260 |
write(str.charAt(off)); |
| 261 |
} |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
if (_surrogate > 0) { |
| 267 |
char second = str.charAt(off++); |
| 268 |
--len; |
| 269 |
write(_convertSurrogate(second)); |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
int outPtr = _outPtr; |
| 274 |
byte[] outBuf = _outBuffer; |
| 275 |
int outBufLast = _outBufferLast; |
| 276 |
|
| 277 |
|
| 278 |
len += off; |
| 279 |
|
| 280 |
output_loop: |
| 281 |
for (; off < len; ) { |
| 282 |
|
| 283 |
|
| 284 |
if (outPtr >= outBufLast) { |
| 285 |
_out.write(outBuf, 0, outPtr); |
| 286 |
outPtr = 0; |
| 287 |
} |
| 288 |
|
| 289 |
int c = str.charAt(off++); |
| 290 |
|
| 291 |
if (c < 0x80) { |
| 292 |
outBuf[outPtr++] = (byte) c; |
| 293 |
|
| 294 |
int maxInCount = (len - off); |
| 295 |
int maxOutCount = (outBufLast - outPtr); |
| 296 |
|
| 297 |
if (maxInCount > maxOutCount) { |
| 298 |
maxInCount = maxOutCount; |
| 299 |
} |
| 300 |
maxInCount += off; |
| 301 |
ascii_loop: |
| 302 |
while (true) { |
| 303 |
if (off >= maxInCount) { |
| 304 |
continue output_loop; |
| 305 |
} |
| 306 |
c = str.charAt(off++); |
| 307 |
if (c >= 0x80) { |
| 308 |
break ascii_loop; |
| 309 |
} |
| 310 |
outBuf[outPtr++] = (byte) c; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
if (c < 0x800) { |
| 316 |
outBuf[outPtr++] = (byte) (0xc0 | (c >> 6)); |
| 317 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 318 |
} else { |
| 319 |
|
| 320 |
if (c < SURR1_FIRST || c > SURR2_LAST) { |
| 321 |
outBuf[outPtr++] = (byte) (0xe0 | (c >> 12)); |
| 322 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 323 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 324 |
continue; |
| 325 |
} |
| 326 |
|
| 327 |
if (c > SURR1_LAST) { |
| 328 |
_outPtr = outPtr; |
| 329 |
throwIllegal(c); |
| 330 |
} |
| 331 |
_surrogate = c; |
| 332 |
|
| 333 |
if (off >= len) { |
| 334 |
break; |
| 335 |
} |
| 336 |
c = _convertSurrogate(str.charAt(off++)); |
| 337 |
if (c > 0x10FFFF) { |
| 338 |
_outPtr = outPtr; |
| 339 |
throwIllegal(c); |
| 340 |
} |
| 341 |
outBuf[outPtr++] = (byte) (0xf0 | (c >> 18)); |
| 342 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 12) & 0x3f)); |
| 343 |
outBuf[outPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f)); |
| 344 |
outBuf[outPtr++] = (byte) (0x80 | (c & 0x3f)); |
| 345 |
} |
| 346 |
} |
| 347 |
_outPtr = outPtr; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private void _flushBuffer() throws IOException { |
| 357 |
if (_outPtr > 0 && _outBuffer != null) { |
| 358 |
_out.write(_outBuffer, 0, _outPtr); |
| 359 |
_outPtr = 0; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private final int _convertSurrogate(int secondPart) |
| 367 |
throws IOException { |
| 368 |
int firstPart = _surrogate; |
| 369 |
_surrogate = 0; |
| 370 |
|
| 371 |
|
| 372 |
if (secondPart < SURR2_FIRST || secondPart > SURR2_LAST) { |
| 373 |
throw new IOException("Broken surrogate pair: first char 0x" + Integer.toHexString(firstPart) + ", second 0x" + Integer.toHexString(secondPart) + "; illegal combination"); |
| 374 |
} |
| 375 |
return 0x10000 + ((firstPart - SURR1_FIRST) << 10) + (secondPart - SURR2_FIRST); |
| 376 |
} |
| 377 |
|
| 378 |
private void throwIllegal(int code) |
| 379 |
throws IOException { |
| 380 |
if (code > 0x10FFFF) { |
| 381 |
throw new IOException("Illegal character point (0x" + Integer.toHexString(code) + ") to output; max is 0x10FFFF as per RFC 3629"); |
| 382 |
} |
| 383 |
if (code >= SURR1_FIRST) { |
| 384 |
if (code <= SURR1_LAST) { |
| 385 |
throw new IOException("Unmatched first part of surrogate pair (0x" + Integer.toHexString(code) + ")"); |
| 386 |
} |
| 387 |
throw new IOException("Unmatched second part of surrogate pair (0x" + Integer.toHexString(code) + ")"); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
throw new IOException("Illegal character point (0x" + Integer.toHexString(code) + ") to output"); |
| 392 |
} |
| 393 |
} |
| 394 |
|