package org.xnio.conduits;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import org.xnio.ChannelListener;
import org.xnio.Option;
import org.xnio.XnioExecutor;
import org.xnio.XnioIoThread;
import org.xnio.XnioWorker;
import org.xnio.channels.CloseListenerSettable;
import org.xnio.channels.Configurable;
import org.xnio.channels.WritableMessageChannel;
import org.xnio.channels.WriteListenerSettable;
public final class ConduitWritableMessageChannel implements WritableMessageChannel, WriteListenerSettable<ConduitWritableMessageChannel>, CloseListenerSettable<ConduitWritableMessageChannel>, Cloneable {
private final Configurable configurable;
private MessageSinkConduit conduit;
private ChannelListener<? super ConduitWritableMessageChannel> writeListener;
private ChannelListener<? super ConduitWritableMessageChannel> closeListener;
public ConduitWritableMessageChannel(final Configurable configurable, final MessageSinkConduit conduit) {
this.configurable = configurable;
this.conduit = conduit;
conduit.setWriteReadyHandler(new WriteReadyHandler.ChannelListenerHandler<ConduitWritableMessageChannel>(this));
}
public MessageSinkConduit getConduit() {
return conduit;
}
public void setConduit(final MessageSinkConduit conduit) {
this.conduit = conduit;
}
public ChannelListener<? super ConduitWritableMessageChannel> getWriteListener() {
return writeListener;
}
public void setWriteListener(final ChannelListener<? super ConduitWritableMessageChannel> writeListener) {
this.writeListener = writeListener;
}
public ChannelListener<? super ConduitWritableMessageChannel> getCloseListener() {
return closeListener;
}
public void setCloseListener(final ChannelListener<? super ConduitWritableMessageChannel> closeListener) {
this.closeListener = closeListener;
}
public ChannelListener.Setter<ConduitWritableMessageChannel> getWriteSetter() {
return new WriteListenerSettable.Setter<ConduitWritableMessageChannel>(this);
}
public ChannelListener.Setter<ConduitWritableMessageChannel> getCloseSetter() {
return new CloseListenerSettable.Setter<ConduitWritableMessageChannel>(this);
}
public void suspendWrites() {
conduit.suspendWrites();
}
public void resumeWrites() {
conduit.resumeWrites();
}
public void wakeupWrites() {
conduit.wakeupWrites();
}
public boolean isWriteResumed() {
return conduit.isWriteResumed();
}
public void awaitWritable() throws IOException {
conduit.awaitWritable();
}
public void awaitWritable(final long time, final TimeUnit timeUnit) throws IOException {
conduit.awaitWritable(time, timeUnit);
}
public boolean send(final ByteBuffer dst) throws IOException {
return conduit.send(dst);
}
public boolean send(final ByteBuffer[] srcs) throws IOException {
return conduit.send(srcs, 0, srcs.length);
}
public boolean send(final ByteBuffer[] dsts, final int offs, final int len) throws IOException {
return conduit.send(dsts, offs, len);
}
@Override
public boolean sendFinal(ByteBuffer buffer) throws IOException {
return conduit.sendFinal(buffer);
}
@Override
public boolean sendFinal(ByteBuffer[] buffers) throws IOException {
return conduit.sendFinal(buffers, 0, buffers.length);
}
@Override
public boolean sendFinal(ByteBuffer[] buffers, int offs, int len) throws IOException {
return conduit.sendFinal(buffers, offs, len);
}
public boolean flush() throws IOException {
return conduit.flush();
}
public boolean supportsOption(final Option<?> option) {
return configurable.supportsOption(option);
}
public <T> T getOption(final Option<T> option) throws IOException {
return configurable.getOption(option);
}
public <T> T setOption(final Option<T> option, final T value) throws IllegalArgumentException, IOException {
return configurable.setOption(option, value);
}
public void shutdownWrites() throws IOException {
conduit.terminateWrites();
}
public boolean isOpen() {
return ! conduit.isWriteShutdown();
}
public void close() throws IOException {
conduit.truncateWrites();
}
@Deprecated
public XnioExecutor getWriteThread() {
return conduit.getWriteThread();
}
public XnioIoThread getIoThread() {
return conduit.getWriteThread();
}
public XnioWorker getWorker() {
return conduit.getWorker();
}
public ConduitWritableMessageChannel clone() {
try {
return (ConduitWritableMessageChannel) super.clone();
} catch (CloneNotSupportedException e) {
throw new IllegalStateException(e);
}
}
}