001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.process.extended;
010:
011: public class CharBuffer {
012: public CharBufferEntry head = null;
013: public CharBufferEntry tail = null;
014: public CharBufferEntry current = null;
015: public int position;
016:
017: public void push(char[] text, int position, int length) {
018: if (head == null) {
019: head = new CharBufferEntry(text, position, length, null);
020: tail = head;
021: current = head;
022: this .position = position;
023: } else {
024: tail = new CharBufferEntry(text, position, length, tail);
025:
026: if ((this .position == (current.position + current.length))
027: && (current.next == tail)) {
028: current = current.next;
029: this .position = current.position;
030: }
031: }
032: }
033:
034: public char read() {
035: if ((position < (current.position + current.length))
036: && (current != null)) {
037: char c = current.text[position];
038:
039: position++;
040: if ((position == (current.position + current.length))
041: && (current.next != null)) {
042: current = current.next;
043: position = current.position;
044: }
045:
046: return c;
047: } else
048: throw new IllegalStateException("Buffer is empty");
049: }
050:
051: public char peek() {
052: if ((position < (current.position + current.length))
053: && (current != null))
054: return current.text[position];
055: else
056: throw new IllegalStateException("Buffer is empty");
057: }
058:
059: public boolean available() {
060: return (current != null)
061: && (position < (current.position + current.length));
062: }
063:
064: public int remaining() {
065: int remaining = (current.position + current.length) - position;
066: CharBufferEntry next = current.next;
067: while (next != null) {
068: remaining += next.length;
069: next = next.next;
070: }
071:
072: return remaining;
073: }
074:
075: public void back() {
076: if (position > current.position)
077: position--;
078: else if (current.previous != null) {
079: current = current.previous;
080: position = (current.position + current.length) - 1;
081: } else
082: throw new IllegalStateException("Couldn't move back");
083: }
084:
085: public void clear() {
086: head = null;
087: tail = null;
088: current = null;
089: }
090:
091: public class CharBufferEntry {
092: public char[] text;
093: public int position;
094: public int length;
095: public CharBufferEntry previous = null;
096: public CharBufferEntry next = null;
097:
098: public CharBufferEntry(char[] text, int position, int length,
099: CharBufferEntry previous) {
100: this.text = text;
101: this.position = position;
102: this.length = length;
103: this.previous = previous;
104: if (previous != null)
105: previous.next = this;
106: }
107: }
108: }
|