KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > buffer > BufferReader


1 package gnu.jemacs.buffer;
2 import gnu.mapping.*;
3 import java.io.*;
4 import gnu.lists.CharBuffer;
5
6 public class BufferReader extends InPort
7 {
8   CharBuffer content;
9   int rangeStart;
10   int rangeLength;
11
12   public BufferReader(CharBuffer content, String JavaDoc name, int start, int count)
13   {
14     super(gnu.text.NullReader.nullReader, name);
15     this.content = content;
16     buffer = content.getArray();
17     rangeStart = start;
18     rangeLength = count;
19     if (start < content.gapStart)
20       {
21     pos = start;
22     limit = start + count;
23     if (limit > content.gapStart)
24       limit = content.gapStart;
25       }
26     else
27       {
28     int gapSize = content.gapEnd - content.gapStart;
29     pos = start + gapSize;
30     int length = content.getArray().length;
31     limit = pos + count > length ? length : pos + count;
32       }
33   }
34
35   public int read ()
36   {
37     if (pos < limit)
38       return buffer[pos++];
39     if (limit == content.gapStart)
40       {
41     int gapSize = content.gapEnd - content.gapStart;
42     pos = content.gapEnd;
43     int count = rangeLength - (content.gapStart - rangeStart);
44     int length = content.getArray().length;
45     limit = pos + count > length ? length : pos + count;
46     if (pos < limit)
47       return buffer[pos++];
48       }
49     return -1;
50   }
51
52   // int highestPos - is not used
53
// public synchronized void mark (int readAheadLimit) - seems OK.
54
// public boolean ready () -- seems OK
55

56   public void reset () throws IOException
57   {
58     if (readAheadLimit <= 0)
59       throw new IOException ("mark invalid");
60     if (pos >= content.gapEnd && markPos <= content.gapStart)
61       limit = content.gapEnd;
62     pos = markPos;
63     readAheadLimit = 0;
64   }
65
66   public int getLineNumber ()
67   {
68     throw new Error JavaDoc("BufferReader.getLineNumber - not implemented");
69   }
70
71   public int getColumnNumber ()
72   {
73     throw new Error JavaDoc("BufferReader.getColumnNumber - not implemented");
74   }
75
76 }
77
Popular Tags