• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Mapping byte stream to Java object

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I need to split the byte array and populate data fields of the Java object what is efficient way of doing the same. Walking through the byte array seems like easier way but may not be efficient. Any ideas would be appreciated.

Thanks,
Basheer
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we'll need a bit more detail. Can you show a SHORT bit of code around the problem, maybe what input and output you expect?
 
Ahmed Basheer
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the example:

let me populate bytearray;
byte bytebuffer[] = new byte[3];
bytebuffer[0]=0x11;
bytebuffer[1]=0xA3;
bytebuffer[2]=0x11;
bytebuffer[3]=0xF3;

This byte array is populated in such a way that first byte and first 4 bits in the 2nd byte contain "id1". lower 4 bits in 2nd byte contain "id2" and third byte contains "id3" and fourth byte contains "id4".

The Java object that should be mapped is like this

// The ExampleObject is like a java bean and byte array should map to this java bean.

ExampleObject {

private short id1;

private byte id2;

private byte id3;

private byte id4;

public setId1(short id1) { this.id1=id1}
...
public short getId1( return id1; }

...

}

I know the byte array and I know the name of the fields and I know the java object, need to figure out the best way to map. Some fields as you can see id1 could not be byte aligned. I can walk through the byte array but is there any other way.

In c++ you can cast struct to the byte stream and you have access to the data fields of structs. Does java offer any convinient way?

Thanks,
Basheer
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic