• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

extracting bytes from byte array

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would never have guessed that working at the byte level in Java would be so tough. I'm having to work with a structured blob that is retrieved from a database. The structured blob is a series of tokenized data in the following form:
2-byte (int) Token ID
2-byte (int) Date Type
2-byte (int) Data Length
n-byte (?) Data value
If I load this structured blob into a byte array, how do I extract these binary and other values from the array and put them into particular variables?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say the easiest way to do this in Java is something like

Note that when using DataInputStream, detecting the end of file can be a bit messy. Using available() works here because the underlying ByteArrayInputStream is guaranteed to have the entire array available - you won't see available() return 0 unless there really are no more bytes to read. However this is not the case for other types of streams - if you were reading from a socket for example, you'd have to allow that when available() == 0 it might just mean that the remaining bytes haven't been sent yet, for example. So be careful about using available() in other circumstances.
 
Gary Frick
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim for the suggestion. I'll play with this to see how it plays out. Gary
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, just noticed. If those integer values are stored using only two bytes, you can't use readInt() - you probably need readShort() or readUnsignedShort(). You can read the API for DataInputStream to see exactly what each one does. If it's not what you need exactly, don't overlook other methods - DataInputStream has a lot of different things it can do.
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic