CometWriterpublic class CometWriter extends Object Non blocking Writer. This class can be used from a
CometHandler to execute non blocking write. If the write was
incomplete, the CometHandler must register itself using
CometContext.registerAsyncWrite() |
Fields Summary |
---|
private boolean | isComplete | private SocketChannel | socketChannel | protected byte[] | chunkLengthBuffer used for chunk length conversion. | private static final ByteBuffer | end | private boolean | readyIs this CometWriter ready |
Constructors Summary |
---|
public CometWriter()
chunkLength = new byte[10];
chunkLength[8] = (byte) '\r";
chunkLength[9] = (byte) '\n";
|
Methods Summary |
---|
public boolean | isComplete()
return isComplete;
| public boolean | isReady()Return true if this instance is ready to read.
return ready;
| public void | recycle()
isComplete = true;
socketChannel = null;
| protected void | setChannel(java.nio.channels.SocketChannel socketChannel)
this.socketChannel = socketChannel;
| public void | setReady(boolean ready)false if this instance is no longer ready to read.
this.ready = ready;
| public int | write(byte[] buf)
return write(buf,0,buf.length);
| public int | write(byte[] buf, int off, int len)
if (!ready){
throw new IllegalStateException("This CometWriter is no longer usable.");
}
int pos = 7;
int current = len;
// Force a blocking read.
if (isComplete) {
while (current > 0) {
int digit = current % 16;
current = current / 16;
chunkLength[pos--] = HexUtils.HEX[digit];
}
OutputWriter.flushChannel(socketChannel,
ByteBuffer.wrap(chunkLength, pos + 1, 9 - pos));
}
int nWrite = socketChannel.write(ByteBuffer.wrap(buf,off,len));
if (nWrite == len) {
isComplete = true;
OutputWriter.flushChannel(socketChannel,
ByteBuffer.wrap(chunkLength, 8, 2));
OutputWriter.flushChannel(socketChannel,end.slice());
} else {
isComplete = false;
}
return nWrite;
|
|