1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  package org.sat4j.reader;
31  
32  import java.io.BufferedInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.Serializable;
36  import java.math.BigInteger;
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  public class EfficientScanner implements Serializable {
50  
51      private static final long serialVersionUID = 1L;
52  
53      
54      private static final int TAILLE_BUF = 16384;
55  
56      private final transient BufferedInputStream in;
57  
58      private static final char EOF = (char) -1;
59  
60      private final char commentChar;
61  
62      
63  
64  
65      public EfficientScanner(final InputStream input, char commentChar) {
66          this.in = new BufferedInputStream(input, EfficientScanner.TAILLE_BUF);
67          this.commentChar = commentChar;
68      }
69  
70      public EfficientScanner(final InputStream input) {
71          this(input, 'c');
72      }
73  
74      public void close() throws IOException {
75          this.in.close();
76      }
77  
78      
79      public void skipComments() throws IOException {
80          char currentChar;
81          for (;;) {
82              currentChar = currentChar();
83              if (currentChar != this.commentChar) {
84                  break;
85              }
86              skipRestOfLine();
87              if (currentChar == EOF) {
88                  break;
89              }
90          }
91      }
92  
93      
94  
95  
96  
97  
98  
99  
100     public int nextInt() throws IOException, ParseFormatException {
101         int val = 0;
102         boolean neg = false;
103         char currentChar = skipSpaces();
104         if (currentChar == '-') {
105             neg = true;
106             currentChar = (char) this.in.read();
107         } else if (currentChar == '+') {
108             currentChar = (char) this.in.read();
109         } else if (currentChar >= '0' && currentChar <= '9') {
110             val = currentChar - '0';
111             currentChar = (char) this.in.read();
112         } else {
113             throw new ParseFormatException("Unknown character " + currentChar);
114         }
115         
116         while (currentChar >= '0' && currentChar <= '9') {
117             val = val * 10 + currentChar - '0';
118             currentChar = (char) this.in.read();
119         }
120         if (currentChar == '\r') {
121             this.in.read(); 
122         }
123         return neg ? -val : val;
124     }
125 
126     public BigInteger nextBigInteger() throws IOException, ParseFormatException {
127         StringBuffer stb = new StringBuffer();
128         char currentChar = skipSpaces();
129         if (currentChar == '-') {
130             stb.append(currentChar);
131             currentChar = (char) this.in.read();
132         } else if (currentChar == '+') {
133             currentChar = (char) this.in.read();
134         } else if (currentChar >= '0' && currentChar <= '9') {
135             stb.append(currentChar);
136             currentChar = (char) this.in.read();
137         } else {
138             throw new ParseFormatException("Unknown character " + currentChar);
139         }
140         while (currentChar >= '0' && currentChar <= '9') {
141             stb.append(currentChar);
142             currentChar = (char) this.in.read();
143         }
144         return new BigInteger(stb.toString());
145     }
146 
147     
148 
149 
150 
151     public String next() throws IOException, ParseFormatException {
152         StringBuffer stb = new StringBuffer();
153         char currentChar = skipSpaces();
154         while (currentChar != ' ' && currentChar != '\n') {
155             stb.append(currentChar);
156             currentChar = (char) this.in.read();
157         }
158         return stb.toString();
159     }
160 
161     public char skipSpaces() throws IOException {
162         char car;
163 
164         do {
165             car = (char) this.in.read();
166         } while (car == ' ' || car == '\n');
167 
168         return car;
169     }
170 
171     public String nextLine() throws IOException {
172         StringBuffer stb = new StringBuffer();
173         char car;
174         do {
175             car = (char) this.in.read();
176             stb.append(car);
177         } while (car != '\n' && car != EOF);
178         return stb.toString();
179     }
180 
181     public void skipRestOfLine() throws IOException {
182         char car;
183         do {
184             car = (char) this.in.read();
185         } while (car != '\n' && car != EOF);
186     }
187 
188     public boolean eof() throws IOException {
189         return currentChar() == EOF;
190     }
191 
192     public char currentChar() throws IOException {
193         this.in.mark(10);
194         char car = (char) this.in.read();
195         this.in.reset();
196         return car;
197     }
198 }