public int append(char chunk[], int offset, int length) throws GuacamoleException {

        int originalOffset = offset;
        int originalLength = length;

        // Process as much of the received chunk as possible
        while (length > 0) {

            int appended = processElement(chunk, offset, length);
            if (appended == 0)
                break;

            length -= appended;
            offset += appended;
        }

        // Update the raw copy of the received instruction with whatever data
        // has now been processed
        int charsParsed = originalLength - length;
        if (charsParsed > 0) {

            System.arraycopy(chunk, originalOffset, rawInstruction, rawInstructionOffset, charsParsed);
            rawInstructionOffset += charsParsed;

            // If the instruction is now complete, we're good to store the
            // parsed instruction for future retrieval via next()
            if (state == State.COMPLETE) {
                parsedInstruction = new ParsedGuacamoleInstruction(elements[0],
                        Arrays.asList(elements).subList(1, elementCount),
                        Arrays.copyOf(rawInstruction, rawInstructionOffset));
                rawInstructionOffset = 0;
            }

        }

        return charsParsed;

    }
Appends data from the given buffer to the current instruction.

@param chunk
    The buffer containing the data to append.

@param offset
    The offset within the buffer where the data begins.

@param length
    The length of the data to append.

@return
    The number of characters appended, or 0 if complete instructions
    have already been parsed and must be read via next() before more
    data can be appended.

@throws GuacamoleException
    If an error occurs while parsing the new data.
guacamole-common/src/main/java/org/apache/guacamole/protocol/GuacamoleParser.java