comparison graal/com.oracle.max.base/src/com/sun/max/io/Streams.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max.io;
24
25 import java.io.*;
26
27 import com.sun.max.lang.*;
28
29 /**
30 * Supplementary java.io utils.
31 */
32 public final class Streams {
33
34 private Streams() {
35 }
36
37 public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
38 final byte[] buffer = new byte[8192];
39 int count;
40 while ((count = inputStream.read(buffer, 0, buffer.length)) > 0) {
41 outputStream.write(buffer, 0, count);
42 }
43 outputStream.flush();
44 }
45
46 public static void copy(Reader reader, Writer writer) throws IOException {
47 final char[] buffer = new char[8192];
48 int count;
49 while ((count = reader.read(buffer, 0, buffer.length)) > 0) {
50 writer.write(buffer, 0, count);
51 }
52 writer.flush();
53 }
54
55 public static boolean equals(InputStream inputStream1, InputStream inputStream2) throws IOException {
56 final int n = 8192;
57 final byte[] buffer1 = new byte[n];
58 final byte[] buffer2 = new byte[n];
59 while (true) {
60 final int n1 = inputStream1.read(buffer1, 0, n);
61 final int n2 = inputStream2.read(buffer2, 0, n);
62 if (n1 != n2) {
63 return false;
64 }
65 if (n1 <= 0) {
66 return true;
67 }
68 if (!Bytes.equals(buffer1, buffer2, n)) {
69 return false;
70 }
71 }
72 }
73
74 public static final class Redirector extends Thread {
75
76 private final InputStream inputStream;
77 private final OutputStream outputStream;
78 private final String name;
79 private final int maxLines;
80 private final Process process;
81 private boolean closed;
82
83 private Redirector(Process process, InputStream inputStream, OutputStream outputStream, String name, int maxLines) {
84 super("StreamRedirector{" + name + "}");
85 this.inputStream = inputStream;
86 this.outputStream = outputStream;
87 this.name = name;
88 this.maxLines = maxLines;
89 this.process = process;
90 start();
91 }
92
93 public void close() {
94 closed = true;
95 }
96
97 @Override
98 public void run() {
99 try {
100 try {
101 int line = 1;
102 while (!closed) {
103 if (inputStream.available() == 0) {
104 // A busy yielding loop is used so that this thread can be
105 // stopped via a call to close() by another thread. Otherwise,
106 // this thread could be blocked forever on an input stream
107 // that is not closed and does not have any available data.
108 // The prime example of course is System.in.
109 Thread.yield();
110 // wait for a few milliseconds to avoid eating too much CPU.
111 Thread.sleep(10);
112 continue;
113 }
114
115 final int b = inputStream.read();
116 if (b < 0) {
117 return;
118 }
119 if (line <= maxLines) {
120 outputStream.write(b);
121 }
122 if (b == '\n') {
123 if (line == maxLines) {
124 outputStream.write(("<redirected stream concatenated after " + maxLines + " lines>" + System.getProperty("line.separator", "\n")).getBytes());
125 }
126 ++line;
127 }
128 }
129 outputStream.flush();
130 } catch (IOException ioe) {
131 try {
132 process.exitValue();
133
134 // This just means the process was terminated and the relevant pipe no longer exists
135 } catch (IllegalThreadStateException e) {
136 // Some other unexpected IO error occurred -> rethrow
137 throw e;
138 }
139 }
140 } catch (Throwable throwable) {
141 if (name != null) {
142 System.err.println("Error while redirecting sub-process stream for \"" + name + "\"");
143 }
144 throwable.printStackTrace();
145 }
146 }
147
148 }
149
150 public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream, String name, int maxLines) {
151 return new Redirector(process, inputStream, outputStream, name, maxLines);
152 }
153
154 public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream, String name) {
155 return redirect(process, inputStream, outputStream, name, Integer.MAX_VALUE);
156 }
157
158 public static Redirector redirect(Process process, InputStream inputStream, OutputStream outputStream) {
159 return redirect(process, inputStream, outputStream, null, Integer.MAX_VALUE);
160 }
161
162 /**
163 * Scans a given buffered input stream for a given sequence of bytes. If the sequence is found, then the read
164 * position of the stream is immediately after the sequence. Otherwise, the read position is at the end of the
165 * stream.
166 *
167 * @param stream
168 * the stream to search
169 * @param bytes
170 * the byte pattern to search for
171 * @return true if {@code bytes} is found in {@code stream}
172 */
173 public static boolean search(BufferedInputStream stream, byte[] bytes) throws IOException {
174 if (bytes.length == 0) {
175 return true;
176 }
177 int b1 = stream.read();
178 top:
179 while (b1 != -1) {
180 if (b1 == (bytes[0] & 0xff)) {
181 for (int i = 1; i < bytes.length; ++i) {
182 b1 = stream.read();
183 if (b1 != (bytes[i] & 0xff)) {
184 continue top;
185 }
186 }
187 return true;
188 }
189 b1 = stream.read();
190 }
191 return false;
192 }
193
194 /**
195 * Scans a given buffered reader for a given sequence of characters. If the sequence is found, then the read
196 * position of the reader is immediately after the sequence. Otherwise, the read position is at the end of the
197 * reader.
198 *
199 * @param reader
200 * the reader to search
201 * @param chars
202 * the char pattern to search for
203 * @return true if {@code chars} is found in {@code reader}
204 */
205 public static boolean search(BufferedReader reader, char[] chars) throws IOException {
206 if (chars.length == 0) {
207 return true;
208 }
209 int c1 = reader.read();
210 top:
211 while (c1 != -1) {
212 if (c1 == chars[0]) {
213 for (int i = 1; i < chars.length; ++i) {
214 c1 = reader.read();
215 if (c1 != chars[i]) {
216 continue top;
217 }
218 }
219 return true;
220 }
221 c1 = reader.read();
222 }
223 return false;
224 }
225
226 public static boolean startsWith(BufferedInputStream bufferedInputStream, byte[] bytes) throws IOException {
227 final byte[] data = new byte[bytes.length];
228 bufferedInputStream.mark(bytes.length);
229 try {
230 readFully(bufferedInputStream, data);
231 if (java.util.Arrays.equals(data, bytes)) {
232 return true;
233 }
234 } catch (IOException ioException) {
235 // This is OK
236 }
237 bufferedInputStream.reset();
238 return false;
239 }
240
241 public static boolean startsWith(BufferedReader bufferedReader, char[] chars) throws IOException {
242 final char[] data = new char[chars.length];
243 bufferedReader.mark(chars.length);
244 try {
245 readFully(bufferedReader, data);
246 if (java.util.Arrays.equals(data, chars)) {
247 return true;
248 }
249 } catch (IOException ioException) {
250 // This is OK
251 }
252 bufferedReader.reset();
253 return false;
254 }
255
256 /**
257 * @see DataInput#readFully(byte[])
258 */
259 public static byte[] readFully(InputStream stream, byte[] buffer) throws IOException {
260 return readFully(stream, buffer, 0, buffer.length);
261 }
262
263 /**
264 * @see DataInput#readFully(byte[], int, int)
265 */
266 public static byte[] readFully(InputStream stream, byte[] buffer, int offset, int length) throws IOException {
267 if (length < 0) {
268 throw new IndexOutOfBoundsException();
269 }
270 int n = 0;
271 while (n < length) {
272 final int count = stream.read(buffer, offset + n, length - n);
273 if (count < 0) {
274 throw new EOFException((length - n) + " of " + length + " bytes unread");
275 }
276 n += count;
277 }
278 return buffer;
279 }
280
281 /**
282 * The analogous operation as {@link DataInput#readFully(byte[])} for {@link Reader}s.
283 */
284 public static char[] readFully(Reader reader, char[] buffer) throws IOException {
285 return readFully(reader, buffer, 0, buffer.length);
286 }
287
288 /**
289 * The analogous operation as {@link DataInput#readFully(byte[], int, int)} for {@link Reader}s.
290 */
291 public static char[] readFully(Reader reader, char[] buffer, int offset, int length) throws IOException {
292 if (length < 0) {
293 throw new IndexOutOfBoundsException();
294 }
295 int n = 0;
296 while (n < length) {
297 final int count = reader.read(buffer, offset + n, length - n);
298 if (count < 0) {
299 throw new TruncatedInputException((length - n) + " of " + length + " characters unread", n);
300 }
301 n += count;
302 }
303 return buffer;
304 }
305 }