KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > io > WritableLineReader


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Nov 13, 2005
23  */

24 package org.lobobrowser.html.io;
25
26 import java.io.*;
27
28 public class WritableLineReader extends LineNumberReader {
29     private final Reader delegate;
30     
31     public WritableLineReader(Reader reader, int bufferSize) {
32         super(reader, bufferSize);
33         this.delegate = reader;
34     }
35
36     public WritableLineReader(Reader reader) {
37         super(reader);
38         this.delegate = reader;
39     }
40
41     /*
42      * Note: Not implicitly thread safe.
43      */

44     public int read() throws IOException {
45         StringBuffer JavaDoc sb = this.writeBuffer;
46         if(sb != null && sb.length() > 0) {
47             char ch = sb.charAt(0);
48             sb.deleteCharAt(0);
49             if(sb.length() == 0) {
50                 this.writeBuffer = null;
51             }
52             return (int) ch;
53         }
54         return super.read();
55     }
56     
57     /* (non-Javadoc)
58      * Note: Not implicitly thread safe.
59      * @see java.io.Reader#read(byte[], int, int)
60      */

61     public int read(char[] b, int off, int len) throws IOException {
62         StringBuffer JavaDoc sb = this.writeBuffer;
63         if(sb != null && sb.length() > 0) {
64             int srcEnd = Math.min(sb.length(), len);
65             sb.getChars(0, srcEnd, b, off);
66             sb.delete(0, srcEnd);
67             if(sb.length() == 0) {
68                 this.writeBuffer = null;
69             }
70             return srcEnd;
71         }
72         return super.read(b, off, len);
73     }
74     
75     public boolean ready() throws IOException {
76         StringBuffer JavaDoc sb = this.writeBuffer;
77         if(sb != null && sb.length() > 0) {
78             return true;
79         }
80         return super.ready();
81     }
82
83     /* (non-Javadoc)
84      * Note: Not implicitly thread safe.
85      * @see java.io.Reader#close()
86      */

87     public void close() throws IOException {
88         this.writeBuffer = null;
89         super.close();
90     }
91     
92     private StringBuffer JavaDoc writeBuffer = null;
93     
94     /**
95      * Note: Not implicitly thread safe.
96      * @param text
97      * @throws IOException
98      */

99     public void write(String JavaDoc text) throws IOException {
100         // Document overrides this to know that new data is coming.
101
StringBuffer JavaDoc sb = this.writeBuffer;
102         if(sb == null) {
103             sb = new StringBuffer JavaDoc();
104             this.writeBuffer = sb;
105         }
106         sb.append(text);
107     }
108 }
109
Popular Tags