mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
8298971: Move Console implementation into jdk internal package
Reviewed-by: jpai
This commit is contained in:
parent
10d62fa218
commit
7e59a0ecb6
4 changed files with 99 additions and 74 deletions
|
@ -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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue