src/_boneyard/org/michailov/async/io/AsyncByteStreamReader.java
private void submitRead2() {
        try {
            // Check what's available from the stream.
            int available = _inputStream.available();
            if (available > 0) {
                
                // If something is available, try to read the minimum of that and what's left from the request.
                int target = Math.min(available, _asyncState.remaining);
                int actual = _inputStream.read(_asyncState.buff, _asyncState.offset, target);
                if (actual != 0) {
                    
                    // If bytes were read, update the state. 
                    if (actual > 0) {
                        _asyncState.offset += actual;
                        _asyncState.remaining -= actual;
                        _asyncState.total += actual;
                    }
                    
                    // If EOF was received from the stream and some bytes have been read as part of this request,
                    // we must return that number. We'll set the EOF state to RECEIVED, so we return a EOF on the
                    // next request right away.
                    // If EOF was received from the stream an no bytes have been read as part of this request,
                    // we'll report EOF right now.
                    if (actual == EOF) {
                        if (_asyncState.total > 0) {
                            _eofState = EOFState.RECEIVED;
                        }
                        else {
                            _eofState = EOFState.REPORTED;
                        }
                    }
                    
                    // Complete the original future now if any of these is true:
                    //  - We just received EOF from the stream.
                    //  - The caller has requested to complete when AVAILABLE.
                    //  - The requested number of bytes has been read. 
                    if (actual == EOF || _asyncState.completeWhen == CompleteWhen.AVAILABLE || _asyncState.remaining == 0) {
                        
                        // Cache everything that is needed from the state, and delete the state.
                        CompletableFuture<Integer> future = _asyncState.future;
                        int total = _asyncState.total > 0 ? _asyncState.total : EOF;
                        _asyncState = null;
                        
                        // Complete the original future and return. 
                        future.complete(Integer.valueOf(total));
                        return;
                    }
                }
            }
        }
        catch(IOException ex) {
            
            // Upon exception, delete the state, and complete the original future.
            CompletableFuture<Integer> future = _asyncState.future;
            _asyncState = null;
            
            future.completeExceptionally(ex);
        }
        
        // There was nothing available from the stream.
        // First, check how long this request has taken so far.
        // If time is up, complete the original future with a TimeoutException, and return.
        if (_asyncState.timeoutMillis >= 0) {
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - _asyncState.startTimeMillis > _asyncState.timeoutMillis) {
                CompletableFuture<Integer> future = _asyncState.future;
                _asyncState = null;
                
                future.completeExceptionally(new TimeoutException());
                return;
            }
        }
        
        // Schedule yet another operation.
        ForkJoinPool.commonPool().execute(() -> submitRead());
    }
/**
     * Submits a new async operation or completes synchronously.
     */
Submits a new async operation or completes synchronously.
Submits a new async operation or completes synchronously .
private void submitRead2() {
        try {
            int available = _inputStream.available();
            if (available > 0) {
                int target = Math.min(available, _asyncState.remaining);
                int actual = _inputStream.read(_asyncState.buff, _asyncState.offset, target);
                if (actual != 0) {
                    if (actual > 0) {
                        _asyncState.offset += actual;
                        _asyncState.remaining -= actual;
                        _asyncState.total += actual;
                    }
                    if (actual == EOF) {
                        if (_asyncState.total > 0) {
                            _eofState = EOFState.RECEIVED;
                        }
                        else {
                            _eofState = EOFState.REPORTED;
                        }
                    }
                    if (actual == EOF || _asyncState.completeWhen == CompleteWhen.AVAILABLE || _asyncState.remaining == 0) {
                        CompletableFuture<Integer> future = _asyncState.future;
                        int total = _asyncState.total > 0 ? _asyncState.total : EOF;
                        _asyncState = null;
                        future.complete(Integer.valueOf(total));
                        return;
                    }
                }
            }
        }
        catch(IOException ex) {
            CompletableFuture<Integer> future = _asyncState.future;
            _asyncState = null;
            future.completeExceptionally(ex);
        }
        if (_asyncState.timeoutMillis >= 0) {
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - _asyncState.startTimeMillis > _asyncState.timeoutMillis) {
                CompletableFuture<Integer> future = _asyncState.future;
                _asyncState = null;
                future.completeExceptionally(new TimeoutException());
                return;
            }
        }
        ForkJoinPool.commonPool().execute(() -> submitRead());
    }
private void submitRead2 ( ) { try { int available = _inputStream . available ( ) ; if ( available > 0 ) { int target = Math . min ( available , _asyncState . remaining ) ; int actual = _inputStream . read ( _asyncState . buff , _asyncState . offset , target ) ; if ( actual != 0 ) { if ( actual > 0 ) { _asyncState . offset += actual ; _asyncState . remaining -= actual ; _asyncState . total += actual ; } if ( actual == EOF ) { if ( _asyncState . total > 0 ) { _eofState = EOFState . RECEIVED ; } else { _eofState = EOFState . REPORTED ; } } if ( actual == EOF || _asyncState . completeWhen == CompleteWhen . AVAILABLE || _asyncState . remaining == 0 ) { CompletableFuture < Integer > future = _asyncState . future ; int total = _asyncState . total > 0 ? _asyncState . total : EOF ; _asyncState = null ; future . complete ( Integer . valueOf ( total ) ) ; return ; } } } } catch ( IOException ex ) { CompletableFuture < Integer > future = _asyncState . future ; _asyncState = null ; future . completeExceptionally ( ex ) ; } if ( _asyncState . timeoutMillis >= 0 ) { long currentTimeMillis = System . currentTimeMillis ( ) ; if ( currentTimeMillis - _asyncState . startTimeMillis > _asyncState . timeoutMillis ) { CompletableFuture < Integer > future = _asyncState . future ; _asyncState = null ; future . completeExceptionally ( new TimeoutException ( ) ) ; return ; } } ForkJoinPool . commonPool ( ) . execute ( ( ) -> submitRead ( ) ) ; }
Submits a new async operation or completes synchronously.
Submits a new async operation or completes synchronously .
// Check what's available from the stream.
 // If something is available, try to read the minimum of that and what's left from the request.
 // If bytes were read, update the state. 
 // If EOF was received from the stream and some bytes have been read as part of this request,
 // we must return that number. We'll set the EOF state to RECEIVED, so we return a EOF on the
 // next request right away.
 // If EOF was received from the stream an no bytes have been read as part of this request,
 // we'll report EOF right now.
 // Complete the original future now if any of these is true:
 //  - We just received EOF from the stream.
 //  - The caller has requested to complete when AVAILABLE.
 //  - The requested number of bytes has been read. 
 // Cache everything that is needed from the state, and delete the state.
 // Complete the original future and return. 
 // Upon exception, delete the state, and complete the original future.
 // There was nothing available from the stream.
 // First, check how long this request has taken so far.
 // If time is up, complete the original future with a TimeoutException, and return.
 // Schedule yet another operation.
