Skip to content

Commit

Permalink
Cleanup - moving to jdk.internal.le
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Jan 8, 2025
1 parent 11c0e61 commit 951ec6a
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/io/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ private static Console instantiateConsole() {
* If no providers are available, or instantiation failed, java.base built-in
* Console implementation is used.
*/
var consModName = "java.base";//System.getProperty("jdk.console",
// JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);

for (var jcp : ServiceLoader.load(ModuleLayer.boot(), JdkConsoleProvider.class)) {
if (consModName.equals(jcp.getClass().getModule().getName())) {
Expand Down
175 changes: 159 additions & 16 deletions src/java.base/share/classes/jdk/internal/io/JdkConsoleImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
Expand Down Expand Up @@ -34,6 +34,7 @@
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -168,15 +169,14 @@ public char[] readPassword(Locale locale, String format, Object ... args) {
ioe.addSuppressed(x);
}
if (ioe != null) {
//TODO:
// Arrays.fill(passwd, ' ');
// try {
// if (reader instanceof LineReader lr) {
// lr.zeroOut();
// }
// } catch (IOException _) {
// // ignore
// }
Arrays.fill(passwd, ' ');
try {
if (reader instanceof LineReader lr) {
lr.zeroOut();
}
} catch (IOException _) {
// ignore
}
throw ioe;
}
}
Expand Down Expand Up @@ -235,14 +235,42 @@ public Charset charset() {
private final Object restoreEchoLock;
private final Reader reader;
private final Writer out;
private final NativeConsoleReader nativeConsoleReader;
private final PrintWriter pw;
private final Formatter formatter;
private char[] rcb;
private boolean restoreEcho;
private boolean shutdownHookInstalled;

private char[] readline(boolean zeroOut) throws IOException {
return nativeConsoleReader.readline(reader, out, zeroOut);
int len = reader.read(rcb, 0, rcb.length);
if (len < 0)
return null; //EOL
if (rcb[len-1] == '\r')
len--; //remove CR at end;
else if (rcb[len-1] == '\n') {
len--; //remove LF at end;
if (len > 0 && rcb[len-1] == '\r')
len--; //remove the CR, if there is one
}
char[] b = new char[len];
if (len > 0) {
System.arraycopy(rcb, 0, b, 0, len);
if (zeroOut) {
Arrays.fill(rcb, 0, len, ' ');
if (reader instanceof LineReader lr) {
lr.zeroOut();
}
}
}
return b;
}

private char[] grow() {
assert Thread.holdsLock(readLock);
char[] t = new char[rcb.length * 2];
System.arraycopy(rcb, 0, t, 0, rcb.length);
rcb = t;
return rcb;
}

/*
Expand All @@ -254,6 +282,122 @@ private char[] readline(boolean zeroOut) throws IOException {
*/
private static native boolean echo(boolean on) throws IOException;

class LineReader extends Reader {
private final Reader in;
private final char[] cb;
private int nChars, nextChar;
boolean leftoverLF;
LineReader(Reader in) {
this.in = in;
cb = new char[1024];
nextChar = nChars = 0;
leftoverLF = false;
}
public void zeroOut() throws IOException {
if (in instanceof StreamDecoder sd) {
sd.fillZeroToPosition();
}
}
public void close () {}
public boolean ready() throws IOException {
//in.ready synchronizes on readLock already
return in.ready();
}

public int read(char[] cbuf, int offset, int length)
throws IOException
{
int off = offset;
int end = offset + length;
if (offset < 0 || offset > cbuf.length || length < 0 ||
end < 0 || end > cbuf.length) {
throw new IndexOutOfBoundsException();
}
synchronized(readLock) {
boolean eof = false;
char c;
for (;;) {
if (nextChar >= nChars) { //fill
int n;
do {
n = in.read(cb, 0, cb.length);
} while (n == 0);
if (n > 0) {
nChars = n;
nextChar = 0;
if (n < cb.length &&
cb[n-1] != '\n' && cb[n-1] != '\r') {
/*
* we're in canonical mode so each "fill" should
* come back with an eol. if there is no lf or nl at
* the end of returned bytes we reached an eof.
*/
eof = true;
}
} else { /*EOF*/
if (off - offset == 0)
return -1;
return off - offset;
}
}
if (leftoverLF && cbuf == rcb && cb[nextChar] == '\n') {
/*
* if invoked by our readline, skip the leftover, otherwise
* return the LF.
*/
nextChar++;
}
leftoverLF = false;
while (nextChar < nChars) {
c = cbuf[off++] = cb[nextChar];
cb[nextChar++] = 0;
if (c == '\n') {
return off - offset;
} else if (c == '\r') {
if (off == end) {
/* no space left even the next is LF, so return
* whatever we have if the invoker is not our
* readLine()
*/
if (cbuf == rcb) {
cbuf = grow();
} else {
leftoverLF = true;
return off - offset;
}
}
if (nextChar == nChars && in.ready()) {
/*
* we have a CR and we reached the end of
* the read in buffer, fill to make sure we
* don't miss a LF, if there is one, it's possible
* that it got cut off during last round reading
* simply because the read in buffer was full.
*/
nChars = in.read(cb, 0, cb.length);
nextChar = 0;
}
if (nextChar < nChars && cb[nextChar] == '\n') {
cbuf[off++] = '\n';
nextChar++;
}
return off - offset;
} else if (off == end) {
if (cbuf == rcb) {
cbuf = grow();
end = cbuf.length;
} else {
return off - offset;
}
}
}
if (eof)
return off - offset;
}
}
}
}

public JdkConsoleImpl(Charset charset) {
Objects.requireNonNull(charset);
this.charset = charset;
Expand All @@ -269,11 +413,10 @@ public void close() {
}
};
formatter = new Formatter(out);
reader = /*new LineReader(*/StreamDecoder.forInputStreamReader(
reader = new LineReader(StreamDecoder.forInputStreamReader(
new FileInputStream(FileDescriptor.in),
readLock,
charset);//);
nativeConsoleReader = NativeConsoleReaderImpl.create(readLock);
charset));
rcb = new char[1024];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface JdkConsoleProvider {
/**
* The module name of the JdkConsole default provider.
*/
String DEFAULT_PROVIDER_MODULE_NAME = "java.base";
String DEFAULT_PROVIDER_MODULE_NAME = "jdk.internal.le";

/**
* {@return the Console instance, or {@code null} if not available}
Expand Down
4 changes: 3 additions & 1 deletion src/java.base/share/classes/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
java.management,
java.rmi,
jdk.charsets,
jdk.internal.le,
jdk.jartool,
jdk.jlink,
jdk.jfr,
Expand Down Expand Up @@ -306,7 +307,8 @@
jdk.net,
jdk.sctp;
exports sun.nio.cs to
jdk.charsets;
jdk.charsets,
jdk.internal.le;
exports sun.nio.fs to
jdk.net;
exports sun.reflect.annotation to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.io;
package jdk.internal.console;

import java.io.IOException;
import java.io.Reader;
Expand Down
Loading

0 comments on commit 951ec6a

Please sign in to comment.