1 /* |
1 /* |
2 * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1996, 2019, 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 |
45 /** |
45 /** |
46 * Creates a new pushback reader with a pushback buffer of the given size. |
46 * Creates a new pushback reader with a pushback buffer of the given size. |
47 * |
47 * |
48 * @param in The reader from which characters will be read |
48 * @param in The reader from which characters will be read |
49 * @param size The size of the pushback buffer |
49 * @param size The size of the pushback buffer |
50 * @exception IllegalArgumentException if {@code size <= 0} |
50 * @throws IllegalArgumentException if {@code size <= 0} |
51 */ |
51 */ |
52 public PushbackReader(Reader in, int size) { |
52 public PushbackReader(Reader in, int size) { |
53 super(in); |
53 super(in); |
54 if (size <= 0) { |
54 if (size <= 0) { |
55 throw new IllegalArgumentException("size <= 0"); |
55 throw new IllegalArgumentException("size <= 0"); |
99 * @param len Maximum number of characters to read |
99 * @param len Maximum number of characters to read |
100 * |
100 * |
101 * @return The number of characters read, or -1 if the end of the |
101 * @return The number of characters read, or -1 if the end of the |
102 * stream has been reached |
102 * stream has been reached |
103 * |
103 * |
104 * @exception IOException If an I/O error occurs |
104 * @throws IOException If an I/O error occurs |
105 * @exception IndexOutOfBoundsException {@inheritDoc} |
105 * @throws IndexOutOfBoundsException {@inheritDoc} |
106 */ |
106 */ |
107 public int read(char cbuf[], int off, int len) throws IOException { |
107 public int read(char cbuf[], int off, int len) throws IOException { |
108 synchronized (lock) { |
108 synchronized (lock) { |
109 ensureOpen(); |
109 ensureOpen(); |
110 try { |
110 try { |
140 } |
140 } |
141 |
141 |
142 /** |
142 /** |
143 * Pushes back a single character by copying it to the front of the |
143 * Pushes back a single character by copying it to the front of the |
144 * pushback buffer. After this method returns, the next character to be read |
144 * pushback buffer. After this method returns, the next character to be read |
145 * will have the value <code>(char)c</code>. |
145 * will have the value {@code (char)c}. |
146 * |
146 * |
147 * @param c The int value representing a character to be pushed back |
147 * @param c The int value representing a character to be pushed back |
148 * |
148 * |
149 * @exception IOException If the pushback buffer is full, |
149 * @throws IOException If the pushback buffer is full, |
150 * or if some other I/O error occurs |
150 * or if some other I/O error occurs |
151 */ |
151 */ |
152 public void unread(int c) throws IOException { |
152 public void unread(int c) throws IOException { |
153 synchronized (lock) { |
153 synchronized (lock) { |
154 ensureOpen(); |
154 ensureOpen(); |
155 if (pos == 0) |
155 if (pos == 0) |
159 } |
159 } |
160 |
160 |
161 /** |
161 /** |
162 * Pushes back a portion of an array of characters by copying it to the |
162 * Pushes back a portion of an array of characters by copying it to the |
163 * front of the pushback buffer. After this method returns, the next |
163 * front of the pushback buffer. After this method returns, the next |
164 * character to be read will have the value <code>cbuf[off]</code>, the |
164 * character to be read will have the value {@code cbuf[off]}, the |
165 * character after that will have the value <code>cbuf[off+1]</code>, and |
165 * character after that will have the value {@code cbuf[off+1]}, and |
166 * so forth. |
166 * so forth. |
167 * |
167 * |
168 * @param cbuf Character array |
168 * @param cbuf Character array |
169 * @param off Offset of first character to push back |
169 * @param off Offset of first character to push back |
170 * @param len Number of characters to push back |
170 * @param len Number of characters to push back |
171 * |
171 * |
172 * @exception IOException If there is insufficient room in the pushback |
172 * @throws IOException If there is insufficient room in the pushback |
173 * buffer, or if some other I/O error occurs |
173 * buffer, or if some other I/O error occurs |
174 */ |
174 */ |
175 public void unread(char cbuf[], int off, int len) throws IOException { |
175 public void unread(char cbuf[], int off, int len) throws IOException { |
176 synchronized (lock) { |
176 synchronized (lock) { |
177 ensureOpen(); |
177 ensureOpen(); |
183 } |
183 } |
184 |
184 |
185 /** |
185 /** |
186 * Pushes back an array of characters by copying it to the front of the |
186 * Pushes back an array of characters by copying it to the front of the |
187 * pushback buffer. After this method returns, the next character to be |
187 * pushback buffer. After this method returns, the next character to be |
188 * read will have the value <code>cbuf[0]</code>, the character after that |
188 * read will have the value {@code cbuf[0]}, the character after that |
189 * will have the value <code>cbuf[1]</code>, and so forth. |
189 * will have the value {@code cbuf[1]}, and so forth. |
190 * |
190 * |
191 * @param cbuf Character array to push back |
191 * @param cbuf Character array to push back |
192 * |
192 * |
193 * @exception IOException If there is insufficient room in the pushback |
193 * @throws IOException If there is insufficient room in the pushback |
194 * buffer, or if some other I/O error occurs |
194 * buffer, or if some other I/O error occurs |
195 */ |
195 */ |
196 public void unread(char cbuf[]) throws IOException { |
196 public void unread(char cbuf[]) throws IOException { |
197 unread(cbuf, 0, cbuf.length); |
197 unread(cbuf, 0, cbuf.length); |
198 } |
198 } |
199 |
199 |
200 /** |
200 /** |
201 * Tells whether this stream is ready to be read. |
201 * Tells whether this stream is ready to be read. |
202 * |
202 * |
203 * @exception IOException If an I/O error occurs |
203 * @throws IOException If an I/O error occurs |
204 */ |
204 */ |
205 public boolean ready() throws IOException { |
205 public boolean ready() throws IOException { |
206 synchronized (lock) { |
206 synchronized (lock) { |
207 ensureOpen(); |
207 ensureOpen(); |
208 return (pos < buf.length) || super.ready(); |
208 return (pos < buf.length) || super.ready(); |
209 } |
209 } |
210 } |
210 } |
211 |
211 |
212 /** |
212 /** |
213 * Marks the present position in the stream. The <code>mark</code> |
213 * Marks the present position in the stream. The {@code mark} |
214 * for class <code>PushbackReader</code> always throws an exception. |
214 * for class {@code PushbackReader} always throws an exception. |
215 * |
215 * |
216 * @exception IOException Always, since mark is not supported |
216 * @throws IOException Always, since mark is not supported |
217 */ |
217 */ |
218 public void mark(int readAheadLimit) throws IOException { |
218 public void mark(int readAheadLimit) throws IOException { |
219 throw new IOException("mark/reset not supported"); |
219 throw new IOException("mark/reset not supported"); |
220 } |
220 } |
221 |
221 |
222 /** |
222 /** |
223 * Resets the stream. The <code>reset</code> method of |
223 * Resets the stream. The {@code reset} method of |
224 * <code>PushbackReader</code> always throws an exception. |
224 * {@code PushbackReader} always throws an exception. |
225 * |
225 * |
226 * @exception IOException Always, since reset is not supported |
226 * @throws IOException Always, since reset is not supported |
227 */ |
227 */ |
228 public void reset() throws IOException { |
228 public void reset() throws IOException { |
229 throw new IOException("mark/reset not supported"); |
229 throw new IOException("mark/reset not supported"); |
230 } |
230 } |
231 |
231 |
242 * it. Once the stream has been closed, further read(), |
242 * it. Once the stream has been closed, further read(), |
243 * unread(), ready(), or skip() invocations will throw an IOException. |
243 * unread(), ready(), or skip() invocations will throw an IOException. |
244 * Closing a previously closed stream has no effect. This method will block |
244 * Closing a previously closed stream has no effect. This method will block |
245 * while there is another thread blocking on the reader. |
245 * while there is another thread blocking on the reader. |
246 * |
246 * |
247 * @exception IOException If an I/O error occurs |
247 * @throws IOException If an I/O error occurs |
248 */ |
248 */ |
249 public void close() throws IOException { |
249 public void close() throws IOException { |
250 synchronized (lock) { |
250 synchronized (lock) { |
251 super.close(); |
251 super.close(); |
252 buf = null; |
252 buf = null; |
255 |
255 |
256 /** |
256 /** |
257 * Skips characters. This method will block until some characters are |
257 * Skips characters. This method will block until some characters are |
258 * available, an I/O error occurs, or the end of the stream is reached. |
258 * available, an I/O error occurs, or the end of the stream is reached. |
259 * |
259 * |
260 * @param n The number of characters to skip |
260 * @param n The number of characters to skip |
261 * |
261 * |
262 * @return The number of characters actually skipped |
262 * @return The number of characters actually skipped |
263 * |
263 * |
264 * @exception IllegalArgumentException If <code>n</code> is negative. |
264 * @throws IllegalArgumentException If {@code n} is negative. |
265 * @exception IOException If an I/O error occurs |
265 * @throws IOException If an I/O error occurs |
266 */ |
266 */ |
267 public long skip(long n) throws IOException { |
267 public long skip(long n) throws IOException { |
268 if (n < 0L) |
268 if (n < 0L) |
269 throw new IllegalArgumentException("skip value is negative"); |
269 throw new IllegalArgumentException("skip value is negative"); |
270 synchronized (lock) { |
270 synchronized (lock) { |