在视图缓冲区上添加浮点数组不会增加位置是否正常?
Is it normal that adding a float array on a view buffer does not increase position?
如标题,此代码:
ByteBuffer vertexBuffer = GLBuffers.newDirectByteBuffer(3*Float.BYTES+3*Byte.BYTES);
System.out.println(vertexBuffer.toString());
vertexBuffer.asFloatBuffer().put(new float[]{1,2,3});
System.out.println(vertexBuffer.toString());
vertexBuffer.put(new byte[]{0,1,2});
System.out.println(vertexBuffer.toString());
打印出以下内容:
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=3 lim=15 cap=15]
但是,理论上,我希望我的视图缓冲区中的第一个 put
将位置从 0 增加到 3,第二个 put
从 3 增加到 6,以便得到这样的东西:
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=3 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=6 lim=15 cap=15]
api 文档说:
public final FloatBuffer put(float[] src) Relative bulk put
method (optional operation). This method transfers the entire content
of the given source float array into this buffer. An invocation of
this method of the form dst.put(a) behaves in exactly the same way as
the invocation
dst.put(a, 0, a.length)
如果我们去看它提到的调用
public FloatBuffer put(float[] src, int offset, int length) Relative
bulk put method (optional operation). This method transfers floats
into this buffer from the given source array. If there are more floats
to be copied from the array than remain in this buffer, that is, if
length > remaining(), then no floats are transferred and a
BufferOverflowException is thrown. Otherwise, this method copies
length floats from the given array into this buffer, starting at the
given offset in the array and at the current position of this buffer.
The position of this buffer is then incremented by length. In other
words, an invocation of this method of the form dst.put(src, off, len)
has exactly the same effect as the loop
for (int i = off; i < off + len; i++)
dst.put(a[i]);
它明确表示位置将增加..
这是正常现象还是我遗漏了什么?
编辑:与以下行为相同:
ByteBuffer vertexBuffer = ByteBuffer.allocate(3*Float.BYTES+3*Byte.BYTES);
是的,没关系,因为实际上 position
在另一个对象中增加了,这是调用 vertexBuffer.asFloatBuffer()
.
时得到的引用
换句话说,如果您将代码更改为:
FloatBuffer floatBuffer = vertexBuffer.asFloatBuffer();
floatBuffer.put(new float[]{1, 2, 3});
System.out.println(floatBuffer.toString());
您将获得:
java.nio.ByteBufferAsFloatBufferB[pos=3 lim=3 cap=3]
如标题,此代码:
ByteBuffer vertexBuffer = GLBuffers.newDirectByteBuffer(3*Float.BYTES+3*Byte.BYTES);
System.out.println(vertexBuffer.toString());
vertexBuffer.asFloatBuffer().put(new float[]{1,2,3});
System.out.println(vertexBuffer.toString());
vertexBuffer.put(new byte[]{0,1,2});
System.out.println(vertexBuffer.toString());
打印出以下内容:
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=3 lim=15 cap=15]
但是,理论上,我希望我的视图缓冲区中的第一个 put
将位置从 0 增加到 3,第二个 put
从 3 增加到 6,以便得到这样的东西:
java.nio.DirectByteBuffer[pos=0 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=3 lim=15 cap=15]
java.nio.DirectByteBuffer[pos=6 lim=15 cap=15]
api 文档说:
public final FloatBuffer put(float[] src) Relative bulk put method (optional operation). This method transfers the entire content of the given source float array into this buffer. An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation dst.put(a, 0, a.length)
如果我们去看它提到的调用
public FloatBuffer put(float[] src, int offset, int length) Relative bulk put method (optional operation). This method transfers floats into this buffer from the given source array. If there are more floats to be copied from the array than remain in this buffer, that is, if length > remaining(), then no floats are transferred and a BufferOverflowException is thrown. Otherwise, this method copies length floats from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length. In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop for (int i = off; i < off + len; i++) dst.put(a[i]);
它明确表示位置将增加..
这是正常现象还是我遗漏了什么?
编辑:与以下行为相同:
ByteBuffer vertexBuffer = ByteBuffer.allocate(3*Float.BYTES+3*Byte.BYTES);
是的,没关系,因为实际上 position
在另一个对象中增加了,这是调用 vertexBuffer.asFloatBuffer()
.
换句话说,如果您将代码更改为:
FloatBuffer floatBuffer = vertexBuffer.asFloatBuffer();
floatBuffer.put(new float[]{1, 2, 3});
System.out.println(floatBuffer.toString());
您将获得:
java.nio.ByteBufferAsFloatBufferB[pos=3 lim=3 cap=3]