KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > io > PushbackPositionReader


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.io;
54
55 import java.io.*;
56
57 /******************************************************************************
58  * The PushbackPositionReader is a kind of pushback reader that tracks the
59  * postion in the stream of the next character to be read.
60  * The java.io.PushbackReader allows arbitrary characters
61  * to be pushed back. Since this Reader may need to keep track of how many
62  * characters were scanned from the underlying Reader to actually produce
63  * a character, the unread operation cannot accept any arbitrary character.
64  *
65  * @author Brian S O'Neill
66  * @version
67  * <!--$$Revision:--> 12 <!-- $-->, <!--$$JustDate:--> 12/11/00 <!-- $-->
68  * @see java.io.PushbackReader
69  */

70 public class PushbackPositionReader extends PositionReader {
71     /** Maximum pushback allowed. */
72     private int mMaxPushback;
73
74     /** Most recently read characters with escapes already processed. */
75     private int[] mCharacters;
76
77     /** The scan lengths of the most recently read characters. */
78     private int[] mPositions;
79
80     /** The cursor marks the position in the arrays of the last character. */
81     private int mCursor;
82
83     /** Amount of characters currently pushed back. */
84     private int mPushback;
85
86     public PushbackPositionReader(Reader reader) {
87         this(reader, 0);
88     }
89
90     public PushbackPositionReader(Reader reader, int pushback) {
91         super(reader);
92
93         // Two more are required for correct operation
94
pushback += 2;
95
96         mMaxPushback = pushback;
97         mCharacters = new int[pushback];
98         mPositions = new int[pushback];
99         mCursor = 0;
100         mPushback = 0;
101     }
102
103     /**
104      * @return the start position of the last read character.
105      */

106     public int getStartPosition() {
107         int back = mCursor - 2;
108         if (back < 0) back += mMaxPushback;
109
110         return mPositions[back];
111     }
112
113     public int read() throws IOException {
114         int c;
115
116         if (mPushback > 0) {
117             mPushback--;
118
119             mPosition = mPositions[mCursor];
120             c = mCharacters[mCursor++];
121             if (mCursor >= mMaxPushback) mCursor = 0;
122             
123             return c;
124         }
125
126         c = super.read();
127         
128         mPositions[mCursor] = mPosition;
129         mCharacters[mCursor++] = c;
130         if (mCursor >= mMaxPushback) mCursor = 0;
131
132         return c;
133     }
134
135     public int peek() throws IOException {
136         int c = read();
137         unread();
138         return c;
139     }
140
141     /**
142      * Unread the last several characters read.
143      *
144      * <p>Unlike PushbackReader, unread does not allow arbitrary characters to
145      * to be unread. Rather, it functions like an undo operation.
146      *
147      * @param amount Amount of characters to unread.
148      * @see java.io.PushbackReader#unread(int)
149      */

150     public void unread(int amount) throws IOException {
151         for (int i=0; i<amount; i++) {
152             unread();
153         }
154     }
155     
156     /**
157      * Unread the last character read.
158      *
159      * <p>Unlike PushbackReader, unread does not allow arbitrary characters to
160      * to be unread. Rather, it functions like an undo operation.
161      *
162      * @see java.io.PushbackReader#unread(int)
163      */

164     public void unread() throws IOException {
165         mPushback++;
166
167         if (mPushback > mMaxPushback - 2) {
168             throw new IOException(this.getClass().getName() +
169                                   ": pushback exceeded " + (mMaxPushback - 2));
170         }
171
172         if ((--mCursor) < 0) mCursor += mMaxPushback;
173
174         if (mCursor > 0) {
175             mPosition = mPositions[mCursor - 1];
176         }
177         else {
178             mPosition = mPositions[mMaxPushback - 1];
179         }
180
181         unreadHook(mCharacters[mCursor]);
182     }
183
184     /**
185      * A hook call from the unread method(s). Every unread character is
186      * passed to this method.
187      */

188     protected void unreadHook(int c) {
189     }
190 }
191
Popular Tags