Question About Streams
I have a program that creates a file using ObjectOutputStream. It writes several ArrayList<Object> items to the file. I then encrypt this file using a CipherOutputStream; however, the only way I could think to do that was create a temporary file with the ObjectOutputStream then read the contents of that file into the CipherOutputStream to get my final file because I couldn't do a writeObject() in the CipherOutputStream. Is there a way that I can write the contents of the temporary file into a Stream of some type and then encrypt that stream with the CipherOutputStream to write the file? I realize what I'm trying to say may be a bit confusing, here is the code I'm using, hopefully it can help clarify.privatevoid createFile(String fileName)throws FileNotFoundException,
IOException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException{
SecretKey key =new SecretKeySpec(byteKey,
"Blowfish/ECB/PKCS5Padding");
finalbyte[] BUFFER =newbyte[1024];
String finalName = fileName;
fileName = fileName +"_temp";
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
CipherOutputStream cout;
ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream(fileName));
for(CategoryPanel panel : panels){
out.writeObject(panel.getState());
}
out.close();
BufferedInputStream in =new BufferedInputStream(new FileInputStream(fileName));
cout =new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(finalName)), cipher);
int i;
while((i = in.read(BUFFER)) != -1){
cout.write(BUFFER,0,i);
}
in.close();
cout.close();
new File(fileName).delete();
System.out.println("File was successfully saved.");
}

