简体   繁体   中英

Bufferunderflowexception Java

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 Bufferunderflowexception when trying to read the file.

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

Did A LOT of research too.

The bufferunderflowexception refers to :

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.

From the docs of java.nio.ByteBuffer:

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

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 checking remaining() instead of hasRemaining() which only checks for one byte:

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

I wouldn't use Double when you can use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM