JAVA BufferOverflowException,同时保持缓冲区容量?

JAVA BufferOverflowException,同时保持缓冲区容量?,java,buffer-overflow,bytebuffer,Java,Buffer Overflow,Bytebuffer,因此,我有一个方法可以从ByteBuffer创建IntBuffer: public static IntBuffer directIntBuffer(int[]buffer){ ByteBuffer bb = ByteBuffer.allocateDirect(4*buffer.length); bb.order(ByteOrder.nativeOrder()); IntBuffer ib = bb.asIntBuffer(); i

因此,我有一个方法可以从ByteBuffer创建IntBuffer:

public static IntBuffer directIntBuffer(int[]buffer){
        ByteBuffer bb = ByteBuffer.allocateDirect(4*buffer.length);
        bb.order(ByteOrder.nativeOrder());
        IntBuffer ib = bb.asIntBuffer();
        ib.put(buffer).flip();
        return ib;
    }
在另一个类的构造函数中,我正在初始化其中一个IntBuffers,并用嵌套的for循环填充它:

编辑:所以这是我的完整构造函数。我用实际的顶点坐标填充FloatBuffer FBVertices顶点,用索引填充IntBuffer IBVerticesIndex。稍后,我使用GL11.gldrawerelements(GL11.GL_三角形,IBVerticesIndex)绘制这些元素

publicsphereHelper(浮动框架、内部岛、内部岛)
{  
FBVertices=GLDrawHelper.directFloatBuffer(新浮点[iSlices*iStacks*4*3]);
FBVertices.clear();
IBVerticesIndex=GLDrawHelper.directIntBuffer(新的int[iSlices*iStacks*4]);
IBVerticesIndex.clear();
int指数=0;
float verticalDegreePerStack=(float)Math.PI/iStacks;//1*PI,因为我们只需要一次球体的高度(从-PI/2到PI/2)
float horizontalDegreePerSlice=2.0f*(float)Math.PI/iSlices;//2*PI,因为单位圆/极坐标绕球体一周
对于(int k=0;k对于(int j=0;j编辑:+1到Arjan对原始问题的评论。我将Don的代码复制到我的机器上,并且它在Java 8上工作,也获得400/400作为输出。您是否在注释代码的其他地方设置了
限制

你读过这个函数是如何工作的吗?特别是,这个类有一个
limit
属性,它没有一个完整的初始值。如果超过
limit
值,就会抛出一个
BufferOverflowException


在我看来,您希望像使用基本数组一样使用此
IntBuffer
,在这种情况下,在初始化例程中,您需要在调用
flip()后将
限制设置为
容量
根据文档,它将
限制重置为
标记

编辑:+1到Arjan对原始问题的评论。我将Don的代码复制到我的机器上,它在Java 8上工作,也得到400/400作为输出。您是否在注释代码的其他地方设置
限制

你读过这个函数是如何工作的吗?特别是,这个类有一个
limit
属性,它没有一个完整的初始值。如果超过
limit
值,就会抛出一个
BufferOverflowException


在我看来,您希望像使用基本数组一样使用此
IntBuffer
,在这种情况下,在初始化例程中,您需要在调用
flip()后将
限制设置为
容量
根据文档将
限制
重置为
标记

无法复制,每次运行代码时,我都会将
400/400
作为输出。您的代码中的IBVerticesIndex是什么类型的(我只是将其声明为
IntBuffer
)…你能编辑你的帖子并包含周围的代码吗?你是如何准确地获得这些输出的?哦,你的
println
在内部循环中,而不是你在示例代码中放的地方。你缺少了一些卷曲…请清理这些乱七八糟的东西,这显然让至少一个读者对你的问题感到困惑:P仍然无法重新编程ce,我从
4/400
400/400
的所有输出都是4步一步得到的。你的问题在别处。你在
index
的声明和循环之间遗漏了什么代码?是的,很抱歉弄得一团糟。我在尝试填充球体顶点的OPENGL索引缓冲区时,删掉了可能不相关的部分。你是关于花括号,println应该在循环之外。我将用我的代码的mor更新这个问题。从缓冲区超类,
flip()
使缓冲区为新的通道写入或相对get操作序列做好准备:它将限制设置为当前位置,然后将位置设置为零。这还意味着您在外部循环的每次迭代中都会覆盖缓冲区中的数据,因为位置设置为0。我真诚地怀疑这是您在该构造中想要的ctor?就是这样!flip()不应该在循环中。当你处理这么多代码时,你会对这些小细节视而不见。多亏了你们两个。无法复制,我在运行代码的任何时候都会得到
400/400
作为输出。你的代码中的IBMVerticesIndex是什么类型(我只是将它声明为
IntBuffer
)…你能编辑你的帖子并包含周围的代码吗?你是如何准确地获得这些输出的?哦,你的
println
在内部循环中,而不是你在示例代码中放的地方。你缺少了一些卷曲…请清理这些乱七八糟的东西,这显然让至少一个读者对你的问题感到困惑:P仍然无法重新编程ce,我从
4/400
400/400
的所有输出都是4步一步得到的。你的问题在别处。你在
index
的声明和循环之间遗漏了什么代码?是的,很抱歉弄得一团糟。我在尝试填充球体顶点的OPENGL索引缓冲区时,删掉了可能不相关的部分。你是关于花括号,println应该在循环之外。我将用我的代码的mor更新这个问题。从缓冲区超类,
flip()
使缓冲区为新的通道写入或相对get操作序列做好准备:它将限制设置为当前位置,然后将位置设置为零。这也意味着
public sphereHelper(float fRadius, int iSlices, int iStacks)

{  
    FBVertices = GLDrawHelper.directFloatBuffer(new float[iSlices*iStacks*4*3]); 
    FBVertices.clear();

    IBVerticesIndex = GLDrawHelper.directIntBuffer(new int[iSlices*iStacks*4]);
    IBVerticesIndex.clear();
    int index=0;




    float verticalDegreePerStack= (float)Math.PI/iStacks; //1*Pi because we only want the height of the sphere once (from -PI/2 to PI/2)
    float horizontalDegreePerSlice = 2.0f* (float)Math.PI/iSlices; //2*Pi because of unit circle/polar coordinates going once around the sphere

    for (int k=0; k< iStacks; k++)
    {
        //overall vertical angle
        float rho= (float)k*verticalDegreePerStack;
        //sinus of that angle
        float srho =(float) (Math.sin(rho));

        float crho = (float) (Math.cos(rho));

        //sinus and cosinus of vertical angle+ angle of 1 stack
        float srhodrho= (float) (Math.sin(rho + verticalDegreePerStack));
        float crhodrho = (float) (Math.cos(rho + verticalDegreePerStack));

        for (int j=0; j<iSlices;j++)
        {
            //overall angle along the horizontal border (0 degree at full 2*PI circle)
            float theta = (j==iSlices)? 0.0f : j*horizontalDegreePerSlice;
            //sinus and cosinus
            float stheta = (float)(-Math.sin(theta));
            float ctheta = (float)(Math.cos(theta));

            //coordinates of first vertex of current triangle
            float x=stheta*srho;
            float z=ctheta*srho;
            float y=crho;


            //put coordinates of first vertex into buffer
            FBVertices.put(new float[]{x*fRadius,y*fRadius,z*fRadius});

            //put index of first vertex of polygon in index array, increase index counter by one
            if (IBVerticesIndex.position()<IBVerticesIndex.capacity())
            {IBVerticesIndex.put(index);}
            index++;
            //second vertex
            x = stheta*srhodrho;
            z = ctheta*srhodrho;
            y = crhodrho;

            //put these coordinates into Buffer
            FBVertices.put(new float[] {x*fRadius,y*fRadius,z*fRadius});


             if (IBVerticesIndex.position()+2<IBVerticesIndex.capacity())
            {
            IBVerticesIndex.put(new int[] {index,index+1,index+2});}
            index++;

            System.out.println(IBVerticesIndex.position()+"/"+IBVerticesIndex.capacity());

        }
        IBVerticesIndex.flip();
        FBVertices.flip();

       }
    }