1 /* |
1 /* |
2 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
51 */ |
52 */ |
52 |
53 |
53 public abstract class Reader implements Readable, Closeable { |
54 public abstract class Reader implements Readable, Closeable { |
54 |
55 |
55 private static final int TRANSFER_BUFFER_SIZE = 8192; |
56 private static final int TRANSFER_BUFFER_SIZE = 8192; |
|
57 |
|
58 /** |
|
59 * Returns a new {@code Reader} that reads no characters. The returned |
|
60 * stream is initially open. The stream is closed by calling the |
|
61 * {@code close()} method. Subsequent calls to {@code close()} have no |
|
62 * effect. |
|
63 * |
|
64 * <p> While the stream is open, the {@code read()}, {@code read(char[])}, |
|
65 * {@code read(char[], int, int)}, {@code read(Charbuffer)}, {@code |
|
66 * ready())}, {@code skip(long)}, and {@code transferTo()} methods all |
|
67 * behave as if end of stream has been reached. After the stream has been |
|
68 * closed, these methods all throw {@code IOException}. |
|
69 * |
|
70 * <p> The {@code markSupported()} method returns {@code false}. The |
|
71 * {@code mark()} method does nothing, and the {@code reset()} method |
|
72 * throws {@code IOException}. |
|
73 * |
|
74 * <p> The {@link #lock object} used to synchronize operations on the |
|
75 * returned {@code Reader} is not specified. |
|
76 * |
|
77 * @return a {@code Reader} which reads no characters |
|
78 * |
|
79 * @since 11 |
|
80 */ |
|
81 public static Reader nullReader() { |
|
82 return new Reader() { |
|
83 private volatile boolean closed; |
|
84 |
|
85 private void ensureOpen() throws IOException { |
|
86 if (closed) { |
|
87 throw new IOException("Stream closed"); |
|
88 } |
|
89 } |
|
90 |
|
91 @Override |
|
92 public int read() throws IOException { |
|
93 ensureOpen(); |
|
94 return -1; |
|
95 } |
|
96 |
|
97 @Override |
|
98 public int read(char[] cbuf, int off, int len) throws IOException { |
|
99 Objects.checkFromIndexSize(off, len, cbuf.length); |
|
100 ensureOpen(); |
|
101 if (len == 0) { |
|
102 return 0; |
|
103 } |
|
104 return -1; |
|
105 } |
|
106 |
|
107 @Override |
|
108 public int read(CharBuffer target) throws IOException { |
|
109 Objects.requireNonNull(target); |
|
110 ensureOpen(); |
|
111 if (target.hasRemaining()) { |
|
112 return -1; |
|
113 } |
|
114 return 0; |
|
115 } |
|
116 |
|
117 @Override |
|
118 public long skip(long n) throws IOException { |
|
119 ensureOpen(); |
|
120 return 0L; |
|
121 } |
|
122 |
|
123 @Override |
|
124 public long transferTo(Writer out) throws IOException { |
|
125 Objects.requireNonNull(out); |
|
126 ensureOpen(); |
|
127 return 0L; |
|
128 } |
|
129 |
|
130 @Override |
|
131 public void close() { |
|
132 closed = true; |
|
133 } |
|
134 }; |
|
135 } |
56 |
136 |
57 /** |
137 /** |
58 * The object used to synchronize operations on this stream. For |
138 * The object used to synchronize operations on this stream. For |
59 * efficiency, a character-stream object may use an object other than |
139 * efficiency, a character-stream object may use an object other than |
60 * itself to protect critical sections. A subclass should therefore use |
140 * itself to protect critical sections. A subclass should therefore use |