package org.xnio.conduits;
import static org.xnio._private.Messages.msg;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.xnio.Buffers;
import org.xnio.Pooled;
public final class FramingMessageSinkConduit extends AbstractSinkConduit<StreamSinkConduit> implements MessageSinkConduit {
private final boolean longLengths;
private final Pooled<ByteBuffer> transmitBuffer;
public FramingMessageSinkConduit(final StreamSinkConduit next, final boolean longLengths, final Pooled<ByteBuffer> transmitBuffer) {
super(next);
this.longLengths = longLengths;
this.transmitBuffer = transmitBuffer;
}
public boolean send(final ByteBuffer src) throws IOException {
if (!src.hasRemaining()) {
return false;
}
final ByteBuffer transmitBuffer = this.transmitBuffer.getResource();
final int remaining = src.remaining();
final boolean longLengths = this.longLengths;
final int lengthFieldSize = longLengths ? 4 : 2;
if (remaining > transmitBuffer.capacity() - lengthFieldSize || ! longLengths && remaining > 65535) {
throw msg.txMsgTooLarge();
}
if (transmitBuffer.remaining() < lengthFieldSize + remaining && ! writeBuffer()) {
return false;
}
if (longLengths) {
transmitBuffer.putInt(remaining);
} else {
transmitBuffer.putShort((short) remaining);
}
transmitBuffer.put(src);
writeBuffer();
return true;
}
public boolean send(final ByteBuffer[] srcs, final int offs, final int len) throws IOException {
if (len == 1) {
return send(srcs[offs]);
} else if (! Buffers.hasRemaining(srcs, offs, len)) {
return false;
}
final ByteBuffer transmitBuffer = this.transmitBuffer.getResource();
final long remaining = Buffers.remaining(srcs, offs, len);
final boolean longLengths = this.longLengths;
final int lengthFieldSize = longLengths ? 4 : 2;
if (remaining > transmitBuffer.capacity() - lengthFieldSize || ! longLengths && remaining > 65535) {
throw msg.txMsgTooLarge();
}
if (transmitBuffer.remaining() < lengthFieldSize + remaining && ! writeBuffer()) {
return false;
}
if (longLengths) {
transmitBuffer.putInt((int) remaining);
} else {
transmitBuffer.putShort((short) remaining);
}
Buffers.copy(transmitBuffer, srcs, offs, len);
writeBuffer();
return true;
}
@Override
public boolean sendFinal(ByteBuffer src) throws IOException {
return Conduits.sendFinalBasic(this, src);
}
@Override
public boolean sendFinal(ByteBuffer[] srcs, int offs, int len) throws IOException {
return Conduits.sendFinalBasic(this, srcs, offs, len);
}
private boolean writeBuffer() throws IOException {
final ByteBuffer buffer = transmitBuffer.getResource();
if (buffer.position() > 0) buffer.flip();
try {
while (buffer.hasRemaining()) {
final int res = next.write(buffer);
if (res == 0) {
return false;
}
}
return true;
} finally {
buffer.compact();
}
}
public boolean flush() throws IOException {
return writeBuffer() && next.flush();
}
public void terminateWrites() throws IOException {
transmitBuffer.free();
next.terminateWrites();
}
public void truncateWrites() throws IOException {
transmitBuffer.free();
next.truncateWrites();
}
}