Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, 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 @@ -395,7 +395,9 @@ public synchronized void write(int b) throws IOException {
buffer = Arrays.copyOf(buffer, 2 * buffer.length);
}

buffer[bp++] = b;
// Can be negative because widening from byte in write(byte[], int, int).
// java.io.OutputStream.write(int b) stipulates "The 24 high-order bits of b are ignored."
buffer[bp++] = b & 0xff;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cause is that OutputStream.write(byte[], int, int) provides negative bytes. I recommend you to update the "can be negative" comment above to be like:

// Can be negative because widening from byte in write(byte[], int, int).

Also note, there is another usage of b below in case READ_CHARS -> but I think it is also a bug; it should be readInt(1).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cause is that OutputStream.write(byte[], int, int) provides negative bytes.

I seem to recall that perhaps so. Your suggestion is better.

it should be readInt(1).

Do you mean int len = readInt(b);? I will update it but unfortunately I have no idea which is correct. Do you have any idea to test that the fix is correct?
I could not tell the difference in jshell by playing with some input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that readInt is probably fine because the buffer is large enough, plus the result is unused, so there was no observable consequence.

Also in principle, we will not approve patches unless oca is cleared; meanwhile please update the 2024 last updated year in the license header of 2 files to 2025. (I thought this rule was mentioned in the guide, but apparently it wasnt!)

Copy link
Contributor Author

@tats-u tats-u Apr 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which notation is the best?

  • 2023-2025 (uses the ASCII hypen)
  • 2023–2025 (uses U+2013 EN dash)
  • 2023, 2024, 2025

Looks like "Copyright (c) A, B" means the the closed interval [A, B] (i.e. A–B).

Is "2023, 2025" correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, Copyright (c) 2023, 2025, Oracle is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Does the current change look good to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But I will get a more professional jshell reviewer to double check once the oca is resolved.


switch (Task.values()[buffer[0]]) {
case WRITE_CHARS -> {
Expand All @@ -412,7 +414,7 @@ public synchronized void write(int b) throws IOException {
}
case READ_CHARS -> {
if (bp >= 5) {
int len = readInt(b);
int len = readInt(1);
int c = console.reader().read();
//XXX: EOF handling!
sendChars(sinkOutput, new char[] {(char) c}, 0, 1);
Expand Down
27 changes: 26 additions & 1 deletion test/langtools/jdk/jshell/ConsoleTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, 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 @@ -157,6 +157,31 @@ public void close() throws IOException {}
assertEquals(sb.toString(), expected);
}

@Test
public void testConsoleUnicodeWritingTest() {
StringBuilder sb = new StringBuilder();
console = new ThrowingJShellConsole() {
@Override
public PrintWriter writer() {
return new PrintWriter(new Writer() {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
sb.append(cbuf, off, len);
}
@Override
public void flush() throws IOException {}
@Override
public void close() throws IOException {}
});
}
};
int count = 384; // 128-255, 384-511, 640-767, ... (JDK-8355371)
String testStr = "\u30A2"; // Japanese katakana (A2 >= 80) (JDK-8354910)
assertEval("System.console().writer().write(\"" + testStr + "\".repeat(" + count + "))");
String expected = testStr.repeat(count);
assertEquals(sb.toString(), expected);
}

@Test
public void testConsoleMultiThreading() {
StringBuilder sb = new StringBuilder();
Expand Down