Michael Bauroth wrote:
Hi,
I've a little problem regarding the different access methods for the
ByteBuffer. I've programmed a decoder, which looks for different
character sequences in the incoming ByteBuffer, so that I must access
each of the elements in the buffer more than once.
I've checked two appraches:
1) In the first I use the ByteBuffer.get( index i ) method.
2) In the second I copy the whole buffer over get( dst[] ) in a byte
array and do the search on this array.
Now my problem. The first method tooks much more CPU time then the
second one. Ok, not problem, use the second one could you say.
Unfortunately in the second approach I must copy each time the whole
buffer (e.g. 500 byte), also in the case when the pattern is located
somewhere at the beginning. Here the first solution needs only 1 or two
gets until match without copying all.
Any suggestions to solve this problem are very welcome.
You could try to use the relative get method instead of the absolute
one. Just make sure you remember the inital position and reset the
position once you're done:
int pos = position();
try {
// Code which uses ByteBuffer.get()
// ...
} finally {
buf.position(pos);
}
/Niklas