public static ByteBuffer minimalBufferFor ( ByteBuffer buf ) { return buf . capacity ( ) > buf . remaining ( ) || ! buf . hasArray ( ) ? ByteBuffer . wrap ( getArray ( buf ) ) : buf ; }
trims size of bytebuffer to exactly number of bytes in it to do not hold too much memory