-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathConsole.java
More file actions
186 lines (169 loc) · 4.49 KB
/
Copy pathConsole.java
File metadata and controls
186 lines (169 loc) · 4.49 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package javaforce;
import javaforce.api.linux.LinuxAPI;
import javaforce.api.windows.WindowsAPI;
import java.io.*;
import javaforce.api.*;
/** Console (alternative to System.in/out).
*
* @author pquiring
*/
public class Console {
private static boolean isWindows = JF.isWindows();
public static InputStream getInputStream() {
return new InputStream() {
public int read() throws IOException {
int ch;
if (isWindows) {
ch = WindowsAPI.getInstance().readConsole();
} else {
ch = LinuxAPI.getInstance().readConsole();
}
return ch;
}
public int read(byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}
public int read(byte[] buf, int offset, int length) throws IOException {
int start = offset;
int end = offset + length;
int count = 0;
for(int i=start;i<end;) {
int ch = read();
if (ch == 0 || ch == -1) break;
buf[i] = (byte)ch;
i++;
count++;
}
return count;
}
};
}
public static OutputStream getOutputStream() {
return new OutputStream() {
public void write(int utf16) throws IOException {
char ch = ASCII8.convertUTF16(utf16);
if (isWindows) {
WindowsAPI.getInstance().writeConsole(ch);
} else {
LinuxAPI.getInstance().writeConsole(ch);
}
}
};
}
public static void enableConsoleMode() {
if (JF.isWindows())
WindowsAPI.getInstance().enableConsoleMode();
else
LinuxAPI.getInstance().enableConsoleMode();
}
public static void disableConsoleMode() {
if (JF.isWindows())
WindowsAPI.getInstance().disableConsoleMode();
else
LinuxAPI.getInstance().disableConsoleMode();
}
public static boolean kbhit() {
if (JF.isWindows()) {
return WindowsAPI.getInstance().peekConsole();
} else {
return LinuxAPI.getInstance().peekConsole();
}
}
public static <T> void printArray(T[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(a[i].toString());
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(byte[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Byte.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(short[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Short.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(int[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Integer.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(long[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Long.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(float[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Float.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void printArray(double[] a) {
StringBuilder sb = new StringBuilder();
sb.append("{");
int size = a.length;
for(int i=0;i<size;i++) {
if (i > 0) sb.append(",");
sb.append(Double.toString(a[i]));
}
sb.append("}");
System.out.println(sb.toString());
}
public static void main(String[] args) {
enableConsoleMode();
InputStream is = getInputStream();
JFLog.log("Press q to quit");
int ch = 0;
while (true) {
try {
if (kbhit()) {
ch = is.read();
JFLog.log("ch=" + ch);
} else {
JF.sleep(100);
}
if (ch=='q') break;
} catch (Exception e) {
break;
}
}
disableConsoleMode();
}
}