8330276: Console methods with explicit Locale

Reviewed-by: joehw, rriggs, jlahoda
This commit is contained in:
Naoto Sato 2024-05-15 19:28:24 +00:00
parent 8a4315f833
commit 7cff04fc8a
8 changed files with 417 additions and 104 deletions

View file

@ -62,6 +62,11 @@ import sun.security.action.GetPropertyAction;
* on the objects returned by {@link #reader()} and {@link #writer()} may
* block in multithreaded scenarios.
* <p>
* Operations that format strings are locale sensitive, using either the
* specified {@code Locale}, or the
* {@link Locale##default_locale default format Locale} to produce localized
* formatted strings.
* <p>
* Invoking {@code close()} on the objects returned by the {@link #reader()}
* and the {@link #writer()} will not close the underlying stream of those
* objects.
@ -147,11 +152,12 @@ public sealed class Console implements Flushable permits ProxyingConsole {
/**
* Writes a formatted string to this console's output stream using
* the specified format string and arguments.
* the specified format string and arguments with the
* {@link Locale##default_locale default format locale}.
*
* @param fmt
* A format string as described in <a
* href="../util/Formatter.html#syntax">Format string syntax</a>.
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
@ -160,39 +166,75 @@ public sealed class Console implements Flushable permits ProxyingConsole {
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the <a
* href="../util/Formatter.html#syntax">conversion</a>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section
* formatting errors, see the {@link
* Formatter##detail Details} section
* of the formatter class specification.
*
* @return This console
*/
public Console format(String fmt, Object ...args) {
public Console format(String format, Object ... args) {
throw newUnsupportedOperationException();
}
/**
* Writes a formatted string to this console's output stream using
* the specified format string and arguments with the specified
* {@code locale}.
*
* @param locale The {@linkplain Locale locale} to apply during
* formatting. If {@code locale} is {@code null} then no localization
* is applied.
*
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the {@link
* Formatter##detail Details} section
* of the formatter class specification.
*
* @return This console
* @since 23
*/
public Console format(Locale locale, String format, Object ... args) {
throw newUnsupportedOperationException();
}
/**
* A convenience method to write a formatted string to this console's
* output stream using the specified format string and arguments.
* output stream using the specified format string and arguments with
* the {@link Locale##default_locale default format locale}.
*
* <p> An invocation of this method of the form
* {@code con.printf(format, args)} behaves in exactly the same way
* as the invocation of
* {@snippet lang=java :
* con.format(format, args)
* }
* @implSpec This is the same as calling {@code format(format, args)}.
*
* @param format
* A format string as described in <a
* href="../util/Formatter.html#syntax">Format string syntax</a>.
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
@ -201,17 +243,17 @@ public sealed class Console implements Flushable permits ProxyingConsole {
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the <a
* href="../util/Formatter.html#syntax">conversion</a>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section of the
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @return This console
@ -221,28 +263,76 @@ public sealed class Console implements Flushable permits ProxyingConsole {
}
/**
* Provides a formatted prompt, then reads a single line of text from the
* console.
* A convenience method to write a formatted string to this console's
* output stream using the specified format string and arguments with
* the specified {@code locale}.
*
* @param fmt
* A format string as described in <a
* href="../util/Formatter.html#syntax">Format string syntax</a>.
* @implSpec This is the same as calling
* {@code format(locale, format, args)}.
*
* @param locale The {@linkplain Locale locale} to apply during
* formatting. If {@code locale} is {@code null} then no localization
* is applied.
*
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The maximum number of arguments is
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section
* of the formatter class specification.
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @return This console
* @since 23
*/
public Console printf(Locale locale, String format, Object ... args) {
throw newUnsupportedOperationException();
}
/**
* Provides a formatted prompt using the
* {@link Locale##default_locale default format locale}, then reads a
* single line of text from the console.
*
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @throws IOError
* If an I/O error occurs.
@ -251,7 +341,51 @@ public sealed class Console implements Flushable permits ProxyingConsole {
* including any line-termination characters, or {@code null}
* if an end of stream has been reached.
*/
public String readLine(String fmt, Object ... args) {
public String readLine(String format, Object ... args) {
throw newUnsupportedOperationException();
}
/**
* Provides a formatted prompt using the specified {@code locale}, then
* reads a single line of text from the console.
*
* @param locale The {@linkplain Locale locale} to apply during
* formatting. If {@code locale} is {@code null} then no localization
* is applied.
*
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @throws IOError
* If an I/O error occurs.
*
* @return A string containing the line read from the console, not
* including any line-termination characters, or {@code null}
* if an end of stream has been reached.
* @since 23
*/
public String readLine(Locale locale, String format, Object ... args) {
throw newUnsupportedOperationException();
}
@ -270,29 +404,34 @@ public sealed class Console implements Flushable permits ProxyingConsole {
}
/**
* Provides a formatted prompt, then reads a password or passphrase from
* the console with echoing disabled.
* Provides a formatted prompt using the
* {@link Locale##default_locale default format locale}, then reads a
* password or passphrase from the console with echoing disabled.
*
* @param fmt
* A format string as described in <a
* href="../util/Formatter.html#syntax">Format string syntax</a>
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}
* for the prompt text.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The maximum number of arguments is
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a>
* section of the formatter class specification.
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @throws IOError
* If an I/O error occurs.
@ -301,7 +440,52 @@ public sealed class Console implements Flushable permits ProxyingConsole {
* from the console, not including any line-termination characters,
* or {@code null} if an end of stream has been reached.
*/
public char[] readPassword(String fmt, Object ... args) {
public char[] readPassword(String format, Object ... args) {
throw newUnsupportedOperationException();
}
/**
* Provides a formatted prompt using the specified {@code locale}, then
* reads a password or passphrase from the console with echoing disabled.
*
* @param locale The {@linkplain Locale locale} to apply during
* formatting. If {@code locale} is {@code null} then no localization
* is applied.
*
* @param format
* A format string as described in {@link
* Formatter##syntax Format string syntax}
* for the prompt text.
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behavior on a
* {@code null} argument depends on the {@link
* Formatter##syntax conversion}.
*
* @throws IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the {@link
* Formatter##detail Details} section of the
* formatter class specification.
*
* @throws IOError
* If an I/O error occurs.
*
* @return A character array containing the password or passphrase read
* from the console, not including any line-termination characters,
* or {@code null} if an end of stream has been reached.
* @since 23
*/
public char[] readPassword(Locale locale, String format, Object ... args) {
throw newUnsupportedOperationException();
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,8 @@
package java.io;
import java.nio.charset.Charset;
import java.util.Locale;
import jdk.internal.io.JdkConsole;
/**
@ -83,9 +85,17 @@ final class ProxyingConsole extends Console {
* {@inheritDoc}
*/
@Override
public Console format(String fmt, Object ... args) {
public Console format(String format, Object ... args) {
return format(Locale.getDefault(Locale.Category.FORMAT), format, args);
}
/**
* {@inheritDoc}
*/
@Override
public Console format(Locale locale, String format, Object ... args) {
synchronized (writeLock) {
delegate.format(fmt, args);
delegate.format(locale, format, args);
}
return this;
}
@ -95,8 +105,16 @@ final class ProxyingConsole extends Console {
*/
@Override
public Console printf(String format, Object ... args) {
return printf(Locale.getDefault(Locale.Category.FORMAT), format, args);
}
/**
* {@inheritDoc}
*/
@Override
public Console printf(Locale locale, String format, Object ... args) {
synchronized (writeLock) {
delegate.printf(format, args);
delegate.format(locale, format, args);
}
return this;
}
@ -105,10 +123,18 @@ final class ProxyingConsole extends Console {
* {@inheritDoc}
*/
@Override
public String readLine(String fmt, Object ... args) {
public String readLine(String format, Object ... args) {
return readLine(Locale.getDefault(Locale.Category.FORMAT), format, args);
}
/**
* {@inheritDoc}
*/
@Override
public String readLine(Locale locale, String format, Object ... args) {
synchronized (writeLock) {
synchronized (readLock) {
return delegate.readLine(fmt, args);
return delegate.readLine(locale, format, args);
}
}
}
@ -127,10 +153,18 @@ final class ProxyingConsole extends Console {
* {@inheritDoc}
*/
@Override
public char[] readPassword(String fmt, Object ... args) {
public char[] readPassword(String format, Object ... args) {
return readPassword(Locale.getDefault(Locale.Category.FORMAT), format, args);
}
/**
* {@inheritDoc}
*/
@Override
public char[] readPassword(Locale locale, String format, Object ... args) {
synchronized (writeLock) {
synchronized (readLock) {
return delegate.readPassword(fmt, args);
return delegate.readPassword(locale, format, args);
}
}
}