简体   繁体   English

将BitSet写入输出文件没有开销?

[英]Writing BitSet to output file without overhead?

I get a line of overhead ( "java.util.BitSet" ) when writing a BitSet to an output file using ObjectOutputStream.writeObject() . 使用ObjectOutputStream.writeObject()将BitSet写入输出文件时,我获得了一线开销( "java.util.BitSet" ObjectOutputStream.writeObject()

Anyway around it? 不管怎么说呢?

That is not an "overhead", that't the marker that lets Java figure out what type it needs to create when deserializing the object from that file. 这不是一个“开销”,它不是让Java知道在从该文件反序列化对象时需要创建什么类型的标记。

Since ObjectInputStream has no idea what you have serialized into a file, and has no way for you to provide a "hint", ObjectOutputStream must "embed" something for the input stream to be able to decide what class needs to be instantiated. 由于ObjectInputStream不知道您将哪些序列化到文件中,并且无法提供“提示”,因此ObjectOutputStream必须“嵌入”输入流的某些内容才能决定需要实例化哪个类。 That is why it places the "java.util.BitSet" string in front of the data of your BitSet . 这就是为什么它将"java.util.BitSet"字符串放在BitSet数据的前面。

You cannot get around writing this marker when you use serialization capabilities built into BitSet class. 使用BitSet类中内置的序列化功能时,无法绕过编写此标记。 If you are serializing the object into a file by itself, with no other objects going in with it, you could write the result of toByteArray() call into a file, and call BitSet.valueOf(byteArray) after reading byteArray from the file. 如果您将对象自行序列化为文件,而没有其他对象,则可以将toByteArray()调用的结果写入文件,并在从文件中读取byteArray后调用BitSet.valueOf(byteArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM