-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIn.java
58 lines (49 loc) · 1.33 KB
/
In.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Scanner;
/*THIS FILE WAS TAKEN FROM PRINCETON UNIVERSITY UNDER MIT LICENSE
*
* STRICTLY FOR THE PURPOSE OF CS 380 NETWORKING EXERCISE
*
* I HAVE TAKEN PART OF THE IN CLASS
*/
public final class In{
private static final String CHARSET_NAME = "UTF-8";
private static final Locale LOCALE = Locale.US;
private Scanner scanner;
//For User Input
public In() {
scanner = new Scanner(new BufferedInputStream(System.in), CHARSET_NAME);
scanner.useLocale(LOCALE);
}
public In(Socket socket) {
final Locale LOCALE = Locale.US;
if (socket == null) throw new IllegalArgumentException("socket argument is null");
try {
InputStream is = socket.getInputStream();
scanner = new Scanner(new BufferedInputStream(is), "UTF-8");
scanner.useLocale(LOCALE);
}catch (IOException ioe) {
throw new IllegalArgumentException("Could not open " + socket, ioe);
}
}
public boolean hasNextLine() {
return scanner.hasNextLine();
}
public String readLine() {
String line;
try {
line = scanner.nextLine();
}catch(NoSuchElementException e) {
line = null;
}
return line;
}
public void close() {
scanner.close();
}
}