2 * position spotted input stream
4 * License : The MIT License
5 * Copyright(c) 2012 MikuToga Partners
8 package jp.sourceforge.mikutoga.parser;
10 import java.io.EOFException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.PushbackInputStream;
20 class SpottedInputStream extends InputStream {
22 private final PushbackInputStream pin;
23 private long position = 0L;
30 SpottedInputStream(InputStream is){
32 this.pin = new PushbackInputStream(is, 1);
39 * @return {@inheritDoc}
40 * @throws IOException {@inheritDoc}
43 public int read() throws IOException{
44 int result = this.pin.read();
45 if(result >= 0) this.position++;
51 * @param b {@inheritDoc}
52 * @return {@inheritDoc}
53 * @throws IOException {@inheritDoc}
56 public int read(byte[] b) throws IOException{
57 int result = this.pin.read(b);
58 if(result >= 0) this.position += result;
64 * @param b {@inheritDoc}
65 * @param off {@inheritDoc}
66 * @param len {@inheritDoc}
67 * @return {@inheritDoc}
68 * @throws IOException {@inheritDoc}
71 public int read(byte[] b, int off, int len) throws IOException{
72 int result = this.pin.read(b, off, len);
73 if(result >= 0) this.position += result;
79 * @param skipLength {@inheritDoc}
80 * @return {@inheritDoc}
81 * @throws IOException {@inheritDoc}
84 public long skip(long skipLength) throws IOException{
85 long result = this.pin.skip(skipLength);
86 if(result >= 0L) this.position += result;
92 * @throws IOException {@inheritDoc}
95 public void close() throws IOException{
104 public long getPosition(){
105 return this.position;
111 * @throws IOException 入力エラー。java.io.EOFException ではないはず。
113 public boolean hasMore() throws IOException{
117 bVal = this.pin.read();
118 }catch(EOFException e){ // ありえない?
126 this.pin.unread(bVal);