Why doesn't ByteBuffer save the index for putDouble and getDouble?

I find it difficult to understand semantics ByteBufferin the following scenario:

int sizeOfDouble = 8;
int numberOfDoubles = 4;
ByteBuffer testBuf = ByteBuffer.allocateDirect(sizeOfDouble*numberOfDoubles);

testBuf.putDouble(0, 1.0);
testBuf.putDouble(1, 2.0);
testBuf.putDouble(2, 3.0);
testBuf.putDouble(3, 4.0);

for (int i = 0; i < numberOfDoubles; ++i) {
    System.out.println("testBuf[" + i + "]: " + testBuf.getDouble(i));
}

I expected to see the values ​​that I just entered in ByteBufferto print on the screen. Instead, I get this output:

testBuf[0]: 4.959404759574682E-4
testBuf[1]: 32.50048828125
testBuf[2]: 32.125
testBuf[3]: 4.0

The value in the third index seems to be as expected: 4.0. But why the values ​​and indices 0, 1 and 2 do not match the values ​​I inserted (1.0, 2.0 and 3.0, respectively)?

I suspect that I misunderstand how it works ByteBuffer, but I could not find it in javadoc.

+4
source share
3 answers

, double s. ; , ; .. , 4.0 .

, sizeOfDouble:

testBuf.putDouble(0*sizeOfDouble, 1.0);
testBuf.putDouble(1*sizeOfDouble, 2.0);
testBuf.putDouble(2*sizeOfDouble, 3.0);
testBuf.putDouble(3*sizeOfDouble, 4.0);

for (int i = 0; i < numberOfDoubles; ++i) {
    System.out.println("testBuf[" + i + "]: " + testBuf.getDouble(sizeOfDouble*i));
}

+5

putDouble() , double. , , , ; , , .

, , Double.BYTES , .

:

int numberOfDoubles = 4;
ByteBuffer testBuf = ByteBuffer.allocateDirect(Double.BYTES*numberOfDoubles);

testBuf.putDouble(0, 1.0);
testBuf.putDouble(Double.BYTES, 2.0);
testBuf.putDouble(Double.BYTES * 2, 3.0);
testBuf.putDouble(Double.BYTES * 3, 4.0);

for (int i = 0; i < numberOfDoubles; ++i) {
    System.out.println("testBuf[" + i + "]: " + testBuf.getDouble(i * Double.BYTES));
}
+4

Since you only work with values double, you can use instead DoubleBuffer:

int numberOfDoubles = 4;
DoubleBuffer testBuf = DoubleBuffer.allocate(numberOfDoubles);

testBuf.put(0, 1.0);
testBuf.put(1, 2.0);
testBuf.put(2, 3.0);
testBuf.put(3, 4.0);

for (int i = 0; i < numberOfDoubles; ++i) {
    System.out.println("testBuf[" + i + "]: " + testBuf.get(i));
}

Or you can wrap ByteBufferwith DoubleBuffer:

int sizeOfDouble = Double.BYTES;
int numberOfDoubles = 4;
ByteBuffer testBuf = ByteBuffer.allocateDirect(sizeOfDouble*numberOfDoubles);
DoubleBuffer dblBuf = testBuf.asDoubleBuffer();

dblBuf.put(0, 1.0);
dblBuf.put(1, 2.0);
dblBuf.put(2, 3.0);
dblBuf.put(3, 4.0);

for (int i = 0; i < numberOfDoubles; ++i) {
    System.out.println("dblBuf[" + i + "]: " + dblBuf.get(i));
}
for (int i = 0; i < testBuf.limit(); ++i) {
    System.out.println("testBuf[" + i + "]: " + testBuf.get(i));
}

Output

dblBuf[0]: 1.0
dblBuf[1]: 2.0
dblBuf[2]: 3.0
dblBuf[3]: 4.0
testBuf[0]: 63
testBuf[1]: -16
testBuf[2]: 0
testBuf[3]: 0
testBuf[4]: 0
testBuf[5]: 0
testBuf[6]: 0
testBuf[7]: 0
testBuf[8]: 64
testBuf[9]: 0
testBuf[10]: 0
testBuf[11]: 0
testBuf[12]: 0
testBuf[13]: 0
testBuf[14]: 0
testBuf[15]: 0
testBuf[16]: 64
testBuf[17]: 8
testBuf[18]: 0
testBuf[19]: 0
testBuf[20]: 0
testBuf[21]: 0
testBuf[22]: 0
testBuf[23]: 0
testBuf[24]: 64
testBuf[25]: 16
testBuf[26]: 0
testBuf[27]: 0
testBuf[28]: 0
testBuf[29]: 0
testBuf[30]: 0
testBuf[31]: 0
+1
source

Source: https://habr.com/ru/post/1694197/


All Articles