Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
305 lines (279 loc) · 10.9 KB

File metadata and controls

305 lines (279 loc) · 10.9 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.io;
import java.io.*;
import com.sun.max.lang.*;
/**
* Supplementary java.io utils.
*
* @author Bernd Mathiske
*/
public final class Streams {
private Streams() {
}
public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
final byte[] buffer = new byte[8192];
int count;
while ((count = inputStream.read(buffer, 0, buffer.length)) > 0) {
outputStream.write(buffer, 0, count);
}
outputStream.flush();
}
public static void copy(Reader reader, Writer writer) throws IOException {
final char[] buffer = new char[8192];
int count;
while ((count = reader.read(buffer, 0, buffer.length)) > 0) {
writer.write(buffer, 0, count);
}
writer.flush();
}
public static boolean equals(InputStream inputStream1, InputStream inputStream2) throws IOException {
final int n = 8192;
final byte[] buffer1 = new byte[n];
final byte[] buffer2 = new byte[n];
while (true) {
final int n1 = inputStream1.read(buffer1, 0, n);
final int n2 = inputStream2.read(buffer2, 0, n);
if (n1 != n2) {
return false;
}
if (n1 <= 0) {
return true;
}
if (!Bytes.equals(buffer1, buffer2, n)) {
return false;
}
}
}
public static final class Redirector extends Thread {
private final InputStream inputStream;
private final OutputStream outputStream;
private final String name;
private final int maxLines;
private final Process process;
private boolean closed;
private Redirector(Process process, InputStream inputStream, OutputStream outputStream, String name, int maxLines) {
super("StreamRedirector{" + name + "}");
this.inputStream = inputStream;
this.outputStream = outputStream;
this.name = name;
this.maxLines = maxLines;
this.process = process;
start();
}
public void close() {
closed = true;
}
@Override
public void run() {
try {
try {
int line = 1;
while (!closed) {
if (inputStream.available() == 0) {
// A busy yielding loop is used so that this thread can be
// stopped via a call to close() by another thread. Otherwise,
// this thread could be blocked forever on an input stream
// that is not closed and does not have any available data.
// The prime example of course is System.in.
Thread.yield();
// wait for a few milliseconds to avoid eating too much CPU.
Thread.sleep(10);
continue;
}
final int b = inputStream.read();
if (b < 0) {
return;
}
if (line <= maxLines) {
outputStream.write(b);
}
if (b == '\n') {
if (line == maxLines) {
outputStream.write(("<redirected stream concatenated after " + maxLines + " lines>" + System.getProperty("line.separator", "\n")).getBytes());
}
++line;
}
}
outputStream.flush();
} catch (IOException ioe) {
try {
process.exitValue();
// This just means the process was terminated and the relevant pipe no longer exists
} catch (IllegalThreadStateException e) {
// Some other unexpected IO error occurred -> rethrow
throw e;
}
}
} catch (Throwable throwable) {
if (name != null) {
System.err.println("Error while redirecting sub-process stream for \"" + name + "\"");
}
throwable.printStackTrace();
}
}
}
public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream, String name, int maxLines) {
return new Redirector(process, inputStream, outputStream, name, maxLines);
}
public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream, String name) {
return redirect(process, inputStream, outputStream, name, Integer.MAX_VALUE);
}
public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream) {
return redirect(process, inputStream, outputStream, null, Integer.MAX_VALUE);
}
/**
* Scans a given buffered input stream for a given sequence of bytes. If the sequence is found, then the read
* position of the stream is immediately after the sequence. Otherwise, the read position is at the end of the
* stream.
*
* @param stream
* the stream to search
* @param bytes
* the byte pattern to search for
* @return true if {@code bytes} is found in {@code stream}
*/
public static boolean search(BufferedInputStream stream, byte[] bytes) throws IOException {
if (bytes.length == 0) {
return true;
}
int b1 = stream.read();
top:
while (b1 != -1) {
if (b1 == (bytes[0] & 0xff)) {
for (int i = 1; i < bytes.length; ++i) {
b1 = stream.read();
if (b1 != (bytes[i] & 0xff)) {
continue top;
}
}
return true;
}
b1 = stream.read();
}
return false;
}
/**
* Scans a given buffered reader for a given sequence of characters. If the sequence is found, then the read
* position of the reader is immediately after the sequence. Otherwise, the read position is at the end of the
* reader.
*
* @param reader
* the reader to search
* @param chars
* the char pattern to search for
* @return true if {@code chars} is found in {@code reader}
*/
public static boolean search(BufferedReader reader, char[] chars) throws IOException {
if (chars.length == 0) {
return true;
}
int c1 = reader.read();
top:
while (c1 != -1) {
if (c1 == chars[0]) {
for (int i = 1; i < chars.length; ++i) {
c1 = reader.read();
if (c1 != chars[i]) {
continue top;
}
}
return true;
}
c1 = reader.read();
}
return false;
}
public static boolean startsWith(BufferedInputStream bufferedInputStream, byte[] bytes) throws IOException {
final byte[] data = new byte[bytes.length];
bufferedInputStream.mark(bytes.length);
try {
readFully(bufferedInputStream, data);
if (java.util.Arrays.equals(data, bytes)) {
return true;
}
} catch (IOException ioException) {
// This is OK
}
bufferedInputStream.reset();
return false;
}
public static boolean startsWith(BufferedReader bufferedReader, char[] chars) throws IOException {
final char[] data = new char[chars.length];
bufferedReader.mark(chars.length);
try {
readFully(bufferedReader, data);
if (java.util.Arrays.equals(data, chars)) {
return true;
}
} catch (IOException ioException) {
// This is OK
}
bufferedReader.reset();
return false;
}
/**
* @see DataInput#readFully(byte[])
*/
public static byte[] readFully(InputStream stream, byte[] buffer) throws IOException {
return readFully(stream, buffer, 0, buffer.length);
}
/**
* @see DataInput#readFully(byte[], int, int)
*/
public static byte[] readFully(InputStream stream, byte[] buffer, int offset, int length) throws IOException {
if (length < 0) {
throw new IndexOutOfBoundsException();
}
int n = 0;
while (n < length) {
final int count = stream.read(buffer, offset + n, length - n);
if (count < 0) {
throw new EOFException((length - n) + " of " + length + " bytes unread");
}
n += count;
}
return buffer;
}
/**
* The analogous operation as {@link DataInput#readFully(byte[])} for {@link Reader}s.
*/
public static char[] readFully(Reader reader, char[] buffer) throws IOException {
return readFully(reader, buffer, 0, buffer.length);
}
/**
* The analogous operation as {@link DataInput#readFully(byte[], int, int)} for {@link Reader}s.
*/
public static char[] readFully(Reader reader, char[] buffer, int offset, int length) throws IOException {
if (length < 0) {
throw new IndexOutOfBoundsException();
}
int n = 0;
while (n < length) {
final int count = reader.read(buffer, offset + n, length - n);
if (count < 0) {
throw new TruncatedInputException((length - n) + " of " + length + " characters unread", n);
}
n += count;
}
return buffer;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.