Java 中的流序列化

发布于 2024-11-10 05:03:58 字数 2260 浏览 1 评论 0原文

在 C++ 中,我创建了一个名为 Serializer 的类。我想复制 Java 中的功能。以下是序列化器的工作原理。

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);

当然,这些是您可以执行的其他几个功能(从 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(); }
  • 是否有任何内置类可以做到这一点,并且能够自由地操作字节数据?
  • 如果没有,可以一起使用输入流和输出流吗?
  • 如果不能,如何在InputStream和OutputStream之间进行转换?
  • 还有其他方法可以解决这个问题吗?

旁注:所有字节数据都可以在内存中。内存不是问题。

谢谢。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-11-17 05:03:58

Java 有一个对象序列化系统,听起来不错适合你想做的事。快速总结:

  • 让你的类实现 java.io.Serialized,并添加一个 private static final long serialVersionUID 字段(使用你喜欢的任何值——通常我只是从1)。
  • 将您不想序列化的所有字段标记为瞬态;所有非瞬态字段都将被序列化。
  • 确保要序列化的所有字段都是原始类型或可序列化类型 - 所有不可序列化类型都必须是瞬态的。
  • 每当您对序列化的字段进行更改时,请更改 serialVersionUID。通常我只是将值增加 1。
  • 对于自定义序列化,您可以实现 readObjectwriteObject 方法。
  • 对于严格的自定义序列化,还有java.io.Externalizable

要实际序列化或反序列化对象,请使用 DataOutputStream 和 DataInputStream 类。是的,您可以通过压缩和/或加密来包装流。 :-)

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. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文