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.");

}

[2734 byte] By [drawimagea] at [2007-11-27 11:36:09]
# 1

Shouldnt you just be piping one stream to the other?

By the way Drawi Mage are you the brother of Black Mage? just kidding :)

TuringPesta at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the quick response TuringPest; however, I'm not quite sure how to pipe streams. Do you have any examples by chance?

drawimagea at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...
# 3

>> Do you have any examples by chance?

I havent really studied your code but all "piping" amounts to

is putting your writing actions in a list one after another.

You have Objects and you want to write a file:

Object --> File

You are using an ObjectOutputStream

Object --> ObjectOutputStream --> File

The file is being written by a FileOutputStream

Object --> ObjectOutputStream --> FileOutputStream --> File

Now you have to put your Cipher stream somewhere in between.

TuringPesta at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...
# 4

Something like ObjectOutputStream out = new ObjectOutputStream(new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(file)),cipher));

or am I way off base?

drawimagea at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...
# 5

Please notice how big a difference coding in a neat and methodical

way makes.

import java.io.*;

import java.util.*;

import javax.crypto.*;

import javax.crypto.spec.*;

public class Piper{

public static void main(String[] args){

(new Piper()).saveFile("cipherobject.ser");

}

public void saveFile(String filename){

try{

ArrayList sample = new ArrayList();

sample.add("line 1");

sample.add("line 2");

sample.add("line 3");

// create pipes backwards!

// Create FILE STREAM

FileOutputStream fos = new FileOutputStream(filename);

// Create CIPHER STREAM

byte[] byteKey = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};

SecretKey key = new SecretKeySpec(byteKey, "Blowfish/ECB/PKCS5Padding");

Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE,key);

CipherOutputStream cos = new CipherOutputStream(fos, cipher);

// Create OBJECT STREAM

ObjectOutputStream oos = new ObjectOutputStream(cos);

// write data

oos.writeObject(sample);

// close pipes in the reverse order they were created

oos.close();

cos.close();

fos.close();

} catch(Exception e){

e.printStackTrace();

}

}

}

TuringPesta at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...
# 6

Thank you for your help, I have it working now. I agree, coding like your example looks cleaner and is easier to read. Thank you again.

drawimagea at 2007-7-29 17:07:00 > top of Java-index,Java Essentials,Java Programming...