8298971: Move Console implementation into jdk internal package

Reviewed-by: jpai
This commit is contained in:
Naoto Sato 2022-12-21 18:09:21 +00:00
parent 10d62fa218
commit 7e59a0ecb6
4 changed files with 99 additions and 74 deletions
src/java.base
share/classes
java/io
jdk/internal/io
unix/native/libjava

@ -31,6 +31,7 @@ import java.util.*;
import java.nio.charset.Charset;
import jdk.internal.access.JavaIOAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.io.JdkConsoleImpl;
import jdk.internal.io.JdkConsoleProvider;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;
@ -93,7 +94,7 @@ import sun.security.action.GetPropertyAction;
* @author Xueming Shen
* @since 1.6
*/
public sealed class Console implements Flushable permits ConsoleImpl, ProxyingConsole {
public sealed class Console implements Flushable permits ProxyingConsole {
/**
* Package private no-arg constructor.
*/
@ -380,6 +381,8 @@ public sealed class Console implements Flushable permits ConsoleImpl, ProxyingCo
@SuppressWarnings("removal")
private static Console instantiateConsole(boolean istty) {
Console c;
try {
// Try loading providers
PrivilegedAction<Console> pa = () -> {
@ -392,13 +395,19 @@ public sealed class Console implements Flushable permits ConsoleImpl, ProxyingCo
.filter(Objects::nonNull)
.findAny()
.map(jc -> (Console) new ProxyingConsole(jc))
.orElse(istty ? new ConsoleImpl() : null);
.orElse(null);
};
return AccessController.doPrivileged(pa);
c = AccessController.doPrivileged(pa);
} catch (ServiceConfigurationError ignore) {
// default to built-in Console
return istty ? new ConsoleImpl() : null;
c = null;
}
// If not found, default to built-in Console
if (istty && c == null) {
c = new ProxyingConsole(new JdkConsoleImpl(CHARSET));
}
return c;
}
private static final Console cons;

@ -23,55 +23,50 @@
* questions.
*/
package java.io;
package jdk.internal.io;
import java.util.*;
import java.io.IOError;
import java.io.IOException;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Objects;
import jdk.internal.access.SharedSecrets;
import sun.nio.cs.StreamDecoder;
import sun.nio.cs.StreamEncoder;
/**
* Console implementation based on the platform's TTY.
* JdkConsole implementation based on the platform's TTY.
*/
final class ConsoleImpl extends Console {
/**
* {@inheritDoc}
*/
public final class JdkConsoleImpl implements JdkConsole {
@Override
public PrintWriter writer() {
return pw;
}
/**
* {@inheritDoc}
*/
@Override
public Reader reader() {
return reader;
}
/**
* {@inheritDoc}
*/
@Override
public Console format(String fmt, Object ...args) {
public JdkConsole format(String fmt, Object ... args) {
formatter.format(fmt, args).flush();
return this;
}
/**
* {@inheritDoc}
*/
@Override
public Console printf(String format, Object ... args) {
public JdkConsole printf(String format, Object ... args) {
return format(format, args);
}
/**
* {@inheritDoc}
*/
@Override
public String readLine(String fmt, Object ... args) {
String line = null;
@ -91,17 +86,11 @@ final class ConsoleImpl extends Console {
return line;
}
/**
* {@inheritDoc}
*/
@Override
public String readLine() {
return readLine("");
}
/**
* {@inheritDoc}
*/
@Override
public char[] readPassword(String fmt, Object ... args) {
char[] passwd = null;
@ -164,31 +153,22 @@ final class ConsoleImpl extends Console {
shutdownHookInstalled = true;
}
/**
* {@inheritDoc}
*/
@Override
public char[] readPassword() {
return readPassword("");
}
/**
* {@inheritDoc}
*/
@Override
public void flush() {
pw.flush();
}
/**
* {@inheritDoc}
*/
@Override
public Charset charset() {
assert CHARSET != null : "charset() should not return null";
return CHARSET;
return charset;
}
private final Charset charset;
private final Object readLock;
private final Object writeLock;
private final Reader reader;
@ -348,19 +328,24 @@ final class ConsoleImpl extends Console {
}
}
ConsoleImpl() {
public JdkConsoleImpl(Charset charset) {
Objects.requireNonNull(charset);
this.charset = charset;
readLock = new Object();
writeLock = new Object();
out = StreamEncoder.forOutputStreamWriter(
new FileOutputStream(FileDescriptor.out),
writeLock,
CHARSET);
pw = new PrintWriter(out, true) { public void close() {} };
charset);
pw = new PrintWriter(out, true) {
public void close() {
}
};
formatter = new Formatter(out);
reader = new LineReader(StreamDecoder.forInputStreamReader(
new FileInputStream(FileDescriptor.in),
readLock,
CHARSET));
charset));
rcb = new char[1024];
}
}

@ -27,11 +27,9 @@
#include "jni_util.h"
#include "jvm.h"
#include "java_io_Console.h"
#include "java_io_ConsoleImpl.h"
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
JNIEXPORT jboolean JNICALL
Java_java_io_Console_istty(JNIEnv *env, jclass cls)
@ -44,27 +42,3 @@ Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
{
return NULL;
}
JNIEXPORT jboolean JNICALL
Java_java_io_ConsoleImpl_echo(JNIEnv *env,
jclass cls,
jboolean on)
{
struct termios tio;
jboolean old;
int tty = fileno(stdin);
if (tcgetattr(tty, &tio) == -1) {
JNU_ThrowIOExceptionWithLastError(env, "tcgetattr failed");
return !on;
}
old = (tio.c_lflag & ECHO) != 0;
if (on) {
tio.c_lflag |= ECHO;
} else {
tio.c_lflag &= ~ECHO;
}
if (tcsetattr(tty, TCSANOW, &tio) == -1) {
JNU_ThrowIOExceptionWithLastError(env, "tcsetattr failed");
}
return old;
}

@ -0,0 +1,57 @@
/*
* Copyright (c) 2022, 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.
*/
#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
#include "jdk_internal_io_JdkConsoleImpl.h"
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
JNIEXPORT jboolean JNICALL
Java_jdk_internal_io_JdkConsoleImpl_echo(JNIEnv *env,
jclass cls,
jboolean on)
{
struct termios tio;
jboolean old;
int tty = fileno(stdin);
if (tcgetattr(tty, &tio) == -1) {
JNU_ThrowIOExceptionWithLastError(env, "tcgetattr failed");
return !on;
}
old = (tio.c_lflag & ECHO) != 0;
if (on) {
tio.c_lflag |= ECHO;
} else {
tio.c_lflag &= ~ECHO;
}
if (tcsetattr(tty, TCSANOW, &tio) == -1) {
JNU_ThrowIOExceptionWithLastError(env, "tcsetattr failed");
}
return old;
}