public void send(ByteBuffer buf)
{
if (closed.get())
{
throw new TransportException("sender is closed", exception);
}
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)
{
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;
}
if (closed.get())
{
throw new TransportException("sender is closed", exception);
}
if (head - tail >= size)
{
throw new TransportException(String.format("write timed out: %s, %s", head, tail));
}
}
continue;
}