nio ByteBuffer filling

R

Remon van Vliet

Hello,

I have a situation with ByteBuffers and i was wondering what the best way is
to do what i want :

- ByteBuffer A is 128 bytes large
- ByteBuffer B is much larger

Now i want to fill buffer A with as much as possible from buffer B, so that
A gets filled with B.position() til B.position() + 128. What is the best way
to accomplish this? Or more importantly, the fastest way.

Thanks,

Remon
 
J

John C. Bollinger

Remon said:
Hello,

I have a situation with ByteBuffers and i was wondering what the best way is
to do what i want :

- ByteBuffer A is 128 bytes large
- ByteBuffer B is much larger

Now i want to fill buffer A with as much as possible from buffer B, so that
A gets filled with B.position() til B.position() + 128. What is the best way
to accomplish this? Or more importantly, the fastest way.

Supposing that A has a backing byte[], have you considered
B.get(A.array()) ? You might need to manually adjust the A's position
and limit after that (if you care about it). You can exert finer
control by using ByteBuffer.get(byte[], int, int). I think it unlikely
that you could find a better-performing approach, unless that altogether
fails. You should, however, read the API docs to get the particulars on
these operations.
 
R

Remon van Vliet

Ah, my apologies, i forgot to mention that A doesnt have a backing byte[]
(or at best it's not guaranteed), because it's a allocateDirect() buffer.
Perhaps my understanding of direct buffers is incorrect, but i was under the
assumption that these buffers offer significant performance gains when used
as IO buffers for NIO channels. If this assumption is incorrect than the
problem is solved

Thanks for your input,

Remon

John C. Bollinger said:
Remon said:
Hello,

I have a situation with ByteBuffers and i was wondering what the best way is
to do what i want :

- ByteBuffer A is 128 bytes large
- ByteBuffer B is much larger

Now i want to fill buffer A with as much as possible from buffer B, so that
A gets filled with B.position() til B.position() + 128. What is the best way
to accomplish this? Or more importantly, the fastest way.

Supposing that A has a backing byte[], have you considered
B.get(A.array()) ? You might need to manually adjust the A's position
and limit after that (if you care about it). You can exert finer
control by using ByteBuffer.get(byte[], int, int). I think it unlikely
that you could find a better-performing approach, unless that altogether
fails. You should, however, read the API docs to get the particulars on
these operations.
 
J

John C. Bollinger

Remon said:
Remon van Vliet wrote:
- ByteBuffer A is 128 bytes large
- ByteBuffer B is much larger

Now i want to fill buffer A with as much as possible from buffer B, so
that
A gets filled with B.position() til B.position() + 128. What is the best
way
to accomplish this? Or more importantly, the fastest way.

Supposing that A has a backing byte[], have you considered
B.get(A.array()) ? You might need to manually adjust the A's position
and limit after that (if you care about it). You can exert finer
control by using ByteBuffer.get(byte[], int, int). I think it unlikely
that you could find a better-performing approach, unless that altogether
fails. You should, however, read the API docs to get the particulars on
these operations.

[moved from above; please don't top-post:]
Ah, my apologies, i forgot to mention that A doesnt have a backing byte[]
(or at best it's not guaranteed), because it's a allocateDirect() buffer.
Perhaps my understanding of direct buffers is incorrect, but i was under the
assumption that these buffers offer significant performance gains when used
as IO buffers for NIO channels. If this assumption is incorrect than the
problem is solved

In that case, it seems your best option is probably something like

byte[] copyBuffer = new byte[128];

....

B.get(copyBuffer);
A.put(copyBuffer);


That begs the question, however, of why you need to transfer bytes among
Buffers at all. The design mode of operation for these things is to use
a single buffer to handle both input and output. For instance, you
clear(), read into the buffer, flip(), write out from the buffer,
repeat. Yes, direct buffers supposedly may have performance advantages,
but I'd be surprised if this remained the case once you add the extra
overhead of moving the bytes between buffers. Any performance advantage
of direct ByteBuffers must come precisely from avoiding this sort of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top