views:

141

answers:

3

I'm developing a kind of an exchange format between instances of an application so that user could save information to file and restore it later. The whole 'exchange format' is a single class that gets serialized and written to disk.

How would you suggest to store graphical/sound information inside that class? I'd like to avoid just putting the files into it a .jar.

A: 

** me **

how about more details on your case? the "best" method usually depends on the particular application/use. does the image/sound come from files? from a stream? Is each instance of the class expected to store separate images? Or can an image be shared between different instances?

gsmd

images come from files, sounds come from a stream; there's actually a Set of items some of which may have an image attached; an image can't be shared


What methods have you tried? I guess using the Serializable interface is the way to go. Here's two articles on the topic.

Basically,

  1. implement the Serializable interface in your class
  2. mark members which shouldn't be saved in the file as transient (members which contain contextual data like file handles, socket connections ,etc).
  3. you have to customize how your image and sound classes write data using, but implementing:

    private void writeObject(ObjectOutputStream out) throws IOException;

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

moogs
images come from files, sounds come from a stream; there's actually a Set of items some of which may have an image attached; an image can't be shared.
alex
A: 

Store the data exactly anyhow you like inside the class but implement custom serialization for the data. See Serializable.

divideandconquer.se
+2  A: 

You might keep your resources stored in the class as byte[] arrays. Using ByteArrayInputStream and ByteArrayOutputStream you are able to wrap the arrays as streams and use them to store and retrieve resources.

jassuncao