You write out serializable object(s), strings are serializable, first using the ObjectOutputStream
to your storage and read the object(s) in using the ObjectInputStream
.
You don't have any object in your byte array storage. That's the reason you are getting the stream corrupted exception.
Following is a very basic snippet to demo -
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
String myStrObj = "Hello World";
oos.writeObject(myStrObj); //write the string as object
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
String readStrObj = (String) ois.readObject(); //read the string as object
ois.close();
System.out.println(readStrObj);
You can replace that Hello World
with your public key and see what it prints out.