File: SingleByteEncoding.java

package info (click to toggle)
lib-xp-java 0.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,652 kB
  • ctags: 2,424
  • sloc: java: 8,085; makefile: 53; sh: 17; xml: 7
file content (86 lines) | stat: -rw-r--r-- 2,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.jclark.xml.tok;

/**
 * An <CODE>Encoding</CODE> for an arbitrary encoding
 * that represents every character by exactly one byte.
 *
 * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:04 $
 */
final class SingleByteEncoding extends Encoding {

  private final byte[] byteTypeTable = new byte[256];
  private final int[] asciiToByte = new int[128];
  private final String map;

  SingleByteEncoding(String map) {
    super(1);
    this.map = map;
    for (int i = 0; i < 256; i++)
      asciiToByte[i] = 0xFF;
    for (int i = 0; i < 256; i++) {
      char c = map.charAt(i);
      if (c != 0xFFFD) {
	byteTypeTable[i] = charTypeTable[c >> 8][c & 0xFF];
	  if (c < 128)
	    asciiToByte[c] = (byte)i;
      }
      else
	byteTypeTable[i] = (byte) BT_MALFORM;
    }
  }

  int byteType(byte[] buf, int off) {
    return byteTypeTable[buf[off] & 0xFF];
  }

  int byteToAscii(byte[] buf, int off) {
    return map.charAt(off);
  }

  // c is a significant ASCII character
  boolean charMatches(byte[] buf, int off, char c) {
    return asciiToByte[c] == buf[off];
  }

  public int convert(byte[] sourceBuf, int sourceStart, int sourceEnd,
		     char[] targetBuf, int targetStart) {
    int initTargetStart = targetStart;
    int c;
    while (sourceStart != sourceEnd) 
      targetBuf[targetStart++] = map.charAt(sourceBuf[sourceStart++] & 0xFF);
    return targetStart - initTargetStart;
  }

  public int getFixedBytesPerChar() {
    return 1;
  }

  public void movePosition(final byte[] buf, int off, int end, Position pos) {
    /* Maintain the invariant: off - colStart == colNumber. */
    int colStart = off - pos.columnNumber;
    int lineNumber = pos.lineNumber;
    while (off != end) {
      switch (byteTypeTable[buf[off++] & 0xFF]) {
      case BT_CR:
	lineNumber += 1;
	colStart = off;
	break;
      case BT_LF:
	lineNumber += 1;
	if (off != end && buf[off] == asciiToByte['\n'])
	  off++;
	colStart = off;
	break;
      }
    }
    pos.columnNumber = off - colStart;
    pos.lineNumber = lineNumber;
  }

  int extendData(final byte[] buf, int off, final int end) {
    while (off != end && byteTypeTable[buf[off] & 0xFF] >= 0)
      off++;
    return off;
  }

}