01: package com.jclark.xml.tok;
02:
03: /**
04: * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:03 $
05: */
06: public final class Buffer {
07: private static final int INIT_SIZE = 64;
08: private char[] buf = new char[INIT_SIZE];
09: private int len;
10:
11: public void clear() {
12: len = 0;
13: }
14:
15: public void append(char c) {
16: need(1);
17: buf[len++] = c;
18: }
19:
20: public void appendRefCharPair(Token t) {
21: need(2);
22: t.getRefCharPair(buf, len);
23: len += 2;
24: }
25:
26: public void append(Encoding enc, byte[] bbuf, int start, int end) {
27: need((end - start) / enc.getMinBytesPerChar());
28: len += enc.convert(bbuf, start, end, buf, len);
29: }
30:
31: private void need(int n) {
32: if (len + n <= buf.length)
33: return;
34: char[] tem = buf;
35: if (n > tem.length)
36: buf = new char[n * 2];
37: else
38: buf = new char[tem.length << 1];
39: System.arraycopy(tem, 0, buf, 0, tem.length);
40: }
41:
42: public byte[] getBytes() {
43: byte[] text = new byte[len << 1];
44: int j = 0;
45: for (int i = 0; i < len; i++) {
46: char c = buf[i];
47: text[j++] = (byte) (c >> 8);
48: text[j++] = (byte) (c & 0xFF);
49: }
50: return text;
51: }
52:
53: public String toString() {
54: return new String(buf, 0, len);
55: }
56:
57: public int length() {
58: return len;
59: }
60:
61: public char charAt(int i) {
62: if (i >= len)
63: throw new IndexOutOfBoundsException();
64: return buf[i];
65: }
66:
67: public void chop() {
68: --len;
69: }
70: }
|