Experimental improvement to client.

This commit is contained in:
Michael Jumper
2010-09-06 13:41:11 -07:00
parent 2a2eb6ae39
commit df72c91b30

View File

@@ -25,6 +25,8 @@ import java.net.Socket;
import java.io.InputStream; import java.io.InputStream;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import net.sourceforge.guacamole.instruction.Instruction; import net.sourceforge.guacamole.instruction.Instruction;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
@@ -34,13 +36,13 @@ import net.sourceforge.guacamole.event.PointerEvent;
public class GuacamoleClient extends Client { public class GuacamoleClient extends Client {
private Socket sock; private Socket sock;
private InputStream input; private Reader input;
public GuacamoleClient(String hostname, int port) throws GuacamoleException { public GuacamoleClient(String hostname, int port) throws GuacamoleException {
try { try {
sock = new Socket(InetAddress.getByName(hostname), port); sock = new Socket(InetAddress.getByName(hostname), port);
input = new BufferedInputStream(sock.getInputStream()); input = new InputStreamReader(sock.getInputStream());
} }
catch (IOException e) { catch (IOException e) {
throw new GuacamoleException(e); throw new GuacamoleException(e);
@@ -69,28 +71,33 @@ public class GuacamoleClient extends Client {
} }
} }
private StringBuilder instructionBuffer = new StringBuilder(); private int bufferPos = 0;
private char[] buffer = new char[20000];
public Instruction nextInstruction(boolean blocking) throws GuacamoleException { public Instruction nextInstruction(boolean blocking) throws GuacamoleException {
try { try {
// While we're blocking, or input is available // While we're blocking, or input is available
while (blocking || input.available() > 0) { while (blocking || input.ready()) {
int readChar = input.read(); int numRead = input.read(buffer, bufferPos, buffer.length - bufferPos);
if (readChar == -1) if (numRead == -1)
return null; return null;
// Add to buffer for (int i=bufferPos+numRead-1; i>=bufferPos; i--) {
instructionBuffer.append((char) readChar);
char readChar = buffer[i];
// If end of instruction, return it. // If end of instruction, return it.
if (readChar == ';') { if (readChar == ';') {
// Get instruction, reset buffer // Get instruction
final String instruction = instructionBuffer.toString(); final String instruction = new String(buffer, 0, i+1);
instructionBuffer.setLength(0);
// Reset buffer
bufferPos = bufferPos + numRead - i - 1;
System.arraycopy(buffer, i+1, buffer, 0, bufferPos);
// Return instruction string wrapped in Instruction class // Return instruction string wrapped in Instruction class
return new Instruction() { return new Instruction() {
@@ -102,6 +109,10 @@ public class GuacamoleClient extends Client {
}; };
} }
}
bufferPos += numRead;
} // End read loop } // End read loop
} }