Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to read a JSON byte array into a java object, e.g byte[]

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to read a byte array passed from a web browser to the server.

The client reads a file then converts it to a string to pass to the server like this:

var bytes = new Uint8Array(arrayBuffer);
var s_bytes = JSON.stringify(bytes);

s_bytes now contains this for example (a txt file containing hello world) :

["{\"0\":104,\"1\":101,\"2\":108,\"3\":108,\"4\":111,\"5\":32,\"6\":119,\"7\":111,\"8\":114,\"9\":108,\"10\":100}"]

On the server side I get the string and try to convert it back to an array that I can then write to a file on the server, but this isn't as simple as I thought it should be. This example would then create a file containg the words hello world - of course the idea is to support other data also! So, my question is, is there an efficient way to convert the string to an array. Sure I could build a parser myself for this, but my guess is there are very fast packages out there that can do the job far better than I ever could!
com.fasterxml.jackson classes seems promising, but haven't got them to work yet.

Thanks in advance!
 
Saloon Keeper
Posts: 13292
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you even using JSON to send binary data? Just use an application/octet-stream MIME type and send the data directly.
 
Stephan van Hulst
Saloon Keeper
Posts: 13292
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to send other parameters as well, you can probably use multipart/form-data too.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic