简体   繁体   中英

java, writing data size with DataOutputStream

I have a series of objects which write their data out to a file via DataOutputStream.

As my application file format evolves, I will be adding further data objects to it, which should be able to be read by "older" versions of the application.

To be able to do this, I am needing to preface the objects data with a size value indicating the number of bytes that the current object will take up.

However, as this data can be a variable size, notably when handling strings, I cannot know beforehand what the size will be.

Is there a way to "pre-write" to a byte buffer, as if it were a DataOutputStream ( methods in particular - writeByte, writeShort, writeInt, writeUTF ) which will enable me to get back the byte length of the data before writing it out to the DataOutputStream ? Doing this will enable me to skip over newer data objects that older versions of the application does understand.

Regular Java serialization is not that great for a variety of reasons. One of the most important ones is that it's very brittle, and tends not to be "future proof". If possible, I'd suggest you use a different serialization format, especially if you specifically mention that you plan on adding fields to the class you serialize.

Formats such as Protobuf , and JSON have Java libraries with nice APIs, and good forwards/backwards compatibility features. In most cases, it would be far simpler to Serialize your data into a more convenient format, than solving the problems of the existing one.

As you can see here , you could use java.lang.instrument.Instrumentation in this way:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}

and then calculate the size in bytes of your object:

public static void main(String [] args) {
   Object myObject = ...;
        System.out.println(ObjectSizeFetcher.getObjectSize(myObject));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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