4

I have a method where I am receiving input stream, I need to decrypt that data first then serialize that data. But my data is not getting serialize. My file is a hash file. Please help me. My code is -

private byte[] getSerializeEncryptedBytes(InputStream inputStream,String password)
               throws Exception {
byte[] fileBytes = getByteArrayFromInputStream(inputStream);
fileBytes = AESEncrytion.getDecryptedData(fileBytes, password);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytes);
ObjectInputStream objectInputStream = objectInputStream = new 
               ObjectInputStream(byteArrayInputStream);
Object dataObject = objectInputStream.readObject();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(fileBytes);
out.flush();
byte[] serializeByte = bos.toByteArray();
out.close();
Util.writeFileNewWay(new File("soapSerialized.txt"), serializeByte);

This is my second method -

public byte[] getByteArrayFromInputStream(InputStream inputStream) throws 
              IOException {
 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 int nRead;
 byte[] data = new byte[16384];
 while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
 }
 buffer.flush();
 return buffer.toByteArray();

}

Can I serialize byte array ?

7
  • Why do you say your data isn't serialized? What does this code do? Commented Feb 20, 2014 at 14:13
  • I am receiving a file in input stream, it is encrypted. I need to decrypt it and then serialize its data and write in a file. @kevin Commented Feb 20, 2014 at 14:18
  • of course u can serialize byte[] as JVM creates object of that byte array , and objects can be serialize. Commented Feb 20, 2014 at 14:18
  • Why are you reading are you reading and writing to Object streams in this case? Which object is involved because it doesn't appear you have one. It appears you have a byte[] containing text. Commented Feb 20, 2014 at 14:20
  • Also why not decrypt and copy the data as you read it... Commented Feb 20, 2014 at 14:21

1 Answer 1

2

The answer to your question:

Can I serialize byte array ?

Actually is "you don't need to do so". Your code apparently writes bytes to disk, and then reads bytes from disk; everything transparent, you are doing pretty nice.

Does your code produce any error?

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting following exception -java.io.StreamCorruptedException: invalid stream header: 0D0A2020 at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
I am sorry, I hadn't noticed you were using ObjectInputStream. I suggest you drop that and try using only FileInputStream since you only need to write, read and handle byte arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.