Skip to content
Open
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
Expand Up @@ -16,6 +16,7 @@
package org.netbeans.modules.nbcode.java.notebook;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -53,14 +54,16 @@ public synchronized int read(byte[] b, int off, int len) throws IOException {
NbCodeLanguageClient client = this.client.get();
if (client == null) {
LOG.log(Level.WARNING, "client is null");
return -1;
throw new EOFException("User input dismissed");
}
CompletableFuture<String> future = client.showInputBox(new ShowInputBoxParams(USER_PROMPT_REQUEST, "", true));
String userInput = future.get();

if (userInput == null) {
LOG.log(Level.WARNING, "User input is null");
return -1;
// Workaround: jshell closes the input stream when -1 is returned and provides no way to reset it.
// This hack bypasses that behavior to prevent the stream from being closed.
throw new EOFException("User input dismissed");
}

byte[] inputBytes = (userInput + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
Expand All @@ -80,6 +83,9 @@ public synchronized int read(byte[] b, int off, int len) throws IOException {
public int read() throws IOException {
byte[] oneByte = new byte[1];
int n = read(oneByte, 0, 1);
return (n == -1) ? -1 : oneByte[0] & 0xFF;
if (n == -1) {
throw new EOFException("User input dismissed");
}
return oneByte[0] & 0xFF;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.netbeans.modules.nbcode.java.notebook;

import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -33,10 +34,10 @@ public void tearDown() {
@Test
public void testReadNoClient() throws IOException {
inputStream = new CustomInputStream(null);
assertEquals(-1, inputStream.read());
Assert.assertThrows(EOFException.class, ()-> inputStream.read());

byte[] buffer = new byte[10];
assertEquals(-1, inputStream.read(buffer, 0, 10));
Assert.assertThrows(EOFException.class, ()-> inputStream.read(buffer, 0, 10));
}

@Test
Expand Down