diff --git a/guacamole-common/src/main/java/net/sourceforge/guacamole/io/GuacamoleReader.java b/guacamole-common/src/main/java/net/sourceforge/guacamole/io/GuacamoleReader.java index 5d4677149..db2c1c1ff 100644 --- a/guacamole-common/src/main/java/net/sourceforge/guacamole/io/GuacamoleReader.java +++ b/guacamole-common/src/main/java/net/sourceforge/guacamole/io/GuacamoleReader.java @@ -48,6 +48,19 @@ import net.sourceforge.guacamole.protocol.GuacamoleInstruction; */ public interface GuacamoleReader { + /** + * Returns whether instruction data is available for reading. Note that + * this does not guarantee an entire instruction is available. If a full + * instruction is not available, this function can return true, and a call + * to read() will still block. + * + * @return true if instruction data is available for reading, false + * otherwise. + * @throws GuacamoleException If an error occurs while checking for + * available data. + */ + public boolean available() throws GuacamoleException; + /** * Reads at least one complete Guacamole instruction, returning a buffer * containing one or more complete Guacamole instructions and no diff --git a/guacamole-common/src/main/java/net/sourceforge/guacamole/io/ReaderGuacamoleReader.java b/guacamole-common/src/main/java/net/sourceforge/guacamole/io/ReaderGuacamoleReader.java index 4d4c5b9b7..c5de457da 100644 --- a/guacamole-common/src/main/java/net/sourceforge/guacamole/io/ReaderGuacamoleReader.java +++ b/guacamole-common/src/main/java/net/sourceforge/guacamole/io/ReaderGuacamoleReader.java @@ -70,6 +70,16 @@ public class ReaderGuacamoleReader implements GuacamoleReader { private char[] buffer = new char[20480]; private int usedLength = 0; + @Override + public boolean available() throws GuacamoleException { + try { + return input.ready() || usedLength != 0; + } + catch (IOException e) { + throw new GuacamoleException(e); + } + } + @Override public char[] read() throws GuacamoleException { diff --git a/guacamole-common/src/main/java/net/sourceforge/guacamole/servlet/GuacamoleTunnelServlet.java b/guacamole-common/src/main/java/net/sourceforge/guacamole/servlet/GuacamoleTunnelServlet.java index b1cef01a9..0d735d17e 100644 --- a/guacamole-common/src/main/java/net/sourceforge/guacamole/servlet/GuacamoleTunnelServlet.java +++ b/guacamole-common/src/main/java/net/sourceforge/guacamole/servlet/GuacamoleTunnelServlet.java @@ -224,8 +224,12 @@ public abstract class GuacamoleTunnelServlet extends HttpServlet { // Get message output bytes out.write(message, 0, message.length); - out.flush(); - response.flushBuffer(); + + // Flush if we expect to wait + if (!reader.available()) { + out.flush(); + response.flushBuffer(); + } // No more messages another stream can take over if (tunnel.hasQueuedReaderThreads())