01: //This is free software; for terms and warranty disclaimer see ./COPYING.
02:
03: package gnu.jemacs.swt;
04:
05: import java.io.IOException;
06: import java.io.Reader;
07:
08: /**
09: * @author Christian Surlykke
10: * 31-07-2004
11: */
12: public class BufferContentReader extends Reader {
13:
14: private boolean closed = false;
15: private BufferContent bufferContent;
16: private int start;
17: private int count;
18:
19: /**
20: *
21: */
22: public BufferContentReader(BufferContent bufferContent, int start,
23: int count) {
24: super ();
25: this .bufferContent = bufferContent;
26: this .start = start;
27: this .count = Math.min(bufferContent.size() - start, count);
28: }
29:
30: /**
31: * @see java.io.Reader#close()
32: */
33: public void close() throws IOException {
34: closed = true;
35: }
36:
37: /**
38: * @see java.io.Reader#read(char[], int, int)
39: */
40: public int read(char[] cbuf, int off, int len) throws IOException {
41: if (closed) {
42: throw new IOException();
43: }
44:
45: if (count <= 0) {
46: return -1;
47: } else {
48: int chars = Math.min(this.count, len);
49: bufferContent.getChars(start, start + chars, cbuf, off);
50: start += chars;
51: count -= chars;
52: return chars;
53: }
54: }
55: }
|