public static final Bytes of ( ByteBuffer bb ) { Objects . requireNonNull ( bb ) ; if ( bb . remaining ( ) == 0 ) { return EMPTY ; } byte [ ] data ; if ( bb . hasArray ( ) ) { data = Arrays . copyOfRange ( bb . array ( ) , bb . position ( ) + bb . arrayOffset ( ) , bb . limit ( ) + bb . arrayOffset ( ) ) ; } else { data = new byte [ bb . remaining ( ) ] ; // duplicate so that it does not change position bb . duplicate ( ) . get ( data ) ; } return new Bytes ( data ) ; }
Creates a Bytes object by copying the data of the given ByteBuffer .