KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > process > extended > CharBuffer


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.process.extended;
10
11 public class CharBuffer
12 {
13   public CharBufferEntry head = null;
14   public CharBufferEntry tail = null;
15   public CharBufferEntry current = null;
16   public int position;
17
18   public void push(char[] text, int position, int length)
19   {
20     if (head==null)
21     {
22       head = new CharBufferEntry(text, position, length, null);
23       tail = head;
24       current = head;
25       this.position = position;
26     }
27     else
28     {
29       tail = new CharBufferEntry(text, position, length, tail);
30
31       if ((this.position==(current.position+current.length)) && (current.next==tail))
32       {
33         current = current.next;
34         this.position = current.position;
35       }
36     }
37   }
38
39   public char read()
40   {
41     if ((position<(current.position+current.length)) && (current!=null))
42     {
43       char c = current.text[position];
44
45       position++;
46       if ((position==(current.position+current.length)) && (current.next!=null))
47       {
48         current = current.next;
49         position = current.position;
50       }
51
52       return c;
53     }
54     else
55       throw new IllegalStateException JavaDoc("Buffer is empty");
56   }
57
58   public char peek()
59   {
60     if ((position<(current.position+current.length)) && (current!=null))
61       return current.text[position];
62     else
63       throw new IllegalStateException JavaDoc("Buffer is empty");
64   }
65
66   public boolean available()
67   {
68     return (current!=null) && (position<(current.position+current.length));
69   }
70
71   public int remaining()
72   {
73     int remaining = (current.position+current.length)-position;
74     CharBufferEntry next = current.next;
75     while (next!=null)
76     {
77       remaining += next.length;
78       next = next.next;
79     }
80
81     return remaining;
82   }
83
84   public void back()
85   {
86     if (position>current.position)
87       position--;
88     else if (current.previous!=null)
89     {
90       current = current.previous;
91       position = (current.position+current.length)-1;
92     }
93     else
94       throw new IllegalStateException JavaDoc("Couldn't move back");
95   }
96
97   public void clear()
98   {
99     head = null;
100     tail = null;
101     current = null;
102   }
103
104   public class CharBufferEntry
105   {
106     public char[] text;
107     public int position;
108     public int length;
109     public CharBufferEntry previous = null;
110     public CharBufferEntry next = null;
111
112     public CharBufferEntry(char[] text, int position, int length, CharBufferEntry previous)
113     {
114       this.text = text;
115       this.position = position;
116       this.length = length;
117       this.previous = previous;
118       if (previous!=null)
119         previous.next = this;
120     }
121   }
122 }
123
Popular Tags