001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.io.Reader;
021    import java.io.IOException;
022    
023    
024    /**
025     * A PushbackReader which keeps track of the position in the stream
026     *
027     * @see #getPosition()
028     */
029    
030    public class PushbackReader extends java.io.PushbackReader
031    {
032       /** current position in stream */
033       int position;
034    
035       public PushbackReader(Reader in) {
036          super(in);
037       }
038    
039       /**
040        * @param in reader to read from
041        * @param size bushback buffer size
042        * @see java.io.PushbackReader#PushbackReader(Reader,int)
043        */
044       public PushbackReader(Reader in, int size) {
045          super(in,size);
046       }
047    
048       public int read() throws IOException {
049          int c = super.read();
050          position++;
051          return c;
052       }
053    
054       public int read(char cbuf[], int off, int len) throws IOException
055       {
056          int nbRead = super.read(cbuf,off,len);
057          if (nbRead!=-1) {
058             position += nbRead;
059          }
060          return nbRead;
061       }
062    
063        public void unread(int c) throws IOException {
064           super.unread(c);
065           position--;
066        }
067    
068        public void unread(char cbuf[], int off, int len) throws IOException {
069           super.unread(cbuf,off,len);
070           position -= len;
071        }
072    
073       /**
074        * Returns the position in the stream
075        */
076       public int getPosition() {
077          return position;
078       }
079    }