Menu

#241 NumberFormatException when generating an ack message

2.3
closed-fixed
nobody
None
1
2016-06-22
2016-06-20
No

When generating an ack message for a message I have received, I sometimes receive a NumberFormatException.

This is the code:

Message ack;
try {
ack = theMessage.generateACK();
return ack;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

The issue is in the DelegatingHiLoGenerator:

@Override
protected int getNextHiId() throws IOException {
if (delegate == null)
throw new NullPointerException(
"Must initialize delegate IDGenerator");
return Integer.parseInt(delegate.getID());
}

In my case, the delegate is a FileBasedGenerator:

public String getID() throws IOException {
try {
lock.lock();

        // If ID is 0, read initial value from file if possible
        if (!minimizeReads || !used) {
            long readInitialValue = readInitialValue(getFilePath());
            if (readInitialValue >= 0) {
                set(readInitialValue);
            }
            used = true;
        }

        String id = super.getID();
        // The id held in the file is always <increment> larger so that
        // the ID is still unique after a restart.
        writeNextValue(Long.parseLong(id) + getIncrement());
        return id;
    } finally {
        lock.unlock();
    }
}

When generating an ID with the FileBasedGenerator, we are generating Long. But when trying to return an ID, we use Integer.parseInt(...)

When I run into this exception, the file has a large ID number, exceeding the limits of an integer and result in a NumberFormatException.

Discussion

  • Christian Ohr

    Christian Ohr - 2016-06-22
    • status: open --> closed-fixed
    • Group: 2.2 --> 2.3
     
  • Christian Ohr

    Christian Ohr - 2016-06-22

    Fixed. Thanks for spotting this.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.