Prev: How to check if object is an instanceof a class, but not a sub-class?
Next: Broadcasting in Java but NOT MulticastSocket
From: Boris Punk on 23 Jun 2010 22:20 Is there anyway to do this? Serialization looks cumbersome and I want to dump the data of an object onto disk. eg. class AClass{ AClass(){ } } how do you get a representation of this in byte form? It can't be that difficult to grab from memory, dump to disk, then retrieve back into memory again? Thanks
From: Joshua Cranmer on 23 Jun 2010 22:56 On 06/23/2010 10:20 PM, Boris Punk wrote: > Is there anyway to do this? Serialization looks cumbersome and I want to > dump the data of an object onto disk. Use serialization. If you are using mostly POD-ish classes (to steal a C++ term) and avoid native data, the defaults for serialization should work pretty well. > > eg. > > class AClass{ > AClass(){ > } > } > > how do you get a representation of this in byte form? It can't be that > difficult to grab from memory, dump to disk, then retrieve back into memory > again? If you want to pull it back into memory without using serialization, you would have to create your own serialization-esque approach if you wanted it to persist across sessions, and probably would need to even if it is in the same JVM instance. Once you get to the actual memory of the object (which is not trivial, and would involve at best fragile pointer magic in native code and at worst would be an ugly kludge which never works reliably for anything beyond the most trivial setups), you would have to figure out what all the objects were referring to and find some way to serialize that, or find out how to patch that data in when pulling back into memory. Or, you could take the easy route and use a framework which does all of that for you written by people who have a much better idea of what needs to be done. It's called "serialization." -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: Mike Schilling on 23 Jun 2010 23:21 "Boris Punk" <khgfhf(a)hmjggg.com> wrote in message news:vhzUn.41045$_F1.14580(a)hurricane... > Is there anyway to do this? Serialization looks cumbersome and I want to > dump the data of an object onto disk. > > eg. > > class AClass{ > AClass(){ > } > } > > how do you get a representation of this in byte form? It can't be that > difficult to grab from memory, dump to disk, then retrieve back into > memory again? Java won't play those games. For one thing, there's no defined order of fields within an object.
From: Owen Jacobson on 23 Jun 2010 23:21 On 2010-06-23 22:20:05 -0400, Boris Punk said: > Is there anyway to do this? Serialization looks cumbersome and I want > to dump the data of an object onto disk. > > eg. > > class AClass{ > AClass(){ > } > } > > how do you get a representation of this in byte form? It can't be that > difficult to grab from memory, dump to disk, then retrieve back into > memory again? > > Thanks This is one of the major differences between Java and its most obvious ancestors: Java does not specify nor expose anything about the internal representations of objects to programs. This has two advantages: the JVM is free to reorganize objects much more freely than you might expect, and it's not possible for a programmer to introduce "invalid" objects, accidentally or otherwise. You can *define* a byte-oriented or text-oriented representation for your objects, and that's exactly what the built-in serialization protocol does: what comes out is not the JVM's in-memory representation; instead, ObjectOutputStream builds up a description according to published and documented protocol rules. The resulting byte stream is fairly resilient to changes in the runtime environment: a serialized blob from Java 1.0 is theoretically still deserializable into an object under Java 1.6 (the current version), more than ten years later, assuming you have a compatible class definition around to deserialize it with. You're not limited to using the built-in (and rather opaque) byte-oriented format, either: there are a lot of easy-to-use object marshalling libraries for a lot of different formats out there. XStream and JAXB can marshal and unmarshal objects to and from XML with very little code investment, and libraries like gson can do the same with JSON, giving you some good options for human-readable and human-debuggable representations. Libraries like Google's protocol buffers give you some more flexibility in designing your own byte-oriented representations of your objects, if you prefer. Hope that helps give you some idea as to where to start looking, -o
From: Arne Vajhøj on 24 Jun 2010 19:38
On 23-06-2010 22:20, Boris Punk wrote: > Is there anyway to do this? Serialization looks cumbersome and I want to > dump the data of an object onto disk. > > eg. > > class AClass{ > AClass(){ > } > } > > how do you get a representation of this in byte form? It can't be that > difficult to grab from memory, dump to disk, then retrieve back into memory > again? You can use Java standard serialization, which will store the data plus some meta data. Or you can write your own custom serialization. If you are interested in the last then I have a little library that allows this syntax: import dk.vajhoej.record.FieldType; import dk.vajhoej.record.Struct; import dk.vajhoej.record.StructField; @Struct public class Data { @StructField(n=0,type=FieldType.INT4) private int iv; @StructField(n=1,type=FieldType.FP8) private double xv; @StructField(n=2,type=FieldType.FIXSTR,length=8,encoding="ISO-8859-1") private String sv; public int getIv() { return iv; } public void setIv(int iv) { this.iv = iv; } public double getXv() { return xv; } public void setXv(double xv) { this.xv = xv; } public String getSv() { return sv; } public void setSv(String sv) { this.sv = sv; } } Arne |