private void sleepIfRequired(int bytesDown) throws ConnectException {

        snifferSocket.lastReadThreadId = Thread.currentThread().getId();

        if (snifferSocket.lastReadThreadId == snifferSocket.lastWriteThreadId) {
            snifferSocket.potentiallyBufferedOutputBytes = 0;
        }

        if (0 == snifferSocket.receiveBufferSize) {
            snifferSocket.checkConnectionAllowed(1);
        } else {

            int potentiallyBufferedInputBytes = snifferSocket.potentiallyBufferedInputBytes -= bytesDown;

            if (potentiallyBufferedInputBytes < 0) {
                int estimatedNumberOfTcpPackets = 1 + (-1 * potentiallyBufferedInputBytes) / snifferSocket.receiveBufferSize;
                snifferSocket.checkConnectionAllowed(estimatedNumberOfTcpPackets);
                snifferSocket.potentiallyBufferedInputBytes = snifferSocket.receiveBufferSize;
            }

        }

    }
private void sleepIfRequired ( int bytesDown ) throws ConnectException { snifferSocket . lastReadThreadId = Thread . currentThread ( ) . getId ( ) ; if ( snifferSocket . lastReadThreadId == snifferSocket . lastWriteThreadId ) { snifferSocket . potentiallyBufferedOutputBytes = 0 ; } if ( 0 == snifferSocket . receiveBufferSize ) { snifferSocket . checkConnectionAllowed ( 1 ) ; } else { int potentiallyBufferedInputBytes = snifferSocket . potentiallyBufferedInputBytes -= bytesDown ; if ( potentiallyBufferedInputBytes < 0 ) { int estimatedNumberOfTcpPackets = 1 + ( - 1 * potentiallyBufferedInputBytes ) / snifferSocket . receiveBufferSize ; snifferSocket . checkConnectionAllowed ( estimatedNumberOfTcpPackets ) ; snifferSocket . potentiallyBufferedInputBytes = snifferSocket . receiveBufferSize ; } } }
Adds a delay as defined for current {@link SnifferSocketImpl} in {@link ConnectionsRegistry#discoveredDataSources}

Delay is added for each <b>N</b> bytes received where <b>N</b> is the value of {@link SocketOptions#SO_RCVBUF}

If application reads <b>M</b> bytes where (k-1) * N &lt; M  &lt; k * N exactly <b>k</b> delays will be added

A call to {@link SnifferOutputStream} obtained from the same {@link SnifferSocketImpl} and made from the same thread
will reset the number of buffered (i.e. which can be read without delay) bytes to 0 effectively adding a guaranteed
delay to any subsequent {@link SnifferInputStream#read()} request

TODO: consider if {@link java.net.SocketInputStream#available()} method can be of any use here

@param bytesDown number of bytes received from socket
@throws ConnectException on underlying socket exception