RuedigerMoeller / fast-serialization

FST: fast java serialization drop in-replacement
Apache License 2.0
1.59k stars 247 forks source link

BufferedImage problem: setup on StackOverFlow #212

Open saulrussell opened 7 years ago

saulrussell commented 7 years ago

Hi Ruediger, I've posted this one on SO. [https://stackoverflow.com/questions/45806408/serialization-issue-de-ruedigermoeller] This would be a repeat. Any help appreciated;

Trying to get your excellent framework as drop in replacement for my JVM serializer. As a first step.

Here is my serializer:

static FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();

public Serialiser() { conf.registerClass(MapSerializableForm.class, // etc// ... }

public byte[] serialise(Object obj) { byte[] bytes = null; try { final ByteArrayOutputStream b = new ByteArrayOutputStream(); final FSTObjectOutput o = new FSTObjectOutput(b); o.writeObject(obj); bytes = b.toByteArray(); b.close(); } catch (IOException e) { e.printStackTrace(); } return bytes; }

public Object deserialize(byte[] bytes) { Object deser = null; try { final ByteArrayInputStream b = new ByteArrayInputStream(bytes); final FSTObjectInput o = new FSTObjectInput(b); deser = o.readObject(); b.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return deser; } The class that is causing me problems is this one: I'm wrapping JavaFX images with Swing images to help me serialise them. My code does work with the standard Java serializer, but is v. slow. I'm hopeful for improvements, if I can get it working. The result is a nullpointerException during deserialization. It's late, here, but I've tried to be clear.

import javafx.embed.swing.SwingFXUtils; import javafx.scene.image.Image; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*;

public class SerializableImage implements Serializable {

private static final long serialVersionUID = 7984341875952256208L; public transient Image image ;

public SerializableImage(Image image) { this.image=image; }

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); BufferedImage bufferedImage = ImageIO.read(s); //image comes back null ! image = SwingFXUtils.toFXImage(bufferedImage, null); }

private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", s); }

public double getWidth() { return image.getWidth(); } In the readObject method, (inside the stream "s") I'm able to see an incoming FSTObjectInput with a wrappedStack of one object: this as hoped contains a SerializableImage in the "toRead" field. However, I'm uncertain how to get this out and into my BufferedImage:

BufferedImage bufferedImage = ImageIO.read(s); Hence, currently, the image comes back null. I tried adding an FSTInput, but couldn't get it to work. Thanks for any help.

RuedigerMoeller commented 7 years ago

the serializer implementation does not make sense to me (check out internal implementations of fst for examples). Will have a deeper look as I find time.