{
checkNotAlreadyClosed();
if(!senderThread.isAlive())
{
throw new SenderException(String.format("sender thread for socket %s is not alive", _remoteSocketAddress));
}
final int size = buffer.length;
int remaining = buf.remaining();
while (remaining > 0)
{
final int hd = head;
final int tl = tail;
if (hd - tl >= size)
{
flush();
synchronized (notFull)
{
final long start = System.currentTimeMillis();
long elapsed = 0;
while (!closed.get() && head - tail >= size && elapsed < timeout)
{
try
{
notFull.wait(timeout - elapsed);
}
catch (InterruptedException e)
{
// pass
}
elapsed += System.currentTimeMillis() - start;
}
checkNotAlreadyClosed();
if (head - tail >= size)
{
try
{
log.error("write timed out for socket %s: head %d, tail %d", _remoteSocketAddress, head, tail);
throw new SenderException(String.format("write timed out for socket %s: head %d, tail %d", _remoteSocketAddress, head, tail));
}
finally
{
close(false, false);
}