InputStream

 1 package java.io;
 2 
 3 public abstract class InputStream implements Closeable {
 4     
 5     private static final int MAX_SKIP_BUFFER_SIZE = 2048;
 6     
 7     public abstract int read() throws IOException;
 8     
 9     public int read(byte[] b) throws IOException {
10         return read(b, 0, b.length);
11     }
12     
13     public int read(byte[] b, int off, int len) throws IOException {
14         if (b == null) {
15             throw new NullPointerException();
16         } else if (off < 0 || len < 0 || len > b.length - off) {
17             throw new IndexOutOfBoundsException();
18         } else if (len ==0) {
19             return 0;
20         }
21         
22         int c = read();
23         
24         if (c == -1) {
25             return -1;
26         }
27         b[off] = (byte)c;
28         
29         int i = 1;
30         try {
31             for (; i < len; i++) {
32                 c = read();
33                 if (c == -1) {
34                     break;
35                 }
36                 b[off + i] = (byte)c;
37             }
38         } catch (IOException ee) {
39         }
40         
41         return i;
42     }
43     
44     public long skip(long n) throws IOException {
45         long remaining = n;
46         int nr;
47         
48         if (n <= 0) {
49             return 0;
50         }
51         
52         int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
53         byte[] skipBuffer = new byte[size];
54         while (remaining > 0) {
55             nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
56             if (nr < 0) {
57                 break;
58             }
59             remaining -= nr;
60         }
61         return n - remaining;    
62     } 
63     
64     public int available() throws IOException {
65         return 0;
66     }
67     
68     public void close() throws IOException {}
69     
70     public synchronized void mark(int readlimit) {}
71     
72     public synchronized void reset() throws IOException {
73         throw new IOException("mark/reset not supported");
74     }
75     
76     public boolean markSupported() {
77         return false;
78     }
79     
80 }

 read()方法阻塞的情况:

执行上述代码,在控制台输入数据之前程序将一直阻塞在read方法

原文地址:https://www.cnblogs.com/hanw1991/p/7484504.html