File: UTF16LittleEndianEncoding.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 (73 lines) | stat: -rw-r--r-- 1,916 bytes parent folder | download | duplicates (3)
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
package com.jclark.xml.tok;

/**
 * An <CODE>Encoding</CODE> for UTF-16 in little-endian byte order
 * (least significant byte first).
 * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:05 $
 */
class UTF16LittleEndianEncoding extends Encoding {
  UTF16LittleEndianEncoding() {
    super(2);
  }

  final int byteType(byte[] buf, int off) {
    return charTypeTable[buf[off + 1] & 0xFF][buf[off] & 0xFF];
  }

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

  final int byteToAscii(byte[] buf, int off) {
    return buf[off + 1] == 0 ? (char)buf[off] : -1;
  }

  public int convert(byte[] sourceBuf, int sourceStart, int sourceEnd,
		     char[] targetBuf, int targetStart) {
    int origTargetStart = targetStart;
    while (sourceStart != sourceEnd) {
      int c = (sourceBuf[sourceStart++] & 0xFF);
      c |= (sourceBuf[sourceStart++] & 0xFF) << 8;
      targetBuf[targetStart++] = (char)c;
    }
    return targetStart - origTargetStart;
  }

  public int getFixedBytesPerChar() {
    return 2;
  }

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

  Encoding getUTF16Encoding() {
    return this;
  }
}