01: package com.jclark.xml.tok;
02:
03: /**
04: * An <CODE>Encoding</CODE> for UTF-16 in big-endian byte order
05: * (network byte order)
06: * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:05 $
07: */
08: class UTF16BigEndianEncoding extends Encoding {
09: UTF16BigEndianEncoding() {
10: super (2);
11: }
12:
13: final int byteType(byte[] buf, int off) {
14: return charTypeTable[buf[off] & 0xFF][buf[off + 1] & 0xFF];
15: }
16:
17: // c is a significant ASCII character
18: final boolean charMatches(byte[] buf, int off, char c) {
19: return buf[off] == 0 && (char) buf[off + 1] == c;
20: }
21:
22: final int byteToAscii(byte[] buf, int off) {
23: return buf[off] == 0 ? (char) buf[off + 1] : -1;
24: }
25:
26: public int convert(byte[] sourceBuf, int sourceStart,
27: int sourceEnd, char[] targetBuf, int targetStart) {
28: int origTargetStart = targetStart;
29: while (sourceStart != sourceEnd) {
30: int c = (sourceBuf[sourceStart++] & 0xFF) << 8;
31: c |= sourceBuf[sourceStart++] & 0xFF;
32: targetBuf[targetStart++] = (char) c;
33: }
34: return targetStart - origTargetStart;
35: }
36:
37: public int getFixedBytesPerChar() {
38: return 2;
39: }
40:
41: public void movePosition(final byte[] buf, int off, int end,
42: Position pos) {
43: int lineNumber = pos.lineNumber;
44: /* Maintain invariant: off - colStart = colNumber*2 */
45: int colStart = off - (pos.columnNumber << 1);
46: while (off != end) {
47: if (buf[off] == 0) {
48: off++;
49: switch (buf[off++]) {
50: case (byte) '\n':
51: lineNumber++;
52: colStart = off;
53: break;
54: case (byte) '\r':
55: if (off != end && buf[off] == 0
56: && buf[off + 1] == '\n')
57: off += 2;
58: lineNumber++;
59: colStart = off;
60: break;
61: }
62: } else
63: off += 2;
64: }
65: pos.lineNumber = lineNumber;
66: pos.columnNumber = (off - colStart) >> 1;
67: }
68:
69: Encoding getUTF16Encoding() {
70: return this;
71: }
72: }
|