So I’ve been trying to figure out how to do this 100% correctly but I can’t find any good info.
Basically, I have a serialization function for a certain object, and that function takes in an OutputStream. This OutputStream may already have stuff written to it, and other things may be written to it after the serialization. Here’s the function:
public void serializeTo(OutputStream output) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(output);
oos.writeObject(this);
//WHAT DO I DO WITH oos NOW?!
oos.flush();
}
My question is: After creating the temporary ObjectOutputStream, I can’t close it once I’m done writing my stuff because that would close the underlying OutputStream. Is the correct thing to do here to only flush() it and then just leave it hanging?
Similarly, what do I do when I want to deserialize?
public static GLSLFile deserializeFrom(InputStream input) throws IOException {
ObjectInputStream ois = new ObjectInputStream(input);
return (GLSLFile) ois.readObject();
//I feel so dirty for not closing ois...
}
Is this correct? Can this explode somehow? What am I supposed to do here?!