缓冲区下溢异常 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20644505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:19:10  来源:igfitidea点击:

Bufferunderflowexception Java

javaexceptionpaintcomponentfilechannelbufferunderflowexception

提问by Swag

I am writing values to a file.

我正在将值写入文件。

The values are written correct. In another application I can read the file without any exceptions.

值写入正确。在另一个应用程序中,我可以毫无例外地读取文件。

But in my new application, I get a Bufferunderflowexceptionwhen trying to read the file.

但是在我的新应用程序中,我Bufferunderflowexception在尝试读取文件时得到了一个。

I have spend days to fix this, but I just don't know how to solve it.

我花了几天时间来解决这个问题,但我只是不知道如何解决它。

Did A LOT of research too.

也做了很多研究。

The bufferunderflowexceptionrefers to :

bufferunderflowexception指:

Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)

This is my code to read the file:

这是我读取文件的代码:

 @Override
    public void paintComponent(Graphics g) {

    RandomAccessFile randomAccessFile = null;
    MappedByteBuffer mappedByteBufferOut = null;
    FileChannel fileChannel = null;

    try {
        super.paintComponent(g);

        File file = new File("/home/user/Desktop/File");

        randomAccessFile = new RandomAccessFile(file, "r");

        fileChannel = randomAccessFile.getChannel();

        mappedByteBufferOut = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());

        while (mappedByteBufferOut.hasRemaining()) {

            Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)
            Double Y1 = mappedByteBufferOut.getDouble();
            Double X2 = mappedByteBufferOut.getDouble();
            Double Y2 = mappedByteBufferOut.getDouble();
            int colorRGB = mappedByteBufferOut.getInt(); //4 byte (int)
            Color c = new Color(colorRGB);

            Edge edge = new Edge(X1, Y1, X2, Y2, c);

            listEdges.add(edge);

        }
        repaint();

        for (Edge ed : listEdges) {
            g.setColor(ed.color);
            ed = KochFrame.edgeAfterZoomAndDrag(ed);
            g.drawLine((int) ed.X1, (int) ed.Y1, (int) ed.X2, (int) ed.Y2);
        }
    }
    catch (IOException ex)
    {
        System.out.println(ex.getMessage());
    }
    finally
    {
        try
        {
            mappedByteBufferOut.force();
            fileChannel.close();
            randomAccessFile.close();
            listEdges.clear();
        } catch (IOException ex)
        {
            System.out.println(ex.getMessage());
        }
    }
}

I hope someone can help me.

我希望有一个人可以帮助我。

采纳答案by Sinkingpoint

From the docsof java.nio.ByteBuffer:

来自java.nio.ByteBuffer的文档

Throws: BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer

抛出: BufferUnderflowException - 如果此缓冲区中剩余的字节少于 8 个

I think that makes it pretty clear where this is Exception is coming from. In order to fix it, you need to make sure that the ByteBuffer has enough data in it in order to read a double (8 bytes) by instead of hasRemaining() which only checks for one byte:

我认为这很清楚这是 Exception 的来源。为了修复它,您需要确保 ByteBuffer 中有足够的数据以读取双精度(8 个字节)而不是 hasRemaining() 只检查一个字节:

while (mappedByteBufferOut.remaining() >= 36) {//36 = 4 * 8(double) + 1 * 4(int)

回答by Peter Lawrey

I wouldn't use Doublewhen you can use double

Double当你可以使用时我不会使用double

I suspect your problem is that you have bytes remaining at the start of the loop, but you didn't check how many bytes and there isn't enough.

我怀疑您的问题是循环开始时还有剩余的字节,但是您没有检查有多少字节并且没有足够的字节。

I would also make sure you have the right byte endianness the default is big endian.

我还要确保你有正确的字节字节序,默认是大字节序。