question: protected byte [ ] _readLastChunk ( long index ) throws IOException , InvalidKeyException , InvalidAlgorithmParameterException , IllegalBlockSizeException , BadPaddingException , FileEncryptionException , ShortBufferException { long nRemaining = backingRandomAccessFile . length ( ) - ( chunkOffset ( index ) ) ; if ( nRemaining > CHUNK_ENC_SIZE ) { throw new FileEncryptionException ( "Calculated size of size of last chunk bigger than default chunk size!" ) ; } else if ( nRemaining <= CHUNK_IV_SIZE ) { return new byte [ ] { } ; } long oldpos = backingRandomAccessFile . getFilePointer ( ) ; backingRandomAccessFile . seek ( chunkOffset ( index ) ) ; byte [ ] iv = new byte [ CHUNK_IV_SIZE ] ; int ret = backingRandomAccessFile . read ( iv ) ; if ( ret != CHUNK_IV_SIZE ) { throw new FileEncryptionException ( "Size mismatch reading chunk IV!" ) ; } IvParameterSpec spec = new IvParameterSpec ( iv ) ; lastChunkCipher . init ( Cipher . DECRYPT_MODE , getFileKey ( ) , spec ) ; nRemaining -= CHUNK_IV_SIZE ; byte [ ] buf = new byte [ ( int ) nRemaining ] ; byte [ ] res ; ret = backingRandomAccessFile . read ( buf ) ; backingRandomAccessFile . seek ( oldpos ) ; if ( ret != nRemaining ) { throw new FileEncryptionException ( "Size mismatch reading encrypted chunk data!" ) ; } res = lastChunkCipher . doFinal ( buf ) ; if ( ( res == null ) || ( res . length != ( nRemaining - CHUNK_TLEN ) ) ) { throw new FileEncryptionException ( "Decryption error or chunk size mismatch during decryption!" ) ; } else { return res ; } }
target:  Helper method for reading and decrypting the last chunk of the file