开发者

Serialization with Streams in Java

In C++, I've created a class called Serializer. And I want to duplicate the functionality in Java. Here's how Serializer works.

开发者_StackOverflow中文版
Serializer s;
s.writeString(myString); // automatically converted to Big-Endian unicode string with length prepended.
s.writeInt(myInt); // automatically converted to Big-Endian integer (swizzled if necessary)
s.writeObject(myObject); // which implements the Serializable interface
    {
        s.writeInt(myObjectId); // internally, it writes the data that I want to serialize.
        ...
    }

ZlibCompressor.compress(s); // 's' is compressed
    {
        bytes = s.getBytes(); // internally, it gets bytes, ...
        compressFunc(bytes); // compress them
        s.overwrite(bytes); // and overwrites
    }

AESCipher.encrypt(s); // similarly, 's' is encrypted

// and the reverse in the same file, using the same all-in-one class to manipulate byte data.

AESCipher.decrypt(s);
ZlibCompressor.decompress(s);

s.readString(myString);
s.readInt(myInt);
s.readObject(myObject);

And of course, these are the other few functions you can do (copied and pasted from C++):

ByteArray Split(const Uint32& start, const Uint32& size);

inline Uint32 GetSize() const                                       { return mBytes.size(); }
inline const Uint32& GetPos() const                                 { return mPos; }
inline Bool IsEmpty() const                                         { return mBytes.empty(); }
inline const Bool& IsError() const                                  { return mError; }

inline void Resize(const Uint32& size, const Byte& val = 0)         { mBytes.resize(size, val); }
inline void SetPos(const Uint32& pos) const                         { mPos = pos; }
inline void Reset() const                                           { mPos = 0; mError = false; }
inline void Clear()                                                 { mBytes.clear(); Reset(); }
inline void Push(const Byte& x)                                     { mBytes.push_back(x); }
inline void Pop()                                                   { mBytes.pop_back(); }
  • Is there any in-built class that does that, freely able to manipulate byte data?
  • If there isn't, can you use the Input and Output stream together?
  • If you can't, how do you convert between InputStream and OutputStream?
  • Is there any other approach to solving this problem?

Side Note: All the byte data can be in memory. Memory is not an issue.

Thank you.


Java has an object serialisation system, which sounds like a good fit for what you want to do. Quick summary:

  • Have your class implement java.io.Serializable, and add a private static final long serialVersionUID field (use any value you like---usually I just start with 1).
  • Mark all fields you don't want to serialise as transient; all non-transient fields will be serialised.
  • Ensure that all fields you want to serialise are either primitive or are also serialisable types---all non-serialisable types must be transient.
  • Change the serialVersionUID whenever you make changes to the fields that get serialised. Usually I just bump the value by 1.
  • For custom serialisation, you can implement the readObject and writeObject methods.
  • For seriously custom serialisation, there's also java.io.Externalizable.

To actually serialise or deserialise your objects, use the DataOutputStream and DataInputStream classes. And yes, you can wrap the streams with compression and/or encryption. :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜