public GuacamoleInstruction readInstruction()

in guacamole-common/src/main/java/org/apache/guacamole/io/ReaderGuacamoleReader.java [98:143]


    public GuacamoleInstruction readInstruction() throws GuacamoleException {
        try {
            // Loop until the parser has prepared a full instruction
            while (!parser.hasNext()) {

                // Parse as much data from the buffer as we can
                int parsed = 0;
                while (parseStart < usedLength && (parsed = parser.append(buffer, parseStart, usedLength - parseStart)) != 0) {
                    parseStart += parsed;
                }

                // If we still don't have a full instruction attempt to read more data into the buffer
                if (!parser.hasNext()) {

                    // If we have already parsed some of the buffer and the buffer is almost full then we can trim the parsed data off the buffer
                    if (parseStart > 0 && buffer.length - usedLength < GuacamoleParser.INSTRUCTION_MAX_LENGTH) {
                        System.arraycopy(buffer, parseStart, buffer, 0, usedLength - parseStart);
                        usedLength -= parseStart;
                        parseStart = 0;
                    }

                    // Read more instruction data into the buffer
                    int numRead = input.read(buffer, usedLength, buffer.length - usedLength);
                    if (numRead == -1)
                        break;

                    usedLength += numRead;

                }
 
            }

            return parser.next();

        }
        catch (SocketTimeoutException e) {
            throw new GuacamoleUpstreamTimeoutException("Connection to guacd timed out.", e);
        }
        catch (SocketException e) {
            throw new GuacamoleConnectionClosedException("Connection to guacd is closed.", e);
        }
        catch (IOException e) {
            throw new GuacamoleServerException(e);
        }

    }