DEADSOFTWARE

4b8793c2e2b5cf78e60d9435a138d26083e69c16
[LongFlight.git] / StringReader.java
1 package code.kalter.longflight;
3 import java.io.IOException;
4 import java.io.InputStream;
6 /**
7 * Для удобного чтения строк и других типов из файла. Является не более, чем
8 * обёрткой для Inputstream
9 *
10 * @author KalterFive
11 */
12 public class StringReader {
14 private final InputStream inputStream;
16 public StringReader(InputStream inputStream) {
17 this.inputStream = inputStream;
18 }
20 public String nextString() throws IOException {
21 StringBuffer stringBuffer = new StringBuffer();
22 int c = inputStream.read();
23 while (c != '\r' && c != -1) {
24 stringBuffer.append((char) c);
25 c = inputStream.read();
26 }
27 inputStream.read();
28 return stringBuffer.toString();
29 }
31 // читает true or false
32 public boolean nextBool() throws IOException {
33 String line = nextString();
34 if (line.equals("true")) {
35 return true;
36 } else if (line.equals("false")) {
37 return false;
38 } else {
39 throw new IllegalArgumentException(line);
40 }
41 }
43 public byte nextByte() throws IOException {
44 return Byte.parseByte(nextString());
45 }
47 public short nextShort() throws IOException {
48 return Short.parseShort(nextString());
49 }
51 public int nextInt() throws IOException {
52 return Integer.parseInt(nextString());
53 }
55 public long nextLong() throws IOException {
56 return Long.parseLong(nextString());
57 }
59 public float nextFloat() throws IOException {
60 return Float.parseFloat(nextString());
61 }
63 public double nextDouble() throws IOException {
64 return Double.parseDouble(nextString());
65 }
67 public void close() throws IOException {
68 inputStream.close();
69 }
70 }