KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > PushbackReader


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program 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 Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.Reader JavaDoc;
21 import java.io.IOException JavaDoc;
22
23
24 /**
25  * A PushbackReader which keeps track of the position in the stream
26  *
27  * @see #getPosition()
28  */

29
30 public class PushbackReader extends java.io.PushbackReader JavaDoc
31 {
32    /** current position in stream */
33    int position;
34
35    public PushbackReader(Reader JavaDoc in) {
36       super(in);
37    }
38
39    /**
40     * @param in reader to read from
41     * @param size bushback buffer size
42     * @see java.io.PushbackReader#PushbackReader(Reader,int)
43     */

44    public PushbackReader(Reader JavaDoc in, int size) {
45       super(in,size);
46    }
47
48    public int read() throws IOException JavaDoc {
49       int c = super.read();
50       position++;
51       return c;
52    }
53
54    public int read(char cbuf[], int off, int len) throws IOException JavaDoc
55    {
56       int nbRead = super.read(cbuf,off,len);
57       if (nbRead!=-1) {
58          position += nbRead;
59       }
60       return nbRead;
61    }
62
63     public void unread(int c) throws IOException JavaDoc {
64        super.unread(c);
65        position--;
66     }
67
68     public void unread(char cbuf[], int off, int len) throws IOException JavaDoc {
69        super.unread(cbuf,off,len);
70        position -= len;
71     }
72
73    /**
74     * Returns the position in the stream
75     */

76    public int getPosition() {
77       return position;
78    }
79 }
80
Popular Tags