get the 6 first bytes of a byte array

Hi,

I have a byte array, I would like to retrieve the 6 first bytes of this byte array.
And then, in certain cases, I need to remove those bytes from the array.
LVL 9
matthew016Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
you'll need to use arraycopy for that
for example to get the first 6 bytes create a new array of 6 bytes and copy 6 bytes from original array into it

there are some examples here
http://helpdesk.objects.com.au/?s=arraycopy
0
VenabiliCommented:
and to remove these 6 bytes, just create a new array with length originalLength-6 and copy the remaining ones there.

No easy removing of elements with arrays I am afraid :)
0
CEHJCommented:
You could use nio:
	ByteBuffer bb = ByteBuffer.wrap(yourByteArray);
	bb.limit(6);

	byte[] start = new byte[6];
	bb.get(start);
	System.out.println(java.util.Arrays.toString(start));

	if (true) { // replace by your condition
	    bb.limit(yourByteArray.length);
	    byte[] end = new byte[yourByteArray.length - 6];
	    bb.get(end);
	    System.out.println(java.util.Arrays.toString(end));
	}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

matthew016Author Commented:
Hi,

I tried but can't make it to work, see my code below :

Actually I have a ByteBuffer :

java.util.Arrays.toString(  this.readBuffer.array()   )

contains : [49, 59, 48, 59, 59, 59, 59, 59, 59, 59, 117, 115, 101, 114, 53, ....

Then I try :

byte[] bytesLentgh = new byte[6];
this.readBuffer.get(bytesLentgh);
System.out.println(java.util.Arrays.toString(bytesLentgh));

The code above will always display :
[0, 0, 0, 0, 0, 0]
0
Mick BarryJava DeveloperCommented:
you should be setting position first

byte[] bytesLentgh = new byte[6];
this.readBuffer.pos(0);  // assuming you want the first 6 bytes
this.readBuffer.get(bytesLentgh);
System.out.println(java.util.Arrays.toString(bytesLentgh));
0
matthew016Author Commented:
Thank you,

Then, how do I remove those 6 bytes from the ByteBuffer ?
0
Mick BarryJava DeveloperCommented:
readBuffer.compact();  // (if supported by the implementation you are using)
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.