bug fixes
[IRC.git] / Robust / src / Benchmarks / SingleTM / Yada / bytereader.java
1 public class bytereader {
2   FileInputStream fis;
3   byte[] buffer;
4   int lastlocation;
5   int pos;
6   byte[] tmp;
7
8   public bytereader(FileInputStream fis) {
9     this.fis=fis;
10     this.buffer=new byte[1024];
11     this.tmp=new byte[200];
12     lastlocation=0;
13     pos=0;
14   }
15   
16   public void jumptonextline() {
17     while(true) {
18       for(;pos<lastlocation;pos++) {
19         if (buffer[pos]=='\n') {
20           pos++;
21           return;
22         }
23       }
24       if (pos==lastlocation) {
25         readnewdata();
26       }
27     }
28   }
29
30   private void readnewdata() {
31     pos=0;
32     lastlocation=fis.read(buffer);
33   }
34
35   byte[] curbuffer;
36   int start;
37   int end;
38
39   private void skipline() {
40     if (buffer[pos]=='#')
41       jumptonextline();
42   }
43
44   public int getInt() {
45     getBytes();
46     String str=new String(curbuffer, start, end-start);
47     return Integer.parseInt(str);
48   }
49
50   public double getDouble() {
51     getBytes();
52     String str=new String(curbuffer, start, end-start);
53     return Double.parseDouble(str);
54   }
55
56   private void getBytes() {
57     boolean searching=true;
58     while(searching) {
59       for(;pos<lastlocation;pos++) {
60         if (buffer[pos]!=' '&&buffer[pos]!='\n'&&buffer[pos]!='\t') {
61           searching=false;
62           break;
63         }
64       }
65       if (searching) {
66         readnewdata();
67       }
68     }
69     start=pos;
70     for(;pos<lastlocation;pos++) {
71       if (buffer[pos]==' '||
72           buffer[pos]=='\n') {
73         end=pos;
74         pos++;
75         skipline();
76         curbuffer=buffer;
77         return;
78       }
79     }
80     curbuffer=tmp;
81     for(int i=start;i<lastlocation;i++) {
82       tmp[i-start]=buffer[i];
83     }
84     readnewdata();
85     start=lastlocation-start;
86     for(;pos<lastlocation;pos++) {
87       if (buffer[pos]==' '||
88           buffer[pos]=='\n') {
89         end=pos+start;
90         start=0;
91         pos++;
92         skipline();
93         return;
94       }
95       tmp[pos+start]=buffer[pos];
96     }
97   }
98
99 }