From c2af272e7bed37082aa524ab938c3dc6f4db0160 Mon Sep 17 00:00:00 2001 From: Dominik Downarowicz Date: Tue, 15 Mar 2022 22:50:29 +0100 Subject: [PATCH] Check if connection is available --- .../java/at/downardo/j3270Server/Telnet.java | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/main/java/at/downardo/j3270Server/Telnet.java b/src/main/java/at/downardo/j3270Server/Telnet.java index bbc6be6..d862f9f 100644 --- a/src/main/java/at/downardo/j3270Server/Telnet.java +++ b/src/main/java/at/downardo/j3270Server/Telnet.java @@ -139,44 +139,44 @@ public static int[] TelnetRead(BufferedInputStream in, int[] buf) throws IOExcep int state = NORMAL; int n = 0; while(n <= (buf.length-1)) { - int bufRead = in.read(); - - - - if(bufRead == 0) { - continue; - } - - - switch(state) { - case NORMAL: - if(bufRead == IAC) { - state = COMMAND; - }else { - buf[n] = bufRead; - n++; + if(in.available() > 0) { + int bufRead = in.read(); + + if(bufRead == 0) { + continue; } - break; - case COMMAND: - if(bufRead == 0xff) { - buf[n] = 0xff; + + + switch(state) { + case NORMAL: + if(bufRead == IAC) { + state = COMMAND; + }else { + buf[n] = bufRead; + n++; + } + break; + case COMMAND: + if(bufRead == 0xff) { + buf[n] = 0xff; + + n++; + state = NORMAL; + }else if(bufRead == SB) { + state = SUBNEG; + }else { + state = NORMAL; + } + break; + case SUBNEG: + if(bufRead == SE) { + state = NORMAL; + }else { + //remain in subnegotiation consumin bytes until we get SE + } + break; - n++; - state = NORMAL; - }else if(bufRead == SB) { - state = SUBNEG; - }else { - state = NORMAL; } - break; - case SUBNEG: - if(bufRead == SE) { - state = NORMAL; - }else { - //remain in subnegotiation consumin bytes until we get SE - } - break; - } }