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), but with newlines already normalized to U+000A.
06: * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:04 $
07: */
08: class InternalEncoding extends Encoding {
09: private static final byte[][] internalCharTypeTable = new byte[256][];
10:
11: InternalEncoding() {
12: super (2);
13: }
14:
15: final int byteType(byte[] buf, int off) {
16: return internalCharTypeTable[buf[off] & 0xFF][buf[off + 1] & 0xFF];
17: }
18:
19: // c is a significant ASCII character
20: final boolean charMatches(byte[] buf, int off, char c) {
21: return buf[off] == 0 && (char) buf[off + 1] == c;
22: }
23:
24: final int byteToAscii(byte[] buf, int off) {
25: return buf[off] == 0 ? (char) buf[off + 1] : -1;
26: }
27:
28: public int convert(byte[] sourceBuf, int sourceStart,
29: int sourceEnd, char[] targetBuf, int targetStart) {
30: int origTargetStart = targetStart;
31: while (sourceStart != sourceEnd) {
32: int c = (sourceBuf[sourceStart++] & 0xFF) << 8;
33: c |= sourceBuf[sourceStart++] & 0xFF;
34: targetBuf[targetStart++] = (char) c;
35: }
36: return targetStart - origTargetStart;
37: }
38:
39: public int getFixedBytesPerChar() {
40: return 2;
41: }
42:
43: public void movePosition(final byte[] buf, int off, int end,
44: Position pos) {
45: int lineNumber = pos.lineNumber;
46: /* Maintain invariant: off - colStart = colNumber*2 */
47: int colStart = off - (pos.columnNumber << 1);
48: while (off != end) {
49: if (buf[off] == 0) {
50: off++;
51: if (buf[off++] == (byte) '\n') {
52: lineNumber++;
53: colStart = off;
54: }
55: } else
56: off += 2;
57: }
58: pos.lineNumber = lineNumber;
59: pos.columnNumber = (off - colStart) >> 1;
60: }
61:
62: static {
63: System.arraycopy(charTypeTable, 0, internalCharTypeTable, 0,
64: 256);
65: internalCharTypeTable[0] = new byte[256];
66: System.arraycopy(charTypeTable[0], 0, internalCharTypeTable[0],
67: 0, 256);
68: internalCharTypeTable[0]['\r'] = BT_S;
69: }
70:
71: }
|