KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > parser > CharBuffer


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.template.parser;
19
20 import java.io.*;
21
22
23 /**
24  * Class CharBuffer
25  *
26  *
27  * @author Wendel D. de Witte
28  * @version %I%, %G%
29  */

30 public class CharBuffer extends InputBuffer {
31
32    // char source
33

34    /** Field input */
35    private Reader input = null;
36
37    /** Field inputString */
38    private String JavaDoc inputString = null;
39
40    /** Field stringIndex */
41    private int stringIndex = 0;
42
43    /** Field stringLength */
44    private int stringLength = 0;
45
46
47    /**
48     * Create a character buffer
49     *
50     * @param input
51     */

52    public CharBuffer(Reader input) {
53       super();
54       this.input = input;
55    }
56
57
58    /**
59     * Create a character buffer
60     *
61     * @param input
62     */

63    public CharBuffer(String JavaDoc input) {
64       super();
65       this.inputString = input;
66       this.stringLength = input.length();
67    }
68
69
70    /**
71     * Ensure that the character buffer is sufficiently full
72     *
73     * @param amount
74     *
75     * @throws CharStreamException
76     */

77    public void fill(int amount) throws CharStreamException {
78       try {
79          syncConsume();
80          // Fill the buffer sufficiently to hold needed characters
81
while (queue.nbrEntries < amount + markerOffset) {
82             queue.append(read());
83          }
84       }
85       catch (IOException io) {
86          throw new CharStreamIOException(io);
87       }
88    }
89
90
91    /**
92     * Method read
93     *
94     *
95     * @return
96     *
97     * @throws IOException
98     *
99     */

100    private char read() throws IOException {
101       if (input != null) {
102          return (char) input.read();
103       }
104
105       if ((inputString != null) && (stringIndex < stringLength)) {
106          return (char) inputString.charAt(stringIndex++);
107       }
108       return (char) -1;
109    }
110 }
Popular Tags