Skip to content

Commit 2c30241

Browse files
committed
Code: Utf8LineInputStream, SourceLocation, SourceManager
1 parent 7adedf0 commit 2c30241

File tree

4 files changed

+135
-7
lines changed

4 files changed

+135
-7
lines changed

Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.duangsuse.telegramscanner;
22

3+
import org.duangsuse.telegramscanner.scanner.Utf8LineInputStream;
4+
5+
import java.io.IOException;
36
import java.io.PrintStream;
47

58
/**
@@ -33,5 +36,19 @@ private Main() {}
3336
*/
3437
public static void main(String... args) {
3538
err.print("TelegramScanner version "); err.println(VERSION);
39+
40+
Utf8LineInputStream input = new Utf8LineInputStream(System.in);
41+
String line = "";
42+
43+
try {
44+
do {
45+
out.print(line);
46+
out.println();
47+
out.print("> ");
48+
}
49+
while ((line = input.readLine()) != null);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
3653
}
3754
}

scanner/Utf8LineInputStream.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.duangsuse.telegramscanner.scanner;
2+
3+
import java.io.*;
4+
import java.nio.charset.Charset;
5+
6+
/**
7+
* Line based input stream
8+
*
9+
* @see java.io.DataInput
10+
* @see java.io.InputStream
11+
*/
12+
public class Utf8LineInputStream implements Closeable {
13+
/**
14+
* Delegated data input stream
15+
*/
16+
private DataInput target;
17+
18+
/**
19+
* Construct using data input {@link DataInputStream}
20+
*
21+
* @param dataIn data input instance
22+
*/
23+
public Utf8LineInputStream(DataInput dataIn) {
24+
this.target = dataIn;
25+
}
26+
27+
/**
28+
* Construct using byte input stream
29+
*
30+
* @see InputStreamReader with utf-8 decoder support
31+
* @param in line-based input stream
32+
*/
33+
public Utf8LineInputStream(InputStream in) {
34+
final InputStreamReader utf8InputReader = new InputStreamReader(in, UTF_8);
35+
InputStream is = new InputStream() {
36+
@Override
37+
public int read() throws IOException {
38+
return utf8InputReader.read();
39+
} /* readLine() uses this method only */
40+
};
41+
42+
this.target = new DataInputStream(is);
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (o == null || getClass() != o.getClass()) return false;
49+
50+
Utf8LineInputStream that = (Utf8LineInputStream) o;
51+
52+
return target.equals(that.target);
53+
}
54+
55+
/**
56+
* Utf-8 charset
57+
*/
58+
@SuppressWarnings("WeakerAccess")
59+
protected static final Charset UTF_8 = Charset.forName("UTF-8");
60+
61+
/**
62+
* Reads the next line of text from the input stream.
63+
* It reads successive bytes, converting
64+
* each byte separately into a character,
65+
* until it encounters a line terminator or
66+
* end of
67+
* file; the characters read are then
68+
* returned as a {@code String}. Note
69+
* that because this
70+
* method processes bytes,
71+
* <b>it does not support input of the full Unicode
72+
* character set.</b>
73+
* <p>
74+
* If end of file is encountered
75+
* before even one byte can be read, then {@code null}
76+
* is returned. Otherwise, each byte that is
77+
* read is converted to type {@code char}
78+
* by zero-extension. If the character {@code '\n'}
79+
* is encountered, it is discarded and reading
80+
* ceases. If the character {@code '\r'}
81+
* is encountered, it is discarded and, if
82+
* the following byte converts &#32;to the
83+
* character {@code '\n'}, then that is
84+
* discarded also; reading then ceases. If
85+
* end of file is encountered before either
86+
* of the characters {@code '\n'} and
87+
* {@code '\r'} is encountered, reading
88+
* ceases. Once reading has ceased, a {@code String}
89+
* is returned that contains all the characters
90+
* read and not discarded, taken in order.
91+
* Note that every character in this string
92+
* will have a value less than {@code \u005Cu0100},
93+
* that is, {@code (char)256}.
94+
*
95+
* @return the next line of text from the input stream,
96+
* or {@code null} if the end of file is
97+
* encountered before a byte can be read.
98+
* @exception IOException if an I/O error occurs.
99+
*/
100+
public String readLine() throws IOException {
101+
return target.readLine();
102+
}
103+
104+
@Override
105+
public void close() throws IOException {
106+
if (target instanceof Closeable)
107+
((Closeable) target).close();
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return "Utf8LineInputStream(" + target.toString() + ")";
113+
}
114+
}

sourcemanager/SourceLocation.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public SourceLocation() {
2828
}
2929

3030
public SourceLocation(int offset, int line, int messageNo, int messageLine) {
31-
super();
31+
super(); // ;)
3232
this.offset = offset;
3333
this.line = line;
3434
this.messageNo = messageNo;
@@ -75,10 +75,6 @@ public int hashCode() {
7575

7676
@Override
7777
public String toString() {
78-
final StringBuilder sb = new StringBuilder("SourceLocation");
79-
sb.append('(').append('@').append(offset).append('L').append(line)
80-
.append(", Message#").append(messageNo).append(':').append(messageLine).append(')');
81-
82-
return sb.toString();
78+
return String.format("SourceLocation(@%dL%d, Message#%d:%d)", offset, line, messageNo, messageLine);
8379
}
8480
}

sourcemanager/SourceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public SourceManager() {
1717
* Lazy source manager singleton instance
1818
*/
1919
private static final class LazyHolder {
20-
static final SourceManager INSTANCE = new SourceManager();
20+
/** Initialized in &lt;clinit&gt; */static final SourceManager INSTANCE;
21+
static { INSTANCE = new SourceManager(); }
2122
}
2223

2324
public SourceManager getInstance() { return LazyHolder.INSTANCE; }

0 commit comments

Comments
 (0)