QuagentSocket.java

import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*;

/**
 */
public class QuagentSocket {

    SocketChannel sChannel;

    public QuagentSocket (SocketChannel s) {

	    sChannel = s;
	    // make sure the socketchannel has connected
	    try {
		while (!sChannel.finishConnect())
		    ;
	    } catch (IOException e) {
		System.out.println("IO Error:"+e.getMessage());
	    }
    }

    public int sendString(String msg) {

	int numBytesWritten = 0;

	try {
	    ByteBuffer buf = ByteBuffer.wrap( msg.getBytes() ); 

	    numBytesWritten = sChannel.write(buf);

	} catch (IOException e) {
	    System.out.println("IO Error:"+e.getMessage());
	}

	return numBytesWritten;
    }

    public String receiveString() {

	ByteBuffer buf = ByteBuffer.allocate(1024);
	int numBytesRead = 0;
	String msg=null;

	try {

	    buf.clear();
	    numBytesRead = sChannel.read(buf);


	    if(numBytesRead < 0)
		return null;
	    if(numBytesRead == 0)
		return "";

	    buf.flip();
	    Charset charset=Charset.forName("US-ASCII");
	    CharsetDecoder decoder = charset.newDecoder();
	    CharBuffer charBuffer = decoder.decode(buf);
	    msg = new String(charBuffer.toString());


	} catch (IOException e){

	    System.out.println("IO Error:" + e.getMessage());
	}

	return msg;

    }

    public int read(byte[] msg, int len) {

	ByteBuffer buf = ByteBuffer.allocate(len);

	int numTotalRead = 0;

	try {

	    buf.clear();
	    int numBytesRead = 0;
	    
	    while(numTotalRead < len) {
		numBytesRead=sChannel.read(buf);

		if(numBytesRead <= 0)
		    return numBytesRead;

		numTotalRead += numBytesRead;
	    }

	    buf.flip();
	    buf.get(msg);

	} catch (IOException e){

	    System.out.println("IO Error:" + e.getMessage());
	}

	return numTotalRead;

    }

    public String getLine() {

	String msg = null;
	StringBuffer line = new StringBuffer();
	ByteBuffer buf = ByteBuffer.allocate(1);
	byte c=0; 

	try {
	    
	    int numBytesRead;
	    do {
		do{
		    buf.clear();
		numBytesRead = sChannel.read(buf);
		}while(numBytesRead == 0);
		if(numBytesRead < 0)
		    break;

		buf.flip();
		c = buf.get(0);

		if(c != 10 && c != 13) {
		    line.append((char)c);
		}

		buf.clear();

	    }while (c!=10 && c != 13);

	    if (numBytesRead < 0)
		return null;

	    msg = line.toString();
	} catch (IOException e){

	    System.out.println("IO Error:" + e.getMessage());
	}

	return msg;


    }
    
    public int write(byte[] msg, int len) {

	ByteBuffer buf = ByteBuffer.allocate(len);
	buf.clear();
	buf.put(msg);

	int numTotalWritten= 0;

	try {

	    int numBytesWritten= 0;
	    
	    while(numTotalWritten< len) {
		numBytesWritten=sChannel.write(buf);

		if(numBytesWritten <= 0)
		    return numBytesWritten;

		numTotalWritten += numBytesWritten;
	    }


	} catch (IOException e){

	    System.out.println("IO Error:" + e.getMessage());
	}

	return numTotalWritten;

    }

}


Generated by GNU enscript 1.6.1.