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}
*/

View File

@ -38,6 +38,9 @@ import java.util.Locale;
public interface JdkConsole {
PrintWriter writer();
Reader reader();
JdkConsole println(Object obj);
JdkConsole print(Object obj);
String readln(String prompt);
JdkConsole format(Locale locale, String format, Object ... args);
String readLine(Locale locale, String format, Object ... args);
String readLine();

View File

@ -57,6 +57,39 @@ public final class JdkConsoleImpl implements JdkConsole {
return reader;
}
@Override
public JdkConsole println(Object obj) {
pw.println(obj);
// automatic flushing covers println
return this;
}
@Override
public JdkConsole print(Object obj) {
pw.print(obj);
pw.flush(); // automatic flushing does not cover print
return this;
}
@Override
public String readln(String prompt) {
String line = null;
synchronized (writeLock) {
synchronized(readLock) {
pw.print(prompt);
pw.flush(); // automatic flushing does not cover print
try {
char[] ca = readline(false);
if (ca != null)
line = new String(ca);
} catch (IOException x) {
throw new IOError(x);
}
}
}
return line;
}
@Override
public JdkConsole format(Locale locale, String format, Object ... args) {
formatter.format(locale, format, args).flush();

View File

@ -84,6 +84,30 @@ public class JdkConsoleProviderImpl implements JdkConsoleProvider {
return terminal.reader();
}
@Override
public JdkConsole println(Object obj) {
writer().println(obj);
writer().flush();
return this;
}
@Override
public JdkConsole print(Object obj) {
writer().print(obj);
writer().flush();
return this;
}
@Override
public String readln(String prompt) {
try {
initJLineIfNeeded();
return jline.readLine(prompt == null ? "null" : prompt.replace("%", "%%"));
} catch (EndOfFileException eofe) {
return null;
}
}
@Override
public JdkConsole format(Locale locale, String format, Object ... args) {
writer().format(locale, format, args).flush();

View File

@ -191,6 +191,46 @@ public class ConsoleImpl {
} return reader;
}
/**
* {@inheritDoc}
*/
@Override
public JdkConsole println(Object obj) {
writer().println(obj);
writer().flush();
return this;
}
/**
* {@inheritDoc}
*/
@Override
public JdkConsole print(Object obj) {
writer().print(obj);
writer().flush();
return this;
}
/**
* {@inheritDoc}
*
* @throws IOError {@inheritDoc}
*/
@Override
public String readln(String prompt) {
try {
return sendAndReceive(() -> {
remoteInput.write(Task.READ_LINE.ordinal());
char[] chars = (prompt == null ? "null" : prompt).toCharArray();
sendChars(chars, 0, chars.length);
char[] line = readChars();
return new String(line);
});
} catch (IOException ex) {
throw new IOError(ex);
}
}
/**
* {@inheritDoc}
*/

177
test/jdk/java/io/IO/IO.java Normal file
View File

@ -0,0 +1,177 @@
/*
* Copyright (c) 2023, 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.
*
* 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.
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Stream;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jtreg.SkippedException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @bug 8305457
* @summary java.io.IO tests
* @library /test/lib
* @run junit IO
*/
public class IO {
@Nested
@EnabledOnOs({OS.LINUX, OS.MAC})
public class OSSpecificTests {
private static Path expect;
@BeforeAll
public static void prepareTTY() {
expect = Paths.get("/usr/bin/expect"); // os-specific path
if (!Files.exists(expect) || !Files.isExecutable(expect)) {
throw new SkippedException("'" + expect + "' not found");
}
}
/*
* Unlike printTest, which tests a _default_ console that is normally
* jdk.internal.org.jline.JdkConsoleProviderImpl, this test tests
* jdk.internal.io.JdkConsoleImpl. Those console implementations operate
* in different conditions and, thus, are tested separately.
*
* To test jdk.internal.io.JdkConsoleImpl one needs to ensure that both
* conditions are met:
*
* - a non-existent console provider is requested
* - isatty is true
*
* To achieve isatty, the test currently uses the EXPECT(1) Unix command,
* which does not work for Windows. Later, a library like pty4j or JPty
* might be used instead of EXPECT, to cover both Unix and Windows.
*/
@ParameterizedTest
@ValueSource(strings = {"println", "print"})
public void outputTestInteractive(String mode) throws Exception {
var testSrc = System.getProperty("test.src", ".");
OutputAnalyzer output = ProcessTools.executeProcess(
expect.toString(),
Path.of(testSrc, "output.exp").toAbsolutePath().toString(),
System.getProperty("test.jdk") + "/bin/java",
"--enable-preview",
"-Djdk.console=gibberish",
Path.of(testSrc, "Output.java").toAbsolutePath().toString(),
mode);
assertEquals(0, output.getExitValue());
assertTrue(output.getStderr().isEmpty());
output.reportDiagnosticSummary();
String out = output.getStdout();
// The first half of the output is produced by Console, the second
// half is produced by IO: those halves must match.
// Executing Console and IO in the same VM (as opposed to
// consecutive VM runs, which are cleaner) to be able to compare string
// representation of objects.
assertFalse(out.isBlank());
assertEquals(out.substring(0, out.length() / 2),
out.substring(out.length() / 2));
}
/*
* This tests simulates terminal interaction (isatty), to check that the
* prompt is output.
*
* To simulate a terminal, the test currently uses the EXPECT(1) Unix
* command, which does not work for Windows. Later, a library like pty4j
* or JPty might be used instead of EXPECT, to cover both Unix and Windows.
*/
@ParameterizedTest
@MethodSource("args")
public void inputTestInteractive(String console, String prompt) throws Exception {
var testSrc = System.getProperty("test.src", ".");
var command = new ArrayList<String>();
command.add(expect.toString());
command.add(Path.of(testSrc, "input.exp").toAbsolutePath().toString());
command.add(System.getProperty("test.jdk") + "/bin/java");
command.add("--enable-preview");
if (console != null)
command.add("-Djdk.console=" + console);
command.add(Path.of(testSrc, "Input.java").toAbsolutePath().toString());
command.add(prompt == null ? "0" : "1");
command.add(String.valueOf(prompt));
OutputAnalyzer output = ProcessTools.executeProcess(command.toArray(new String[]{}));
output.reportDiagnosticSummary();
assertEquals(0, output.getExitValue());
}
public static Stream<Arguments> args() {
// cross product: consoles x prompts
return Stream.of(null, "gibberish").flatMap(console -> Stream.of(null, "?", "%s")
.map(prompt -> new String[]{console, prompt}).map(Arguments::of));
}
}
@ParameterizedTest
@ValueSource(strings = {"println", "print"})
public void printTest(String mode) throws Exception {
var file = Path.of(System.getProperty("test.src", "."), "Output.java")
.toAbsolutePath().toString();
var pb = ProcessTools.createTestJavaProcessBuilder("--enable-preview", file, mode);
OutputAnalyzer output = ProcessTools.executeProcess(pb);
assertEquals(0, output.getExitValue());
assertTrue(output.getStderr().isEmpty());
output.reportDiagnosticSummary();
String out = output.getStdout();
// The first half of the output is produced by Console, the second
// half is produced by IO: those halves must match.
// Executing Console and IO in the same VM (as opposed to
// consecutive VM runs, which are cleaner) to be able to compare string
// representation of objects.
assertFalse(out.isBlank());
assertEquals(out.substring(0, out.length() / 2),
out.substring(out.length() / 2));
}
@ParameterizedTest
@ValueSource(strings = {"println", "print", "input"})
public void nullConsole(String method) throws Exception {
var file = Path.of(System.getProperty("test.src", "."), "Methods.java")
.toAbsolutePath().toString();
var pb = ProcessTools.createTestJavaProcessBuilder("-Djdk.console=gibberish",
"--enable-preview", file, method);
OutputAnalyzer output = ProcessTools.executeProcess(pb);
output.reportDiagnosticSummary();
assertEquals(1, output.getExitValue());
output.shouldContain("Exception in thread \"main\" java.io.IOError");
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.
*
* 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.
*/
import java.io.IOException;
import static java.io.IO.readln;
public class Input {
public static void main(String[] args) throws IOException {
if (args[0].equals("0"))
System.out.print(readln(null));
else
System.out.print(readln(args[1]));
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.
*
* 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.
*/
import static java.io.IO.*;
public class Methods {
public static void main(String[] args) {
switch (args[0]) {
case "println" -> println("hello");
case "print" -> print("hello");
case "input" -> readln("hello");
default -> throw new IllegalArgumentException(args[0]);
}
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.
*
* 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.
*/
import static java.io.IO.*;
import java.util.function.Consumer;
public class Output {
private static final Object[] OBJECTS = {
null,
false,
(byte) 1,
(short) 2,
'a',
3,
4L,
5f,
6d,
new Object(),
"%s", // to test that print(ln) does not interpret its argument as a format string
new char[]{'a'},
};
public static void main(String[] args) {
switch (args[0]) {
case "print" -> {
printObjects(obj -> System.console().format("%s", obj).flush());
printObjects(obj -> print(obj));
}
case "println" -> {
printObjects(obj -> System.console().format("%s%n", obj).flush());
printObjects(obj -> println(obj));
}
default -> throw new IllegalArgumentException();
}
}
private static void printObjects(Consumer<Object> printer) {
for (var obj : OBJECTS) {
printer.accept(obj);
}
}
}

View File

@ -0,0 +1,36 @@
#
# 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.
#
# 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.
#
set prompt [lindex $argv $argc-1]
set stty_init "rows 24 cols 80"
spawn {*}$argv
expect {
-exact "$prompt" {
send "hello\r"
}
timeout {
puts "timeout"; exit 1
}
}
expect eof

View File

@ -0,0 +1,32 @@
#
# 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.
#
# 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.
#
################################################################################
# This script does not expect/verify anything and is only used to simulate tty #
################################################################################
# Use `noecho` below, otherwise, expect will output the expanded "spawn ..."
# command, which will interfere with asserting output from the java test
spawn -noecho {*}$argv
expect eof

View File

@ -118,7 +118,6 @@ compiler.warn.illegal.char.for.encoding
compiler.warn.incubating.modules # requires adjusted classfile
compiler.warn.invalid.archive.file
compiler.warn.invalid.utf8.in.classfile # bad class file
compiler.warn.is.preview # difficult to produce reliably despite future changes to java.base
compiler.warn.is.preview.reflective # difficult to produce reliably despite future changes to java.base
compiler.warn.output.file.clash # this warning is not generated on Linux
compiler.warn.override.bridge

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 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
@ -21,9 +21,10 @@
* questions.
*/
// key: compiler.misc.feature.implicit.classes
// key: compiler.warn.preview.feature.use.plural
// options: -source ${jdk.version} --enable-preview -Xlint:preview
// key: compiler.misc.feature.implicit.classes
// key: compiler.warn.preview.feature.use.plural
// key: compiler.warn.is.preview
// options: -source ${jdk.version} --enable-preview -Xlint:preview
public static void main(String... args) {
}