D
Duane Evenson
I am grabbing chunks of a ByteBuffer for processing. The last requested
chunk's size will have to be shrunk to the amount remaining in the buffer.
Should I use a test to reduce the size, or should I use the try-catch
mechanism and handle the exception?
The two choices look something like this:
// pretest
while ( data.hasRemaining() ) {
if (size > data.remaining() )
size = data.remaining();
data.get(ba, 0, size) );
// process ba
}
// exception handling
while ( data.hasRemaining() ) {
try {
data.get(ba, 0, size);
}
catch (BufferUnderflowException) {
data.get(ba, 0, data.limit - data.position);
}
// process ba
}
Which is better code?
chunk's size will have to be shrunk to the amount remaining in the buffer.
Should I use a test to reduce the size, or should I use the try-catch
mechanism and handle the exception?
The two choices look something like this:
// pretest
while ( data.hasRemaining() ) {
if (size > data.remaining() )
size = data.remaining();
data.get(ba, 0, size) );
// process ba
}
// exception handling
while ( data.hasRemaining() ) {
try {
data.get(ba, 0, size);
}
catch (BufferUnderflowException) {
data.get(ba, 0, data.limit - data.position);
}
// process ba
}
Which is better code?