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