英文:
Can't convert FloatBuffer to float[] (UnsupportedOperationException)
问题
我实际上正在编写一个立方体网格的代码,其中在超级方法中我需要传递立方体顶点,这是一个 float[] 数组,但我想以一种“人性化的方式”来做到这一点,所以我做了这样:
```java
private static FloatBuffer vertices = MemoryUtil.memAllocFloat(72);
private static final float[] A={0,0,0},B={0,1,0},C={1,1,0},D={1,0,0},E={0,0,0},F={0,1,0},G={1,1,0},H={1,0,0}; //顶点
public class MyClass extends MyMeshClass
{
public MyClass()
{
super(
vertices
//前面
.put(A).put(B).put(C).put(D)
//后面
.put(E).put(F).put(G).put(H)
//右面
.put(D).put(C).put(G).put(H)
//左面
.put(A).put(B).put(F).put(E)
//顶面
.put(B).put(F).put(G).put(C)
//底面
.put(A).put(E).put(H).put(D).array(),
在这里做其他很酷的事情);
}
}
但是当转换为数组时,我遇到了 UnsupportedOperationException。所以我尝试了另一种方法,将 FloatBuffer 转换为数组,就像这样:
public static float[] toArray(FloatBuffer buffer)
{
float[] array = new float[buffer.limit()];
buffer.get(array);
return array;
}
但是在执行 get(array)
时我得到了 BufferUnderflowException。
有人知道为什么它不起作用吗?
<details>
<summary>英文:</summary>
I'm actually coding a cube mesh where in the super method I need to pass cube vertices, which is a float[], but I want to do it in a "human friendly way" so I've done this :
```java
private static FloatBuffer vertices = MemoryUtil.memAllocFloat(72);
private static final float[] A={0,0,0},B={0,1,0},C={1,1,0},D={1,0,0},E={0,0,0},F={0,1,0},G={1,1,0},H={1,0,0}; //Vertices
public class MyClass extends MyMeshClass
{
public MyClass()
{
super(
vertices
//Front
.put(A).put(B).put(C).put(D)
//Back
.put(E).put(F).put(G).put(H)
//Right
.put(D).put(C).put(G).put(H)
//Left
.put(A).put(B).put(F).put(E)
//Top
.put(B).put(F).put(G).put(C)
//Bottom
.put(A).put(E).put(H).put(D).array(),
Doing other cool stuff here);
}
}
But I end up with a UnsupportedOperationException when converting to array, So I've tried differently by transforming FloatBuffer into array like this
public static float[] toArray(FloatBuffer buffer)
{
float[] array = new float[buffer.limit()];
buffer.get(array);
return array;
}
But I get a BufferUnderflowException on the get(array).
Does anyone know why it is not working ?
答案1
得分: 1
根据Buffer.array()
的Javadoc文档,如果缓冲区不由可访问的数组支持,将会抛出UnsupportedOperationException
异常 - https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html#array--
这种情况会发生在缓冲区利用在JVM堆之外分配的数组(例如本地内存)的情况下。由于LWJGL的MemoryUtil.memAllocFloat
被优化用于OpenGL,我猜它使用了本地内存,因此对这种缓冲区调用array()
方法会失败。
如果你想要在堆中分配缓冲区,你可以简单地使用,例如:
FloatBuffer vertices = FloatBuffer.allocate(72);
或者
FloatBuffer vertices = FloatBuffer.wrap(new float[72]);
英文:
According to javadoc for Buffer.array()
UnsupportedOperationException
will be thrown if buffer is not backed by accessible array - https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html#array--
Such situation will happen i.e. when buffer utilizes array allocated not in JVM heap, but in native memory. Since LWJGL's MemoryUtil.memAllocFloat
is optimized for opengl I guess it utilizes native memory, hence array()
method invoked on such buffer fails.
If you want allocate buffer in heap you can just use i.e.
FloatBuffer vertices = FloatBuffer.allocate(72);
or
FloatBuffer vertices = FloatBuffer.wrap(new float[72]);
``
</details>
# 答案2
**得分**: 0
你是这样尝试的吗?
```java
FloatBuffer buf = FloatBuffer.allocate(10);
buf.put(1f).put(2f).put(3f).put(new float[]{4,5,6});
System.out.println(Arrays.toString(buf.array()));
输出
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0, 0.0, 0.0, 0.0]
英文:
Did you do try it like this?
FloatBuffer buf = FloatBuffer.allocate(10);
buf.put(1f).put(2f).put(3f).put(new float[]{4,5,6});
System.out.println(Arrays.toString(buf.array()));
Prints
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0, 0.0, 0.0, 0.0]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论