public static byte [ ] loadBytesSafely ( long offset , int length , RandomAccessFile raf ) throws IOException { Preconditions . checkArgument ( length >= 0 ) ; if ( offset < 0 ) return new byte [ 0 ] ; raf . seek ( offset ) ; int readsize = length ; if ( readsize + offset > raf . length ( ) ) { readsize = ( int ) ( raf . length ( ) - offset ) ; } if ( readsize < 0 ) { readsize = 0 ; } byte [ ] bytes = new byte [ readsize ] ; raf . readFully ( bytes ) ; bytes = padBytes ( bytes , length ) ; assert bytes . length == length ; return bytes ; }
Loads the bytes at the offset into a byte array with the given length using the raf . If EOF the byte array is zero padded . If offset < 0 it will return a zero sized array .