I have a to read a file, zip it, encrypt it with AES algorithm and send it to an S3 bucket.
The workflow of my actual code is :
- Read file and write it as a zip file (
Files.copy(filePath, zipOutputStream)fromjava.nio.file - Read the zip file, encrypt and rewrite it
"Pseudocode" for 1. and 2.
fileList.foreach(localFile => {
zipOutputStream.putNextEntry(new ZipEntry(localFile.toPath.getFileName.toString))
file.Files.copy(localFile.toPath, zipOutputStream)
encryptAndReplace(localFile, someEncryptionConfig)
})
- Read the encrypted zip and send it to s3 (
FileInputStreamtoS3OutputStreamWrapper)
I want to do all those steps without rewriting the file 2 times and avoid IO to save time.
How can i process to optimize my workflow ?
If i return an InputStream in my step 1. instead of rewriting it. And then re-return an InputStream in my step 2. and then send it to S3, does is means that my 2 methods will return the whole file ? And that my whole file will be stored in memory ? What should i take care of if I process in this way ?
You don't need to read and write the file for encryption. Using
CipherOutputStreamyou can do everything in one go.I think you can even replace
fileOutwith theS3OutputStreamWrapperand do everything in one go.