4 package jp.sourceforge.mikutoga.parser;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.LinkedList;
9 import java.util.Queue;
15 public class DummyInputStream extends InputStream {
17 private static final int MASK_08BIT = 0xff;
20 private final Queue<Byte> queue = new LinkedList<Byte>();
22 private boolean closed = false;
24 private long errPos = -1L;
25 private IOException errEx = null;
28 public DummyInputStream(byte[] bArray, int offset, int len){
30 queueByteArrayImpl(bArray, offset, len);
34 public DummyInputStream(byte... bArray){
35 this(bArray, 0, bArray.length);
39 public DummyInputStream(int... iArray) throws IllegalArgumentException{
40 this(toBarray(iArray));
46 * int配列からbyte配列への変換を行う。
50 * @throws IllegalArgumentException bit欠損が発生
52 protected static byte[] toBarray(int[] iArray)
53 throws IllegalArgumentException{
54 int length = iArray.length;
55 byte[] result = new byte[length];
57 for(int pos = 0; pos < length; pos++){
58 int iVal = iArray[pos];
59 if(iVal < 0x00 || 0xff < iVal){
60 throw new IllegalArgumentException();
62 byte bVal = (byte) iVal;
71 * 指定バイト数読み込んだ後に入力例外を意図的に発生させるよう設定する。
72 * @param pos 正常に読める残りバイト数。負の値なら設定解除。
73 * @param ex 入力例外。解除時に限ってnullも可。
75 public void setErrorPosition(long pos, IOException ex){
78 if(this.errPos >= 0L){
79 if(ex == null) throw new NullPointerException();
90 * あと何バイト正常に読めるか設定値を返す。
93 public long getErrorPosition(){
94 long result = this.errPos;
99 * 意図した入力エラー報告を投げる状況かチェックする。
100 * @throws IOException 意図した入力エラー
102 protected void checkIOEx() throws IOException {
103 if(this.errPos < 0L) return;
104 if(this.errPos > 0L) return;
106 IOException ex = this.errEx;
115 * クローズの有無は外部からの観察が可能。
116 * @throws IOException {@inheritDoc}
117 * 設定状況によっては意図的な入力例外を投げる。
120 public void close() throws IOException {
128 * @return クローズ済みであればtrue
130 public boolean isClosed(){
131 boolean result = this.closed;
137 * @param bArray {@inheritDoc}
138 * @param offset {@inheritDoc}
139 * @param len {@inheritDoc}
140 * @return {@inheritDoc}
141 * @throws IOException {@inheritDoc}
144 public int read(byte[] bArray, int offset, int len) throws IOException {
147 for(int pos = 0; pos < len; pos++){
150 if(result == 0) result = -1;
153 bArray[offset + pos] = (byte)iVal;
162 * @param bArray {@inheritDoc}
163 * @return {@inheritDoc}
164 * @throws IOException {@inheritDoc}
167 public int read(byte[] bArray) throws IOException {
168 int result = read(bArray, 0, bArray.length);
174 * @return {@inheritDoc}
175 * @throws IOException {@inheritDoc}
178 public int read() throws IOException {
181 Byte val = this.queue.poll();
182 if(val == null) return -1;
184 int iVal = (int)( val.byteValue() );
185 int result = iVal & MASK_08BIT;
198 public int getQueueLength(){
199 int result = this.queue.size();
206 public void clearQueue(){
214 * @param offset 開始オフセット
217 private void queueByteArrayImpl(byte[] bArray, int offset, int len){
218 for(int pos = 0; pos < len; pos++){
219 byte bVal = bArray[offset + pos];
220 this.queue.add(bVal);
228 * @param offset 開始オフセット
231 public void queueByteArray(byte[] bArray, int offset, int len){
232 queueByteArrayImpl(bArray, offset, len);
240 public void queueByteArray(byte[] bArray){
241 for(byte bVal : bArray){
242 this.queue.add(bVal);
249 * @param bArray byte型可変引数
251 public void queueByte(byte... bArray){
252 queueByteArray(bArray);
258 * @param iArray int型可変引数。byte値に収まる要素値でなければならない。
259 * @throws IllegalArgumentException byte値に収まらない値が指定された。
261 public void queueByte(int... iArray) throws IllegalArgumentException{
262 for(int iVal : iArray){
263 if(iVal < 0x00 || 0xff < iVal){
264 throw new IllegalArgumentException();
266 byte bVal = (byte) iVal;
267 this.queue.add(bVal);