A png
is not a Java java.lang.Object
(e.g. a raw byte sequence doesn't encode a length and byte arrays are not dynamically sized in Java they are java.lang.Object
s). Therefore, one solution is to wrap your (ImageIO.write(image1, "png", oos);
[and the second image write] and then the read calls) with a byte[]
. Allow me to illustrate, that is something like -
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image1, "png", baos);
oos.writeObject(baos.toByteArray());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
ImageIO.write(image2, "png", baos2);
oos.writeObject(baos2.toByteArray());
and then update your read (that is) -
image1 = ImageIO.read(new ByteArrayInputStream((byte[])ois.readObject()));
image2 = ImageIO.read(new ByteArrayInputStream((byte[])ois.readObject()));