KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > swt > BufferContentReader


1 //This is free software; for terms and warranty disclaimer see ./COPYING.
2

3 package gnu.jemacs.swt;
4
5 import java.io.IOException JavaDoc;
6 import java.io.Reader JavaDoc;
7
8 /**
9  * @author Christian Surlykke
10  * 31-07-2004
11  */

12 public class BufferContentReader extends Reader JavaDoc
13 {
14
15   private boolean closed = false;
16   private BufferContent bufferContent;
17   private int start;
18   private int count;
19   
20   /**
21    *
22    */

23   public BufferContentReader(BufferContent bufferContent, int start, int count)
24   {
25     super();
26     this.bufferContent = bufferContent;
27     this.start = start;
28     this.count = Math.min(bufferContent.size() - start, count);
29   }
30
31   /**
32    * @see java.io.Reader#close()
33    */

34   public void close() throws IOException JavaDoc
35   {
36     closed = true;
37   }
38
39   /**
40    * @see java.io.Reader#read(char[], int, int)
41    */

42   public int read(char[] cbuf, int off, int len) throws IOException JavaDoc
43   {
44     if (closed)
45     {
46       throw new IOException JavaDoc();
47     }
48     
49     if (count <= 0)
50     {
51       return -1;
52     }
53     else
54     {
55       int chars = Math.min(this.count, len);
56       bufferContent.getChars(start, start + chars, cbuf, off);
57       start += chars;
58       count -= chars;
59       return chars;
60     }
61   }
62 }
63
Popular Tags