8308591: JLine as the default Console provider

Reviewed-by: alanb
This commit is contained in:
Naoto Sato 2023-07-20 16:11:13 +00:00
parent b772e67e29
commit bae2247938
5 changed files with 62 additions and 15 deletions

View file

@ -344,16 +344,33 @@ public sealed class Console implements Flushable permits ProxyingConsole {
throw newUnsupportedOperationException();
}
/**
* {@return {@code true} if the {@code Console} instance is a terminal}
* <p>
* This method returns {@code true} if the console device, associated with the current
* Java virtual machine, is a terminal, typically an interactive command line
* connected to a keyboard and display.
*
* @implNote The default implementation returns the value equivalent to calling
* {@code isatty(stdin/stdout)} on POSIX platforms, or whether standard in/out file
* descriptors are character devices or not on Windows.
*
* @since 22
*/
public boolean isTerminal() {
return istty;
}
private static UnsupportedOperationException newUnsupportedOperationException() {
return new UnsupportedOperationException(
"Console class itself does not provide implementation");
}
private static native String encoding();
private static final boolean istty = istty();
static final Charset CHARSET;
static {
Charset cs = null;
boolean istty = istty();
if (istty) {
String csname = encoding();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, 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
@ -34,17 +34,13 @@ import jdk.internal.io.JdkConsole;
*/
final class ProxyingConsole extends Console {
private final JdkConsole delegate;
private final Object readLock;
private final Object writeLock;
private final Reader reader;
private final PrintWriter printWriter;
private final Object readLock = new Object();
private final Object writeLock = new Object();
private volatile Reader reader;
private volatile PrintWriter printWriter;
ProxyingConsole(JdkConsole delegate) {
this.delegate = delegate;
readLock = new Object();
writeLock = new Object();
reader = new WrappingReader(delegate.reader(), readLock);
printWriter = new WrappingWriter(delegate.writer(), writeLock);
}
/**
@ -52,6 +48,16 @@ final class ProxyingConsole extends Console {
*/
@Override
public PrintWriter writer() {
PrintWriter printWriter = this.printWriter;
if (printWriter == null) {
synchronized (this) {
printWriter = this.printWriter;
if (printWriter == null) {
printWriter = new WrappingWriter(delegate.writer(), writeLock);
this.printWriter = printWriter;
}
}
}
return printWriter;
}
@ -60,6 +66,16 @@ final class ProxyingConsole extends Console {
*/
@Override
public Reader reader() {
Reader reader = this.reader;
if (reader == null) {
synchronized (this) {
reader = this.reader;
if (reader == null) {
reader = new WrappingReader(delegate.reader(), readLock);
this.reader = reader;
}
}
}
return reader;
}