Support for new instruction format, dependency on Apache commons

This commit is contained in:
Michael Jumper
2011-09-10 19:08:42 -07:00
parent e3260ca3e9
commit 12717e038c
3 changed files with 48 additions and 32 deletions

View File

@@ -43,6 +43,13 @@
<version>1.6.1</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -21,9 +21,13 @@ package net.sourceforge.guacamole.io;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.protocol.GuacamoleInstruction;
import net.sourceforge.guacamole.protocol.GuacamoleInstruction.Operation;
import org.apache.commons.lang3.ArrayUtils;
/**
* A GuacamoleReader which wraps a standard Java Reader, using that Reader as
@@ -128,53 +132,58 @@ public class ReaderGuacamoleReader implements GuacamoleReader {
if (instructionBuffer == null)
return null;
// Locate end-of-opcode and end-of-instruction
int opcodeEnd = -1;
int instructionEnd = -1;
// Build list of elements
LinkedList<String> elements = new LinkedList<String>();
while (instructionStart < instructionBuffer.length) {
for (int i=instructionStart; i<instructionBuffer.length; i++) {
// Find end of length
int lengthEnd = ArrayUtils.indexOf(instructionBuffer, '.', instructionStart);
char c = instructionBuffer[i];
// Parse length
int length = Integer.parseInt(new String(
instructionBuffer,
instructionStart,
lengthEnd - instructionStart
));
if (c == ':')
opcodeEnd = i;
// Parse element from just after period
instructionStart = lengthEnd + 1;
String element = new String(
instructionBuffer,
instructionStart,
length
);
else if (c == ';') {
instructionEnd = i;
// Append element to list of elements
elements.addLast(element);
// Read terminator after element
instructionStart += length;
char terminator = instructionBuffer[instructionStart];
// Continue reading instructions after terminator
instructionStart++;
// If we've reached the end of the instruction
if (terminator == ';')
break;
}
}
// If no end-of-instruction marker, malformed.
if (instructionEnd == -1)
throw new GuacamoleException("Malformed instruction.");
// If no end-of-opcode marker, end is end-of-instruction
if (opcodeEnd == -1)
opcodeEnd = instructionEnd;
// Parse opcode
String opcode = new String(instructionBuffer, instructionStart, opcodeEnd - instructionStart);
// Parse args
String[] args;
if (instructionEnd > opcodeEnd)
args = new String(instructionBuffer, opcodeEnd+1, instructionEnd - opcodeEnd - 1).split(",");
else
args = new String[0];
// Pull opcode off elements list
String opcode = elements.removeFirst();
// Create instruction
GuacamoleInstruction instruction = new GuacamoleInstruction(
Operation.fromOpcode(opcode),
args
elements.toArray(new String[elements.size()])
);
// Advance instructionBuffer
instructionStart = instructionEnd + 1;
// Detect end of buffer
if (instructionStart >= instructionBuffer.length)
instructionBuffer = null;
// Return parsed instruction
return instruction;
}

View File

@@ -221,7 +221,7 @@ public abstract class GuacamoleTunnelServlet extends HttpServlet {
tunnel.close();
// End-of-instructions marker
out.write(';');
out.write("0.;");
out.flush();
response.flushBuffer();