KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > template > parser > PositionReader


1 /*
2  * Copyright (c) 1997-1999 The Java Apache Project. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  * software must display the following acknowledgment:
18  * "This product includes software developed by the Java Apache
19  * Project for use in the Apache JServ servlet engine project
20  * (http://java.apache.org/)."
21  *
22  * 4. The names "Apache JServ", "Apache JServ Servlet Engine" and
23  * "Java Apache Project" must not be used to endorse or promote products
24  * derived from this software without prior written permission.
25  *
26  * 5. Products derived from this software may not be called "Apache JServ"
27  * nor may "Apache" nor "Apache JServ" appear in their names without
28  * prior written permission of the Java Apache Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  * acknowledgment:
32  * "This product includes software developed by the Java Apache
33  * Project for use in the Apache JServ servlet engine project
34  * (http://java.apache.org/)."
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Java Apache Group. For more information
51  * on the Java Apache Project and the Apache JServ Servlet Engine project,
52  * please see <http://java.apache.org/>.
53  */

54
55 /*
56  * $Id: PositionReader.java,v 1.4 2004/12/01 07:54:28 hengels Exp $
57  * Copyright 2000,2005 wingS development team.
58  *
59  * This file is part of wingS (http://www.j-wings.org).
60  *
61  * wingS is free software; you can redistribute it and/or modify
62  * it under the terms of the GNU Lesser General Public License
63  * as published by the Free Software Foundation; either version 2.1
64  * of the License, or (at your option) any later version.
65  *
66  * Please see COPYING for the complete licence.
67  */

68 package org.wings.template.parser;
69
70 import java.io.IOException JavaDoc;
71 import java.io.Reader JavaDoc;
72
73 /**
74  * PositionReader can be asked for the
75  * current position in the stream.
76  *
77  * @author <a HREF="mailto:zeller@think.de">Henner Zeller</a>
78  * @version $Revision: 1.4 $ $Date: 2004/12/01 07:54:28 $
79  */

80 public class PositionReader
81         extends Reader JavaDoc {
82
83     private Reader JavaDoc reader = null;
84     private long position;
85     private long savePosition = -1;
86
87     public PositionReader(Reader JavaDoc r) {
88         this.reader = r;
89         position = 0;
90     }
91
92     /* ---- PositionReader ---- */
93     public long getPosition() {
94         return position;
95     }
96
97     /* ---- Implementation of Reader ---- */
98
99     public int read()
100             throws IOException JavaDoc {
101         int c = reader.read();
102         if (c != -1) position++;
103         return c;
104     }
105
106     public int read(char cbuff[])
107             throws IOException JavaDoc {
108         int num = reader.read(cbuff);
109         if (num > 0) position += num;
110         return num;
111     }
112
113     public int read(char cbuff[], int off, int len)
114             throws IOException JavaDoc {
115         int num = reader.read(cbuff, off, len);
116         if (num > 0) position += num;
117         return num;
118     }
119
120     public long skip(long n)
121             throws IOException JavaDoc {
122         long skipped = reader.skip(n);
123         position += skipped;
124         return skipped;
125     }
126
127     public boolean ready()
128             throws IOException JavaDoc {
129         return reader.ready();
130     }
131
132     public boolean markSupported() {
133         return reader.markSupported();
134     }
135
136     public void mark(int readAheadLimit)
137             throws IOException JavaDoc {
138         savePosition = position;
139         reader.mark(readAheadLimit);
140     }
141
142     public void reset()
143             throws IOException JavaDoc {
144         reader.reset();
145         // this should be noticed by the reader before
146
if (savePosition < 0)
147             throw new IOException JavaDoc("mark() not set before");
148         position = savePosition;
149     }
150
151     public void close()
152             throws IOException JavaDoc {
153         reader.close();
154     }
155 }
156
157
158
Popular Tags