KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > io > RememberingReader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.io;
20
21 import java.io.*;
22
23 /**
24  * This class can remember what was read from underlayng Reader.
25  * <p>
26  * It is not a good idea to plug under a buffered Reader because
27  * it would remember whole buffer or miss whole buffer if above
28  * buffer will match.
29  *
30  * @author Petr Kuzel
31  * @version
32  */

33 public class RememberingReader extends Reader {
34
35     private final Reader peer;
36     private StringBuffer JavaDoc memory;
37
38     /** Creates new RememberingReader */
39     public RememberingReader (Reader peer) {
40         this.peer = peer;
41     }
42     
43     /**
44      * All subsequent reads
45      */

46     public void startRemembering () {
47         memory = new StringBuffer JavaDoc ();
48     }
49     
50     public StringBuffer JavaDoc stopRemembering () {
51         StringBuffer JavaDoc toret = memory;
52         memory = null;
53         return toret;
54     }
55     
56     public void close () throws java.io.IOException JavaDoc {
57         peer.close ();
58     }
59     
60     public int read (char[] values, int off, int len) throws java.io.IOException JavaDoc {
61         int toret = peer.read (values, off, len);
62         if (memory != null && toret > 0) memory.append (values, off, toret);
63         return toret;
64     }
65     
66 }
67
Popular Tags