private boolean immediateRead ( int amountToRead ) { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "immediateRead, Executing a read" ) ; } if ( amountToRead > 1 ) { //Allocate a new temp buffer, then set the position to 0 and limit to the amount we want to read //Copy in the current this.buffer as it should only have one byte in it WsByteBuffer tempBuffer = allocateBuffer ( amountToRead ) ; tempBuffer . position ( 0 ) ; tempBuffer . limit ( amountToRead ) ; tempBuffer . put ( _buffer ) ; tempBuffer . position ( 1 ) ; _buffer . release ( ) ; _buffer = tempBuffer ; tempBuffer = null ; _tcpContext . getReadInterface ( ) . setBuffer ( _buffer ) ; long bytesRead = 0 ; try { bytesRead = _tcpContext . getReadInterface ( ) . read ( 0 , WCCustomProperties31 . UPGRADE_READ_TIMEOUT ) ; } catch ( IOException readException ) { //If we encounter an exception here we need to return the 1 byte that we already have. //Returned true immediately and the next read will catch the exception and propagate it properly if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "immediateRead, The read encountered an exception. " + readException ) ; Tr . debug ( tc , "immediateRead, Return with our one byte" ) ; } configurePostReadBuffer ( ) ; return true ; } if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "immediateRead, Complete, " + bytesRead ) ; } //Get the buffer from the TCP Channel after we have told them to read. _buffer = _tcpContext . getReadInterface ( ) . getBuffer ( ) ; //We don't need to check for null first as we know we will always get the buffer we just set configurePostReadBuffer ( ) ; // record the new amount of data read from the channel _totalBytesRead += _buffer . remaining ( ) ; } //We will return true here in all circumstances because we always have 1 byte read from the isReady call or the initial read of the connection return true ; }
This method will execute an immediate read The immediate read will issue a read to the TCP Channel and immediately return with whatever can fit in the buffers This will only ever be called after we had read the 1 byte from the isReady or initialRead methods . As such we will allocate a buffer and add in the 1 byte . This method should always return at least 1 byte even if the read fails since we have already read that byte