javaiomappedbytebuffer

Java Mapped Byte Buffer - garbage values in buffer


I'm experimenting with Mapped Byte Buffer (Java), I have to use DirectBuffer on a file to perform some arithmetic operations:

MappedByteBuffer mbf = new RandomAccessFile("blah.bin", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE,0,100*8L);

Questions: - is the direct buffer is zeroed filled? I have a debug method which dumps the values as

    private void dump(){
    for (int i = 0; i < 100; i++) {
        System.out.println(mbf.getDouble(i));
    }
}

Interestingly, when I take take the dump before writing any values, it's dumping all zeroes (doubles):

When I write to any location say:

mbf.putDouble(13,100.0);

When I re-ran the dump, it's dumping some random values:

0.0
0.0
0.0
0.0
0.0
3.16E-322
8.1387E-320
2.0835183E-317
5.333806864E-315
1.36545455721E-312
3.4955636664571E-310
1.8187371284868433E-307
100.0
5.164499756173817E120
0.0
0.0
0.0
0.0
0.0

My logic is dependent on the zero value

if (mbf.getDouble(6)==0.0)
//then do some thing
else
//do some thing else

How can I ensure the values are properly initialized to zero before writing any condition like above? Does any one got a similar issue? what's the best way to tackle this situation?

Any insights are highly appreciated.

Few more details: OS: Windows 10 JDK: java version "1.8.0_111"

Thanks in advance!


Solution

  • A double value occupies 8 bytes. (In Java always, in some other languages usually but not always.) From the javadoc https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#getDouble-int-

    Reads eight bytes at the given index, composing them into a double value according to the current byte order.

    When you ask for the double starting at byte index 6, it actually uses bytes 6 through 13, and byte 13 contains the first byte of the (nonzero) value you stored.