FileDocCategorySizeDatePackage
ObjectReader.javaAPI DocApache Tomcat 6.0.145281Fri Jul 20 04:20:34 BST 2007org.apache.catalina.tribes.io

ObjectReader

public class ObjectReader extends Object
The object reader object is an object used in conjunction with java.nio TCP messages. This object stores the message bytes in a XByteBuffer until a full package has been received. This object uses an XByteBuffer which is an extendable object buffer that also allows for message encoding and decoding.
author
Filip Hanik
version
$Revision: 467173 $, $Date: 2006-10-24 01:12:17 +0200 (mar., 24 oct. 2006) $

Fields Summary
protected static org.apache.juli.logging.Log
log
private XByteBuffer
buffer
protected long
lastAccess
protected boolean
accessed
private boolean
cancelled
Constructors Summary
public ObjectReader(SocketChannel channel)
Creates an ObjectReader for a TCP NIO socket channel

param
channel - the channel to be read.


                          
       
        this(channel.socket());
    
public ObjectReader(Socket socket)
Creates an ObjectReader for a TCP socket

param
socket Socket

        try{
            this.buffer = new XByteBuffer(socket.getReceiveBufferSize(), true);
        }catch ( IOException x ) {
            //unable to get buffer size
            log.warn("Unable to retrieve the socket receiver buffer size, setting to default 43800 bytes.");
            this.buffer = new XByteBuffer(43800,true);
        }
    
Methods Summary
public synchronized voidaccess()

        this.accessed = true;
        this.lastAccess = System.currentTimeMillis();
    
public intappend(java.nio.ByteBuffer data, int len, boolean count)
Append new bytes to buffer.

see
XByteBuffer#countPackages()
param
data new transfer buffer
param
off offset
param
len length in buffer
return
number of messages that sended to callback
throws
java.io.IOException

       buffer.append(data,len);
       int pkgCnt = -1;
       if ( count ) pkgCnt = buffer.countPackages();
       return pkgCnt;
   
public intappend(byte[] data, int off, int len, boolean count)

        buffer.append(data,off,len);
        int pkgCnt = -1;
        if ( count ) pkgCnt = buffer.countPackages();
        return pkgCnt;
    
public intbufferSize()

        return buffer.getLength();
    
public voidclose()

        this.buffer = null;
    
public intcount()
Returns the number of packages that the reader has read

return
int

        return buffer.countPackages();
    
public org.apache.catalina.tribes.ChannelMessage[]execute()
Send buffer to cluster listener (callback). Is message complete receiver send message to callback?

see
org.apache.catalina.tribes.transport.ClusterReceiverBase#messageDataReceived(ChannelMessage)
see
XByteBuffer#doesPackageExist()
see
XByteBuffer#extractPackage(boolean)
return
number of received packages/messages
throws
java.io.IOException

        int pkgCnt = buffer.countPackages();
        ChannelMessage[] result = new ChannelMessage[pkgCnt];
        for (int i=0; i<pkgCnt; i++)  {
            ChannelMessage data = buffer.extractPackage(true);
            result[i] = data;
        }
        return result;
    
public synchronized voidfinish()

        this.accessed = false;
        this.lastAccess = System.currentTimeMillis();
    
public longgetLastAccess()

        return lastAccess;
    
public booleanhasPackage()

        return buffer.countPackages(true)>0;
    
public booleanisAccessed()

        return this.accessed;
    
public booleanisCancelled()

        return cancelled;
    
public voidsetCancelled(boolean cancelled)

        this.cancelled = cancelled;
    
public voidsetLastAccess(long lastAccess)

        this.lastAccess = lastAccess;