01: package com.jclark.xml.tok;
02:
03: /**
04: * An <CODE>Encoding</CODE> for UTF-16 in little-endian byte order
05: * (least significant byte first).
06: * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:05 $
07: */
08: class UTF16LittleEndianEncoding extends Encoding {
09: UTF16LittleEndianEncoding() {
10: super (2);
11: }
12:
13: final int byteType(byte[] buf, int off) {
14: return charTypeTable[buf[off + 1] & 0xFF][buf[off] & 0xFF];
15: }
16:
17: // c is a significant ASCII character
18: final boolean charMatches(byte[] buf, int off, char c) {
19: return buf[off + 1] == 0 && (char) buf[off] == c;
20: }
21:
22: final int byteToAscii(byte[] buf, int off) {
23: return buf[off + 1] == 0 ? (char) buf[off] : -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);
31: c |= (sourceBuf[sourceStart++] & 0xFF) << 8;
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: switch (buf[off++]) {
48: case (byte) '\n':
49: if (buf[off++] == 0) {
50: lineNumber++;
51: colStart = off;
52: }
53: break;
54: case (byte) '\r':
55: if (buf[off++] == 0) {
56: if (off != end && buf[off] == '\n'
57: && buf[off + 1] == 0)
58: off += 2;
59: lineNumber++;
60: colStart = off;
61: }
62: break;
63: default:
64: off++;
65: break;
66: }
67: }
68: pos.lineNumber = lineNumber;
69: pos.columnNumber = (off - colStart) >> 1;
70: }
71:
72: Encoding getUTF16Encoding() {
73: return this;
74: }
75: }
|