01: // Port of the ChannelBuffer struct from tclIO.h/tclIO.c
02: // and associated functionality
03:
04: package tcl.lang;
05:
06: class ChannelBuffer {
07:
08: // The next position into which a character
09: // will be put in the buffer.
10:
11: int nextAdded;
12:
13: // Position of next byte to be removed
14: // from the buffer.
15:
16: int nextRemoved;
17:
18: // How big is the buffer?
19:
20: int bufLength;
21:
22: // Next buffer in chain.
23:
24: ChannelBuffer next;
25:
26: // The actual bytes stored in the buffer
27:
28: byte[] buf;
29:
30: // A channel buffer has BUFFER_PADDING bytes extra at beginning to
31: // hold any bytes of a native-encoding character that got split by
32: // the end of the previous buffer and need to be moved to the
33: // beginning of the next buffer to make a contiguous string so it
34: // can be converted to UTF-8.
35: //
36: // A channel buffer has BUFFER_PADDING bytes extra at the end to
37: // hold any bytes of a native-encoding character (generated from a
38: // UTF-8 character) that overflow past the end of the buffer and
39: // need to be moved to the next buffer.
40:
41: final static int BUFFER_PADDING = 16;
42:
43: /**
44: * AllocChannelBuffer -> ChannelBuffer
45: *
46: * Create a new ChannelBuffer object
47: */
48:
49: ChannelBuffer(int length) {
50: int n;
51:
52: n = length + BUFFER_PADDING + BUFFER_PADDING;
53: buf = new byte[n];
54: nextAdded = BUFFER_PADDING;
55: nextRemoved = BUFFER_PADDING;
56: bufLength = length + BUFFER_PADDING;
57: next = null;
58: }
59:
60: // Generate debug output that describes the contents
61: // of this ChannelBuffer object.
62:
63: public String toString() {
64: int numBytes = nextAdded - nextRemoved;
65:
66: StringBuffer sb = new StringBuffer(256);
67: sb.append("ChannelBuffer contains " + numBytes + " bytes"
68: + "\n");
69:
70: for (int i = 0; i < numBytes; i++) {
71: int ival = buf[nextRemoved + i];
72: String srep;
73: if (((char) ival) == '\r') {
74: srep = "\\r";
75: } else if (((char) ival) == '\n') {
76: srep = "\\n";
77: } else {
78: srep = "" + ((char) ival);
79: }
80:
81: sb.append("bytes[" + i + "] = '" + srep + "'" + ", (int) "
82: + ival + " , " + "0x" + Integer.toHexString(ival)
83: + "\n");
84: }
85: return sb.toString();
86: }
87: }
|