Search code examples
javadatainputstreamdataoutputstreambytearrayoutputstreambytearrayinputstream

Serializing and Deserializing data with Double value in Java


I am using the function toByteBuffer() to serialize some data. Please note that I am using DataOutputStream to write the serialized data.

public ByteBuffer toByteBuffer() throws IOException {
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    DataOutputStream ds = new DataOutputStream(bs);

    int klistSize = 128;
    ds.writeInt(klistSize);             /* writes KListSize*/

    for (int klistIndex = 0; klistIndex < klistSize; klistIndex++) {

        Double freq = klistIndex * 12.2;
        ds.writeDouble(freq);

        ds.writeInt(klistIndex);

        for (int i = 0; i < klistIndex; i++) {

            Double l1 = (double) (System.currentTimeMillis() / 1000);
            Double l2 = (double) (System.currentTimeMillis() / 1000);

            ds.writeDouble(l1);
            ds.writeDouble(l2);
        }
    }

    ds.flush();

    return ByteBuffer.wrap(bs.toString().getBytes(charset));
}

Then later in my project, I am using the function fromByteBuffer(ByteBuffer byteBuffer) to deserialize the data. Please note that I am using DataInputStream to read the serialized data.

public void fromByteBuffer(ByteBuffer byteBuffer) throws IOException {

    DataInputStream ds = new DataInputStream(new ByteArrayInputStream(charset.decode(byteBuffer).toString().getBytes()));

    int klistSize = ds.readInt();

    for (int i = 0; i < klistSize; i++) {

        Double freq = ds.readDouble();
        System.out.print("freq : " + freq + ", ");

        int entryCount = ds.readInt();
        System.out.print("count : " + entryCount + " ");

        for(int j = 0; j< entryCount; j++)
        {
            double[] f1 = new double[2];
            f1[0] = ds.readDouble();
            f1[1] = ds.readDouble();
        }
    }
}

But for some reason I am getting the following error.

    freq : -0.11328125, count : 0 freq : 3.485250766913E-310, count : 1717960704 Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at ucr.edu.Main.fromByteBuffer(Main.java:67)
at ucr.edu.Main.main(Main.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The above codes are simplified versions of the code that I am using for my project but they still give the same error.

What is the reason for this exception and how can I fix this ?


Solution

  • For some reason you are converting your ByteBuffer to String and back. That's typically a bad idea.

    In your toByteBuffer code, rather write something like:

    ByteBuffer.wrap(bs.toByteArray());
    

    And in your fromByteBuffer method, something like:

    // to read the byteBuffer from the beginning, you might want to rewind
    // before copying it to an array:
    // byteBuffer.rewind();
    byte[] array = new byte[byteBuffer.remaining()];
    byteBuffer.get(array);
    DataInputStream ds = new DataInputStream(new ByteArrayInputStream(array));
    

    Good luck.