OSDN Git Service

入出力強化
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / parser / SpottedInputStream.java
1 /*
2  * position spotted input stream
3  *
4  * License : The MIT License
5  * Copyright(c) 2012 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.parser;
9
10 import java.io.EOFException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.PushbackInputStream;
14
15 /**
16  * エラー報告用のバイト位置管理、
17  * およびストリーム末端の判定
18  * のための機能を含む入力ストリーム。
19  */
20 class SpottedInputStream extends InputStream {
21
22     private final PushbackInputStream pin;
23     private long position = 0L;
24
25
26     /**
27      * コンストラクタ。
28      * @param is 入力ストリーム
29      */
30     SpottedInputStream(InputStream is){
31         super();
32         this.pin = new PushbackInputStream(is, 1);
33         return;
34     }
35
36
37     /**
38      * {@inheritDoc}
39      * @return {@inheritDoc}
40      * @throws IOException {@inheritDoc}
41      */
42     @Override
43     public int read() throws IOException{
44         int result = this.pin.read();
45         if(result >= 0) this.position++;
46         return result;
47     }
48
49     /**
50      * {@inheritDoc}
51      * @param b {@inheritDoc}
52      * @return {@inheritDoc}
53      * @throws IOException {@inheritDoc}
54      */
55     @Override
56     public int read(byte[] b) throws IOException{
57         int result = this.pin.read(b);
58         if(result >= 0) this.position += result;
59         return result;
60     }
61
62     /**
63      * {@inheritDoc}
64      * @param b {@inheritDoc}
65      * @param off {@inheritDoc}
66      * @param len {@inheritDoc}
67      * @return {@inheritDoc}
68      * @throws IOException {@inheritDoc}
69      */
70     @Override
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;
74         return result;
75     }
76
77     /**
78      * {@inheritDoc}
79      * @param skipLength {@inheritDoc}
80      * @return {@inheritDoc}
81      * @throws IOException {@inheritDoc}
82      */
83     @Override
84     public long skip(long skipLength) throws IOException{
85         long result = this.pin.skip(skipLength);
86         if(result >= 0L) this.position += result;
87         return result;
88     }
89
90     /**
91      * {@inheritDoc}
92      * @throws IOException {@inheritDoc}
93      */
94     @Override
95     public void close() throws IOException{
96         this.pin.close();
97         return;
98     }
99
100     /**
101      * 読み込み済みバイト数を返す。
102      * @return 読み込み済みバイト数
103      */
104     public long getPosition(){
105         return this.position;
106     }
107
108     /**
109      * まだ入力が残っているか判定する。
110      * @return 残っていればtrue
111      * @throws IOException 入力エラー。java.io.EOFException ではないはず。
112      */
113     public boolean hasMore() throws IOException{
114         int bVal;
115
116         try{
117             bVal = this.pin.read();
118         }catch(EOFException e){ // ありえない?
119             return false;
120         }
121
122         if(bVal < 0){
123             return false;
124         }
125
126         this.pin.unread(bVal);
127
128         return true;
129     }
130
131 }