b3294c0312c815e4ff59fbcfa32c7c6551bbca61
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / PositionAwareReader.java
1 /*
2  * Copyright (c) 1995-2012, Index Data
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package org.z3950.zing.cql;
7
8 import java.io.IOException;
9 import java.io.Reader;
10 import java.nio.CharBuffer;
11
12 /**
13  * Reader proxy to count how many characters has been read so far.
14  * @author jakub
15  */
16 public class PositionAwareReader extends Reader {
17   protected Reader reader;
18   protected int pos = -1;
19   
20   public PositionAwareReader(Reader reader) {
21     this.reader = reader;
22   }
23
24   /*
25    * Position of the last read character or -1 if either reading from an empty 
26    * stream or no 'read' has been invoked for this reader.
27    */
28   public int getPosition() {
29     return pos;
30   }
31
32   @Override
33   public void mark(int readAheadLimit) throws IOException {
34     reader.mark(readAheadLimit);
35   }
36
37   @Override
38   public boolean markSupported() {
39     return reader.markSupported();
40   }
41
42   @Override
43   public int read() throws IOException {
44     int c = reader.read();
45     if (c != -1) pos++;
46     return c;
47   }
48
49   @Override
50   public int read(char[] cbuf) throws IOException {
51     int c = reader.read(cbuf);
52     if (c != -1) pos+=c;
53     return c;
54   }
55
56   @Override
57   public int read(CharBuffer target) throws IOException {
58     int c = reader.read(target);
59     if (c != -1) pos+=c;
60     return c;
61   }
62   
63   @Override
64   public int read(char[] cbuf, int off, int len) throws IOException {
65     int c = reader.read(cbuf, off, len);
66     if (c != -1) pos+=c;
67     return c;
68   }
69
70   @Override
71   public boolean ready() throws IOException {
72     return reader.ready();
73   }
74
75   @Override
76   public long skip(long n) throws IOException {
77     return reader.skip(n);
78   }
79   
80   @Override
81   public void close() throws IOException {
82     reader.close();
83   }
84
85   @Override
86   public void reset() throws IOException {
87     reader.reset();
88   }
89   
90   //override object methods, to be on the safe-side
91
92   @Override
93   public boolean equals(Object obj) {
94     return reader.equals(obj);
95   }
96
97   @Override
98   public String toString() {
99     return reader.toString();
100   }
101
102   @Override
103   public int hashCode() {
104     return reader.hashCode();
105   }
106   
107 }