ROCKY
ROCKY

Reputation: 73

Can I serialize byte array in java?

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 ?

Upvotes: 4

Views: 9804

Answers (1)

Jorge_B
Jorge_B

Reputation: 9872

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?

Upvotes: 2

Related Questions