开发者

Concept of "Serialization"

What are methods to convert data (ints, strings) to bytes in Java? I am looking for methods other than using the Serializable class. I researched and found things like ByteOutputStream.

Can I just parse strings and ints to 开发者_JAVA百科a byte data type?

Any suggestions?


Have a look at DataInputStream and DataOutputStream, they convert all Java data types to bytes and read/write to an underlying Input/OutputStream.

If you need to read or write ints, longs etc.. to a file, then these are the classes for you.

If instead you are just interested in how to convert then to bytes for other purposes, have a look at the source code of those classes, they convert to big-endian.


Classes supporting the DataOutput interface will do what you want. Use DataInput to read the stream back to data.


The standard encoding method used by Java when serializing is, just as your own thoughts, a simple translation of the fields into a byte stream.

Primitives as well as non-transient, non-static referenced objects are encoded into the stream. Each object that is referenced by the serialized object and must also be serialized.

Other languages, such as PHP for example, serializes to a pretty much human readable format and some implementations serialize to JSON or XML.

In my own mind though, true serialization should be binary byte-per-byte representation of the data. That way it's possible to quickly read all the data up into memory again and it can be executed as is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜