Skip to content

Commit 5fc6e25

Browse files
authored
Merge pull request #501 from Achal1607/fix-nb-scanner-issue
Handle user dismissal of input box in notebook instead of closing input stream
2 parents da7f49d + 7afd957 commit 5fc6e25

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/CustomInputStream.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.netbeans.modules.nbcode.java.notebook;
1717

1818
import java.io.ByteArrayInputStream;
19+
import java.io.EOFException;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.lang.ref.WeakReference;
@@ -53,14 +54,16 @@ public synchronized int read(byte[] b, int off, int len) throws IOException {
5354
NbCodeLanguageClient client = this.client.get();
5455
if (client == null) {
5556
LOG.log(Level.WARNING, "client is null");
56-
return -1;
57+
throw new EOFException("User input dismissed");
5758
}
5859
CompletableFuture<String> future = client.showInputBox(new ShowInputBoxParams(USER_PROMPT_REQUEST, "", true));
5960
String userInput = future.get();
6061

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

6669
byte[] inputBytes = (userInput + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
@@ -80,6 +83,9 @@ public synchronized int read(byte[] b, int off, int len) throws IOException {
8083
public int read() throws IOException {
8184
byte[] oneByte = new byte[1];
8285
int n = read(oneByte, 0, 1);
83-
return (n == -1) ? -1 : oneByte[0] & 0xFF;
86+
if (n == -1) {
87+
throw new EOFException("User input dismissed");
88+
}
89+
return oneByte[0] & 0xFF;
8490
}
8591
}

nbcode/notebooks/test/unit/src/org/netbeans/modules/nbcode/java/notebook/CustomInputStreamTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.netbeans.modules.nbcode.java.notebook;
22

3+
import java.io.EOFException;
34
import java.io.IOException;
45
import java.nio.charset.StandardCharsets;
56
import java.util.concurrent.CompletableFuture;
6-
import java.util.function.Consumer;
77
import org.junit.After;
8+
import org.junit.Assert;
89
import org.junit.Before;
910
import org.junit.Test;
1011

@@ -33,10 +34,10 @@ public void tearDown() {
3334
@Test
3435
public void testReadNoClient() throws IOException {
3536
inputStream = new CustomInputStream(null);
36-
assertEquals(-1, inputStream.read());
37+
Assert.assertThrows(EOFException.class, ()-> inputStream.read());
3738

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

4243
@Test

0 commit comments

Comments
 (0)