8305457: Implement java.io.IO

Reviewed-by: naoto, smarks, jpai, jlahoda
This commit is contained in:
Pavel Rappo 2024-05-24 13:37:14 +00:00
parent 6a35311468
commit c099f14f07
15 changed files with 695 additions and 5 deletions

View file

@ -33,6 +33,7 @@ import jdk.internal.access.JavaIOAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.io.JdkConsoleImpl;
import jdk.internal.io.JdkConsoleProvider;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;
@ -150,6 +151,69 @@ public sealed class Console implements Flushable permits ProxyingConsole {
throw newUnsupportedOperationException();
}
/**
* Writes a string representation of the specified object to this console's
* output stream, terminates the line using {@link System#lineSeparator()}
* and then flushes the console.
*
* <p> The string representation of the specified object is obtained as if
* by calling {@link String#valueOf(Object)}.
*
* @param obj
* An object whose string representation is to be written,
* may be {@code null}.
*
* @return This console
*
* @since 23
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public Console println(Object obj) {
throw newUnsupportedOperationException();
}
/**
* Writes a string representation of the specified object to this console's
* output stream and then flushes the console.
*
* <p> The string representation of the specified object is obtained as if
* by calling {@link String#valueOf(Object)}.
*
* @param obj
* An object whose string representation is to be written,
* may be {@code null}.
*
* @return This console
*
* @since 23
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public Console print(Object obj) {
throw newUnsupportedOperationException();
}
/**
* Writes a prompt as if by calling {@code print}, then reads a single line
* of text from this console.
*
* @param prompt
* A prompt string, may be {@code null}.
*
* @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 without having read
* any characters.
*
* @since 23
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public String readln(String prompt) {
throw newUnsupportedOperationException();
}
/**
* Writes a formatted string to this console's output stream using
* the specified format string and arguments with the

View file

@ -0,0 +1,110 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import jdk.internal.javac.PreviewFeature;
/**
* A collection of static convenience methods that provide access to
* {@linkplain System#console() system console} for implicitly declared classes.
*
* <p> Each of this class' methods throws {@link IOError} if the system console
* is {@code null}; otherwise, the effect is as if a similarly-named method
* had been called on that console.
*
* <p> Input and output from methods in this class use the character set of
* the system console as specified by {@link Console#charset}.
*
* @since 23
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public final class IO {
private IO() {
throw new Error("no instances");
}
/**
* Writes a string representation of the specified object to the system
* console, terminates the line and then flushes that console.
*
* <p> The effect is as if {@link Console#println(Object) println(obj)}
* had been called on {@code System.console()}.
*
* @param obj the object to print, may be {@code null}
*
* @throws IOError if {@code System.console()} returns {@code null},
* or if an I/O error occurs
*/
public static void println(Object obj) {
con().println(obj);
}
/**
* Writes a string representation of the specified object to the system
* console and then flushes that console.
*
* <p> The effect is as if {@link Console#print(Object) print(obj)}
* had been called on {@code System.console()}.
*
* @param obj the object to print, may be {@code null}
*
* @throws IOError if {@code System.console()} returns {@code null},
* or if an I/O error occurs
*/
public static void print(Object obj) {
con().print(obj);
}
/**
* Writes a prompt as if by calling {@code print}, then reads a single line
* of text from the system console.
*
* <p> The effect is as if {@link Console#readln(String) readln(prompt)}
* had been called on {@code System.console()}.
*
* @param prompt the prompt string, may be {@code null}
*
* @return a string containing the line read from the system console, not
* including any line-termination characters. Returns {@code null} if an
* end of stream has been reached without having read any characters.
*
* @throws IOError if {@code System.console()} returns {@code null},
* or if an I/O error occurs
*/
public static String readln(String prompt) {
return con().readln(prompt);
}
private static Console con() {
var con = System.console();
if (con != null) {
return con;
} else {
throw new IOError(null);
}
}
}

View file

@ -81,6 +81,42 @@ final class ProxyingConsole extends Console {
return reader;
}
/**
* {@inheritDoc}
*/
@Override
public Console println(Object obj) {
synchronized (writeLock) {
delegate.println(obj);
}
return this;
}
/**
* {@inheritDoc}
*/
@Override
public Console print(Object obj) {
synchronized (writeLock) {
delegate.print(obj);
}
return this;
}
/**
* {@inheritDoc}
*
* @throws IOError {@inheritDoc}
*/
@Override
public String readln(String prompt) {
synchronized (writeLock) {
synchronized (readLock) {
return delegate.readln(prompt);
}
}
}
/**
* {@inheritDoc}
*/