public void toArray ( int offset , int length , byte [ ] targetBuffer , int targetOffset ) { // validate the offset, length are ok ByteBuffer . checkOffsetLength ( size ( ) , offset , length ) ; // validate the offset, length are ok ByteBuffer . checkOffsetLength ( targetBuffer . length , targetOffset , length ) ; // will we have a large enough byte[] allocated? if ( targetBuffer . length < length ) { throw new IllegalArgumentException ( "TargetBuffer size must be large enough to hold a byte[] of at least a size=" + length ) ; } // are we actually copying any data? if ( length > 0 ) { // create adjusted versions of read and write positions based // on the offset and length passed into this method int offsetReadPosition = ( this . currentReadPosition + offset ) % this . buffer . length ; int offsetWritePosition = ( this . currentReadPosition + offset + length ) % this . buffer . length ; if ( offsetReadPosition >= offsetWritePosition ) { System . arraycopy ( this . buffer , offsetReadPosition , targetBuffer , targetOffset , this . buffer . length - offsetReadPosition ) ; System . arraycopy ( this . buffer , 0 , targetBuffer , targetOffset + this . buffer . length - offsetReadPosition , offsetWritePosition ) ; } else { System . arraycopy ( this . buffer , offsetReadPosition , targetBuffer , targetOffset , offsetWritePosition - offsetReadPosition ) ; } } }
Will copy data from this ByteBuffer s buffer into the targetBuffer . This method requires the targetBuffer to already be allocated with enough capacity to hold this ByteBuffer s data .