1 | /*
|
---|
2 | * 02/24/2004
|
---|
3 | *
|
---|
4 | * DocumentReader.java - A reader for javax.swing.text.Document
|
---|
5 | * objects.
|
---|
6 | *
|
---|
7 | * This library is distributed under a modified BSD license. See the included
|
---|
8 | * RSyntaxTextArea.License.txt file for details.
|
---|
9 | */
|
---|
10 | package org.fife.io;
|
---|
11 |
|
---|
12 | import java.io.Reader;
|
---|
13 | import javax.swing.text.BadLocationException;
|
---|
14 | import javax.swing.text.Document;
|
---|
15 | import javax.swing.text.Segment;
|
---|
16 |
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A <code>Reader</code> for <code>javax.swing.text.Document</code> objects.
|
---|
20 | *
|
---|
21 | * @author Robert Futrell
|
---|
22 | * @version 1.0
|
---|
23 | */
|
---|
24 | public class DocumentReader extends Reader {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The stream's position in the document.
|
---|
28 | */
|
---|
29 | private long position;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A remembered position in the document.
|
---|
33 | */
|
---|
34 | private long mark;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The document we're working on.
|
---|
38 | */
|
---|
39 | private Document document;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Used for fast character access.
|
---|
43 | */
|
---|
44 | private Segment segment;
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructor.
|
---|
49 | *
|
---|
50 | * @param document The document we're 'reading'.
|
---|
51 | */
|
---|
52 | public DocumentReader(Document document) {
|
---|
53 | position = 0;
|
---|
54 | mark = -1;
|
---|
55 | this.document = document;
|
---|
56 | this.segment = new Segment();
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This currently does nothing...
|
---|
62 | */
|
---|
63 | public void close() {
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Marks the present position in the stream. Subsequent calls to
|
---|
69 | * <code>reset()</code> will reposition the stream to this point.
|
---|
70 | *
|
---|
71 | * @param readAheadLimit Ignored.
|
---|
72 | */
|
---|
73 | public void mark(int readAheadLimit) {
|
---|
74 | mark = position;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Tells whether this reader supports the <code>mark</code> operation.
|
---|
80 | * This always returns <code>true</code> for <code>DocumentReader</code>.
|
---|
81 | */
|
---|
82 | public boolean markSupported() {
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Reads the single character at the current position in the document.
|
---|
89 | */
|
---|
90 | public int read() {
|
---|
91 | if(position>=document.getLength()) {
|
---|
92 | return -1; // Read past end of document.
|
---|
93 | }
|
---|
94 | try {
|
---|
95 | document.getText((int)position,1, segment);
|
---|
96 | position++;
|
---|
97 | return segment.array[segment.offset];
|
---|
98 | } catch (BadLocationException ble) {
|
---|
99 | /* Should never happen?? */
|
---|
100 | ble.printStackTrace();
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Read <code>array.length</code> characters from the beginning
|
---|
108 | * of the document into <code>array</code>.
|
---|
109 | *
|
---|
110 | * @param array The array to read characters into.
|
---|
111 | * @return The number of characters read.
|
---|
112 | */
|
---|
113 | public int read(char array[]) {
|
---|
114 | return read(array, 0, array.length);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Reads characters into a portion of an array.
|
---|
120 | *
|
---|
121 | * @param cbuf The destination buffer.
|
---|
122 | * @param off Offset at which to start storing characters.
|
---|
123 | * @param len Maximum number of characters to read.
|
---|
124 | * @return The number of characters read, or <code>-1</code> if the
|
---|
125 | * end of the stream (document) has been reached.
|
---|
126 | */
|
---|
127 | public int read(char cbuf[], int off, int len) {
|
---|
128 | int k;
|
---|
129 | if(position>=document.getLength()) {
|
---|
130 | return -1; // Read past end of document.
|
---|
131 | }
|
---|
132 | k = len;
|
---|
133 | if((position+k)>=document.getLength())
|
---|
134 | k = document.getLength() - (int)position;
|
---|
135 | if(off + k >= cbuf.length)
|
---|
136 | k = cbuf.length - off;
|
---|
137 | try {
|
---|
138 | document.getText((int)position, k, segment);
|
---|
139 | position += k;
|
---|
140 | System.arraycopy(segment.array,segment.offset,
|
---|
141 | cbuf,off,
|
---|
142 | k);
|
---|
143 | return k;
|
---|
144 | } catch (BadLocationException ble) {
|
---|
145 | /* Should never happen ? */
|
---|
146 | return -1;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Tells whether this reader is ready to be read without
|
---|
153 | * blocking for input. <code>DocumentReader</code> will
|
---|
154 | * always return true.
|
---|
155 | *
|
---|
156 | * @return <code>true</code> if the next read operation will
|
---|
157 | * return without blocking.
|
---|
158 | */
|
---|
159 | public boolean ready() {
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Resets the stream. If the stream has been marked, then attempt to
|
---|
166 | * reposition it at the mark. If the stream has not been marked, then
|
---|
167 | * move it to the beginning of the document.
|
---|
168 | */
|
---|
169 | public void reset() {
|
---|
170 | if(mark==-1) {
|
---|
171 | position = 0;
|
---|
172 | }
|
---|
173 | else {
|
---|
174 | position = mark;
|
---|
175 | mark = -1;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Skips characters. This will not 'skip' past the end of the document.
|
---|
182 | *
|
---|
183 | * @param n The number of characters to skip.
|
---|
184 | * @return The number of characters actually skipped.
|
---|
185 | */
|
---|
186 | public long skip(long n) {
|
---|
187 | if (position+n<=document.getLength()) {
|
---|
188 | position += n;
|
---|
189 | return n;
|
---|
190 | }
|
---|
191 | long temp = position;
|
---|
192 | position = document.getLength();
|
---|
193 | return document.getLength() - temp;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Move to the specified position in the document. If <code>pos</code>
|
---|
199 | * is greater than the document's length, the stream's position is moved
|
---|
200 | * to the end of the document.
|
---|
201 | *
|
---|
202 | * @param pos The position in the document to move to.
|
---|
203 | */
|
---|
204 | public void seek(long pos) {
|
---|
205 | position = Math.min(pos, document.getLength());
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | } |
---|