1 16 package org.apache.catalina.cluster.io; 17 18 import java.nio.ByteBuffer ; 19 import java.nio.channels.Selector ; 20 import java.nio.channels.SocketChannel ; 21 22 35 public class ObjectReader { 36 37 private SocketChannel channel; 38 39 private Selector selector; 40 41 private ListenCallback callback; 42 43 private XByteBuffer buffer; 44 45 51 public ObjectReader(SocketChannel channel, Selector selector, ListenCallback callback, boolean isCompressed) { 52 this.channel = channel; 53 this.selector = selector; 54 this.callback = callback; 55 this.buffer = new XByteBuffer(isCompressed); 56 } 57 58 62 public ListenCallback getCallback() { 63 return callback; 64 } 65 66 70 public SocketChannel getChannel() { 71 return this.channel; 72 } 73 74 83 public int append(byte[] data,int off,int len) throws java.io.IOException { 84 buffer.append(data,off,len); 85 int pkgCnt = buffer.countPackages(); 86 return pkgCnt; 87 } 88 89 98 public int execute() throws java.io.IOException { 99 int pkgCnt = 0; 100 boolean pkgExists = buffer.doesPackageExist(); 101 while ( pkgExists ) { 102 byte[] b = buffer.extractPackage(true); 103 getCallback().messageDataReceived(b); 104 pkgCnt++; 105 pkgExists = buffer.doesPackageExist(); 106 } 107 return pkgCnt; 108 } 109 110 116 public int write(ByteBuffer buf) throws java.io.IOException { 117 return getChannel().write(buf); 118 } 119 120 } 121 | Popular Tags |