protected WsByteBuffer getCurrentByteBuffer ( int sizeNeeded ) { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEntryEnabled ( ) ) SibTr . entry ( this , tc , "getCurrentByteBuffer" , Integer . valueOf ( sizeNeeded ) ) ; WsByteBuffer byteBuffer = null ; // First have a look in the dataList for a buffer. if ( dataList . size ( ) == 0 ) { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) SibTr . debug ( this , tc , "Creating first buffer" ) ; // This is the first piece of data to be written into the buffer. As such, we simply // allocate a new buffer and return that dataList = new ArrayList < WsByteBuffer > ( ) ; byteBuffer = createNewWsByteBuffer ( sizeNeeded ) ; dataList . add ( byteBuffer ) ; } else { // Otherwise get the last byte buffer in the list byteBuffer = dataList . get ( dataList . size ( ) - 1 ) ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) SibTr . debug ( this , tc , "Last buffer: " , byteBuffer ) ; // Now check and see if there is enough room in there for data that needs to be written. // If there is enough room, great - we return that buffer. Otherwise we need to complete // the current buffer and start a new one. if ( byteBuffer . remaining ( ) < sizeNeeded ) { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) SibTr . debug ( this , tc , "No room in buffer, creating a new one" ) ; // First flip the finished one byteBuffer . flip ( ) ; // Now create a new one and add it onto the list byteBuffer = createNewWsByteBuffer ( sizeNeeded ) ; dataList . add ( byteBuffer ) ; } } if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEntryEnabled ( ) ) SibTr . exit ( this , tc , "getCurrentByteBuffer" , byteBuffer ) ; return byteBuffer ; }
This method will return a byte buffer that can be written to for the amount of data that needs to be written . If there is no room left in the current byte buffer a new one will be created and added to the list .