Hello,
I just need some help in ByteBuffer's put(index, byte) method.
Basically I have a byte array which stores data and is put into a ByteBuffer. The problem I'm facing is I want to put extra data in the ByteBuffer in index 2 (The positioning is very important)
How would i go about this without overriding the previous data?

Just an example of what I mean:

byte[] d = new byte[10];
for(byte i = 0; i < 10; i++)
	d[i] = i;
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(d);
buffer.put(2, (byte)100);
for(byte b : buffer.array())
	System.out.print(b);

The output of that would be

0
1
100
3
4
5
6
7
8
9

Instead I want

0
1
2
100
3
4
5
6
7
8
9

So any help in achieving this is much appreciated :)

AFAICT, there is no such method which does that. BTW, why would you want such a method? Why not just manipulate the source array first and then add it to the ByteBuffer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.