public int toByteArray ( byte [ ] target , int targetOffset ) throws IOException { long length = length ( ) ; if ( ( long ) targetOffset + length > Integer . MAX_VALUE ) { throw new IOException ( "Unable to write - too big data" ) ; } if ( target . length < targetOffset + length ) { throw new IOException ( "Insufficient target byte array size" ) ; } if ( length < 0 ) { length = 0 ; int curOffset = targetOffset ; InputStream in = asStream ( ) ; try { int readbyte ; while ( ( readbyte = in . read ( ) ) != EOF ) { target [ curOffset ] = ( byte ) readbyte ; curOffset ++ ; length ++ ; } } finally { in . close ( ) ; } } else { System . arraycopy ( asByteArray ( false ) , 0 , target , targetOffset , ( int ) length ) ; } return ( int ) length ; }
Writes data to some existing byte array starting from specific offset