public int readWithChecksum ( byte [ ] b , int off , int len ) throws IOException { if ( currentOffset == length ) { return - 1 ; } else if ( currentOffset >= dataLength ) { // If the previous read drained off all the data, then just return // the checksum now. Note that checksum validation would have // happened in the earlier read int lenToCopy = ( int ) ( checksumSize - ( currentOffset - dataLength ) ) ; if ( len < lenToCopy ) { lenToCopy = len ; } System . arraycopy ( csum , ( int ) ( currentOffset - dataLength ) , b , off , lenToCopy ) ; currentOffset += lenToCopy ; return lenToCopy ; } int bytesRead = doRead ( b , off , len ) ; if ( currentOffset == dataLength ) { if ( len >= bytesRead + checksumSize ) { System . arraycopy ( csum , 0 , b , off + bytesRead , checksumSize ) ; bytesRead += checksumSize ; currentOffset += checksumSize ; } } return bytesRead ; }
Read bytes from the stream . At EOF checksum is validated and sent back as the last four bytes of the buffer . The caller should handle these bytes appropriately