46 * @see PipedReader 47 * @see StringReader 48 * @see Writer 49 * 50 * @author Mark Reinhold 51 * @since 1.1 52 */ 53 54 public abstract class Reader implements Readable, Closeable { 55 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 { 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 }; | 46 * @see PipedReader 47 * @see StringReader 48 * @see Writer 49 * 50 * @author Mark Reinhold 51 * @since 1.1 52 */ 53 54 public abstract class Reader implements Readable, Closeable { 55 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()} and {@code reset()} methods throw an {@code IOException}. 72 * 73 * <p> The {@link #lock object} used to synchronize operations on the 74 * returned {@code Reader} is not specified. 75 * 76 * @return a {@code Reader} which reads no characters 77 * 78 * @since 11 79 */ 80 public static Reader nullReader() { 81 return new Reader() { 82 private volatile boolean closed; 83 84 private void ensureOpen() throws IOException { 85 if (closed) { 86 throw new IOException("Stream closed"); 87 } 88 } 89 90 @Override 91 public int read() throws IOException { 94 } 95 96 @Override 97 public int read(char[] cbuf, int off, int len) throws IOException { 98 Objects.checkFromIndexSize(off, len, cbuf.length); 99 ensureOpen(); 100 if (len == 0) { 101 return 0; 102 } 103 return -1; 104 } 105 106 @Override 107 public int read(CharBuffer target) throws IOException { 108 Objects.requireNonNull(target); 109 ensureOpen(); 110 if (target.hasRemaining()) { 111 return -1; 112 } 113 return 0; 114 } 115 116 @Override 117 public boolean ready() throws IOException { 118 ensureOpen(); 119 return false; 120 } 121 122 @Override 123 public long skip(long n) throws IOException { 124 ensureOpen(); 125 return 0L; 126 } 127 128 @Override 129 public long transferTo(Writer out) throws IOException { 130 Objects.requireNonNull(out); 131 ensureOpen(); 132 return 0L; 133 } 134 135 @Override 136 public void close() { 137 closed = true; 138 } 139 }; |