|
1 /* |
|
2 * Copyright (c) 2002-2018, the original author or authors. |
|
3 * |
|
4 * This software is distributable under the BSD license. See the terms of the |
|
5 * BSD license in the documentation provided with this software. |
|
6 * |
|
7 * http://www.opensource.org/licenses/bsd-license.php |
|
8 */ |
|
9 package jdk.internal.org.jline.utils; |
|
10 |
|
11 import java.io.IOException; |
|
12 import java.io.InputStream; |
|
13 |
|
14 /** |
|
15 * Non blocking input stream |
|
16 */ |
|
17 public abstract class NonBlockingInputStream extends InputStream { |
|
18 |
|
19 public static final int EOF = -1; |
|
20 public static final int READ_EXPIRED = -2; |
|
21 |
|
22 /** |
|
23 * Reads the next byte of data from the input stream. The value byte is |
|
24 * returned as an <code>int</code> in the range <code>0</code> to |
|
25 * <code>255</code>. If no byte is available because the end of the stream |
|
26 * has been reached, the value <code>-1</code> is returned. This method |
|
27 * blocks until input data is available, the end of the stream is detected, |
|
28 * or an exception is thrown. |
|
29 * |
|
30 * @return the next byte of data, or <code>-1</code> if the end of the |
|
31 * stream is reached. |
|
32 * @exception IOException if an I/O error occurs. |
|
33 */ |
|
34 @Override |
|
35 public int read() throws IOException { |
|
36 return read(0L, false); |
|
37 } |
|
38 |
|
39 /** |
|
40 * Peeks to see if there is a byte waiting in the input stream without |
|
41 * actually consuming the byte. |
|
42 * |
|
43 * @param timeout The amount of time to wait, 0 == forever |
|
44 * @return -1 on eof, -2 if the timeout expired with no available input |
|
45 * or the character that was read (without consuming it). |
|
46 * @exception IOException if an I/O error occurs. |
|
47 */ |
|
48 public int peek(long timeout) throws IOException { |
|
49 return read(timeout, true); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Attempts to read a character from the input stream for a specific |
|
54 * period of time. |
|
55 * |
|
56 * @param timeout The amount of time to wait for the character |
|
57 * @return The character read, -1 if EOF is reached, |
|
58 * or -2 if the read timed out. |
|
59 * @exception IOException if an I/O error occurs. |
|
60 */ |
|
61 public int read(long timeout) throws IOException { |
|
62 return read(timeout, false); |
|
63 } |
|
64 |
|
65 public int read(byte b[], int off, int len) throws IOException { |
|
66 if (b == null) { |
|
67 throw new NullPointerException(); |
|
68 } else if (off < 0 || len < 0 || len > b.length - off) { |
|
69 throw new IndexOutOfBoundsException(); |
|
70 } else if (len == 0) { |
|
71 return 0; |
|
72 } |
|
73 int c = read(); |
|
74 if (c == EOF) { |
|
75 return EOF; |
|
76 } |
|
77 b[off] = (byte)c; |
|
78 return 1; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Shuts down the thread that is handling blocking I/O if any. Note that if the |
|
83 * thread is currently blocked waiting for I/O it may not actually |
|
84 * shut down until the I/O is received. |
|
85 */ |
|
86 public void shutdown() { |
|
87 } |
|
88 |
|
89 public abstract int read(long timeout, boolean isPeek) throws IOException; |
|
90 |
|
91 } |