1616package org .netbeans .modules .nbcode .java .notebook ;
1717
1818import java .io .ByteArrayInputStream ;
19+ import java .io .EOFException ;
1920import java .io .IOException ;
2021import java .io .InputStream ;
2122import 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}
0 commit comments