0

I have a list of ByteStrings generated by java using Google's Protocol Buffers toByteArray() method (docs), like below:

  ...
  [B@e3bbd6a
  [B@5f9c39ba
  [B@11838ae3
  [B@57dc7b85
  [B@61870e86
  [B@2592cb49
  ...

I'm assuming they are UTF-8 byte strings (but I'm not sure) but how would I convert them to JavaScript Buffers? I'm using node's protocol-buffers package to decode but the format they can decode from is something like below:

<Buffer 00 c0 a9 07 12 08 43 61 64 69 6c 6c 61 63>

I've tried using node's Buffer api, but haven't had much success.

Any tips on where to start? I've found this question that was asked previously, is that the best way to go?

Community
  • 1
  • 1
ynih
  • 1
  • 2

1 Answers1

1

I think you're confusing byte arrays with their debug string representations. Byte arrays (or Buffers) do not necessarily contain UTF-8 text and do not make sense to print as text, and so when you try to print them out in various languages, the runtime will try to create a human-readable text representation of the value as a convenience. Java and Node do this in completely different ways for the same underlying values.

Java: The string [B@e3bbd6a is not the content of a byte array. When you call toString() on a Java byte[], you get some text like this, but all it's telling you is that the object is a byte array located at address e3bbd6a. This is not the content of the array, it's just the memory address. (The idea here is that if you printed two different byte[] values you would be able to tell if they are the same object based on the addresses matching, but otherwise the text is totally meaningless.)

Node: The string <Buffer 00 c0 a9 ...> is again just a friendly human-readable string meant for debugging purposes. This isn't the actual low-level representation -- the low-level representation is raw bytes, i.e. the numeric values 00, c0, a9, etc.

There is no need to "convert" between the two. If you have a byte[] in Java and you write it to a socket or file, and then you read that socket or file in Node into a Buffer object, the Buffer will in fact contain the same data.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
  • So if I'm writing the list of strings like `[B@e3bbd6a` to a socket, and I use node to read from that socket, how do I get the data so I can decode it? Am I to understand that my I'm only writing the address of the byte array into the socket and not the actual data? – ynih Mar 17 '15 at 08:52
  • Don't write strings to the socket, write the actual byte array. E.g. `OutputStream#write()` takes `byte[]` as the argument, not `String`. If you use that, you are writing the actual bytes, not pointers. On the Node end, reading from a socket normally results in a Buffer. – Kenton Varda Mar 17 '15 at 18:13
  • Thank you so much! Your advice was just the right push I needed. Now I got it working and both sides are happily encoding, streaming, and decoding. Much appreciated :) – ynih Mar 18 '15 at 08:38