package com.barchart.udt.net;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.IllegalBlockingModeException;
import com.barchart.udt.ErrorUDT;
import com.barchart.udt.SocketUDT;
public class NetOutputStreamUDT extends OutputStream {
protected final SocketUDT socketUDT;
public NetOutputStreamUDT(final SocketUDT socketUDT) {
if (!socketUDT.isBlocking()) {
throw new IllegalBlockingModeException();
}
this.socketUDT = socketUDT;
}
@Override
public void write(final int b) throws IOException {
final byte[] bytes = { (byte) b };
write(bytes);
}
@Override
public void write(final byte[] bytes) throws IOException {
write(bytes, 0, bytes.length);
}
@Override
public void write(final byte[] bytes, final int off, final int len)
throws IOException {
int bytesRemaining = len;
while (bytesRemaining > 0) {
final int count = socketUDT.send(bytes, off + len - bytesRemaining,
off + len);
if (count > 0) {
bytesRemaining -= count;
continue;
}
if (count == 0) {
throw new ExceptionSendUDT(socketUDT.id(),
ErrorUDT.USER_DEFINED_MESSAGE, "UDT send time out");
}
throw new IllegalStateException(
"Socket has been chaged to non-blocking");
}
}
@Override
public void close() throws IOException {
socketUDT.close();
}
}