230 * @throws IllegalFormatException
231 * If a format string contains an illegal syntax, a format
232 * specifier that is incompatible with the given arguments,
233 * insufficient arguments given the format string, or other
234 * illegal conditions. For specification of all possible
235 * formatting errors, see the <a
236 * href="../util/Formatter.html#detail">Details</a> section
237 * of the formatter class specification.
238 *
239 * @throws IOError
240 * If an I/O error occurs.
241 *
242 * @return A string containing the line read from the console, not
243 * including any line-termination characters, or {@code null}
244 * if an end of stream has been reached.
245 */
246 public String readLine(String fmt, Object ... args) {
247 String line = null;
248 synchronized (writeLock) {
249 synchronized(readLock) {
250 if (fmt.length() != 0)
251 pw.format(fmt, args);
252 try {
253 char[] ca = readline(false);
254 if (ca != null)
255 line = new String(ca);
256 } catch (IOException x) {
257 throw new IOError(x);
258 }
259 }
260 }
261 return line;
262 }
263
264 /**
265 * Reads a single line of text from the console.
266 *
267 * @throws IOError
268 * If an I/O error occurs.
269 *
270 * @return A string containing the line read from the console, not
302 *
303 * @throws IOError
304 * If an I/O error occurs.
305 *
306 * @return A character array containing the password or passphrase read
307 * from the console, not including any line-termination characters,
308 * or {@code null} if an end of stream has been reached.
309 */
310 public char[] readPassword(String fmt, Object ... args) {
311 char[] passwd = null;
312 synchronized (writeLock) {
313 synchronized(readLock) {
314 installShutdownHook();
315 try {
316 restoreEcho = echo(false);
317 } catch (IOException x) {
318 throw new IOError(x);
319 }
320 IOError ioe = null;
321 try {
322 if (fmt.length() != 0)
323 pw.format(fmt, args);
324 passwd = readline(true);
325 } catch (IOException x) {
326 ioe = new IOError(x);
327 } finally {
328 try {
329 if (restoreEcho)
330 restoreEcho = echo(true);
331 } catch (IOException x) {
332 if (ioe == null)
333 ioe = new IOError(x);
334 else
335 ioe.addSuppressed(x);
336 }
337 if (ioe != null)
338 throw ioe;
339 }
340 pw.println();
341 }
342 }
|
230 * @throws IllegalFormatException
231 * If a format string contains an illegal syntax, a format
232 * specifier that is incompatible with the given arguments,
233 * insufficient arguments given the format string, or other
234 * illegal conditions. For specification of all possible
235 * formatting errors, see the <a
236 * href="../util/Formatter.html#detail">Details</a> section
237 * of the formatter class specification.
238 *
239 * @throws IOError
240 * If an I/O error occurs.
241 *
242 * @return A string containing the line read from the console, not
243 * including any line-termination characters, or {@code null}
244 * if an end of stream has been reached.
245 */
246 public String readLine(String fmt, Object ... args) {
247 String line = null;
248 synchronized (writeLock) {
249 synchronized(readLock) {
250 if (!fmt.isEmpty())
251 pw.format(fmt, args);
252 try {
253 char[] ca = readline(false);
254 if (ca != null)
255 line = new String(ca);
256 } catch (IOException x) {
257 throw new IOError(x);
258 }
259 }
260 }
261 return line;
262 }
263
264 /**
265 * Reads a single line of text from the console.
266 *
267 * @throws IOError
268 * If an I/O error occurs.
269 *
270 * @return A string containing the line read from the console, not
302 *
303 * @throws IOError
304 * If an I/O error occurs.
305 *
306 * @return A character array containing the password or passphrase read
307 * from the console, not including any line-termination characters,
308 * or {@code null} if an end of stream has been reached.
309 */
310 public char[] readPassword(String fmt, Object ... args) {
311 char[] passwd = null;
312 synchronized (writeLock) {
313 synchronized(readLock) {
314 installShutdownHook();
315 try {
316 restoreEcho = echo(false);
317 } catch (IOException x) {
318 throw new IOError(x);
319 }
320 IOError ioe = null;
321 try {
322 if (!fmt.isEmpty())
323 pw.format(fmt, args);
324 passwd = readline(true);
325 } catch (IOException x) {
326 ioe = new IOError(x);
327 } finally {
328 try {
329 if (restoreEcho)
330 restoreEcho = echo(true);
331 } catch (IOException x) {
332 if (ioe == null)
333 ioe = new IOError(x);
334 else
335 ioe.addSuppressed(x);
336 }
337 if (ioe != null)
338 throw ioe;
339 }
340 pw.println();
341 }
342 }
|