protected void _writeLastChunk(byte[] buffer,long index) throws FileEncryptionException, RandomDataGenerationException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
  byte[] iv=generateRandomChunkIV();
  IvParameterSpec spec=new IvParameterSpec(iv);
  lastChunkCipher.init(Cipher.ENCRYPT_MODE,getFileKey(),spec);
  byte[] encChunk=lastChunkCipher.doFinal(buffer);
  if ((encChunk == null) || (encChunk.length != (buffer.length + CHUNK_TLEN))) {
    throw new FileEncryptionException("Encrypted chunk length mismatch!");
  }
  long oldpos=backingRandomAccessFile.getFilePointer();
  backingRandomAccessFile.seek(chunkOffset(index));
  backingRandomAccessFile.write(iv);
  backingRandomAccessFile.write(encChunk);
  backingRandomAccessFile.seek(oldpos);
}

protected void _writeLastChunk ( byte [ ] buffer , long index ) throws FileEncryptionException , RandomDataGenerationException , InvalidKeyException , InvalidAlgorithmParameterException , IllegalBlockSizeException , BadPaddingException , IOException { byte [ ] iv = generateRandomChunkIV ( ) ; IvParameterSpec spec = new IvParameterSpec ( iv ) ; lastChunkCipher . init ( Cipher . ENCRYPT_MODE , getFileKey ( ) , spec ) ; byte [ ] encChunk = lastChunkCipher . doFinal ( buffer ) ; if ( ( encChunk == null ) || ( encChunk . length != ( buffer . length + CHUNK_TLEN ) ) ) { throw new FileEncryptionException ( "Encrypted chunk length mismatch!" ) ; } long oldpos = backingRandomAccessFile . getFilePointer ( ) ; backingRandomAccessFile . seek ( chunkOffset ( index ) ) ; backingRandomAccessFile . write ( iv ) ; backingRandomAccessFile . write ( encChunk ) ; backingRandomAccessFile . seek ( oldpos ) ; }
helper method for encrypting and writing the last chunk of the file
