@ Override public byte [ ] bitcoinSerialize ( ) { // we have completely cached byte array. if ( headerBytesValid && transactionBytesValid ) { Preconditions . checkNotNull ( payload , "Bytes should never be null if headerBytesValid && transactionBytesValid" ) ; if ( length == payload . length ) { return payload ; } else { // byte array is offset so copy out the correct range. byte [ ] buf = new byte [ length ] ; System . arraycopy ( payload , offset , buf , 0 , length ) ; return buf ; } } // At least one of the two cacheable components is invalid // so fall back to stream write since we can't be sure of the length. ByteArrayOutputStream stream = new UnsafeByteArrayOutputStream ( length == UNKNOWN_LENGTH ? HEADER_SIZE + guessTransactionsLength ( ) : length ) ; try { writeHeader ( stream ) ; writeTransactions ( stream ) ; } catch ( IOException e ) { // Cannot happen, we are serializing to a memory stream. } return stream . toByteArray ( ) ; }
Special handling to check if we have a valid byte array for both header and transactions