2

We have a Java UUID being written to a ByteArrayOutputStream. Then bytearray of that ByteArrayOutputStream is base64 encoded and sent in a JSON. The encoding process is as follows.

public String uuidToString(UUID uuidObj) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final DataOutputStream dos = new DataOutputStream(baos);
    dos.writeLong(uuidObj.getMostSignificantBits());
    dos.writeLong(uuidObj.getLeastSignificantBits());
    dos.flush();
    return Base64.encodeBase64URLSafeString(baos.toByteArray());
}

I am trying to figure out how to decode this String in the javascript side. Ofcourse the base64 part is easy. Using atob I get a String and then using Crypto.js I can get a byte array out of that base64 decoded String. Then I tried to use the conversion techniques mentioned in the following link to convert but its always returning garbage.

Converting byte array to string in javascript

Using the String as is directly in JSON is not an option for a couple of reasons. Firstly we want to keep this String opaque, but we also want to keep the size of the final encoded string that is sent over the wire to be small.

I know another option is protocol buffers, but the lack of a supported js implementation and the extra build time leg work seem to be barriers on that front.

So do I have any other options ? And any suggestions on why my javascript decoding isn't working.

Community
  • 1
  • 1
  • If the result is 'garbage' my guess would be the correct base64 string is not being read in JavaScript. Is there a test UUID used with corresponding base64 output that can be (has been) verified? – user2864740 Dec 09 '15 at 05:28
  • @user2864740 This is a test uuid and the corresponding base64 output UUID: 8a628dd0-c87a-4720-a60c-c0f402a13dd1 Base64 Output: imKN0Mh6RyCmDMD0AqE90Q – objectGlory Dec 09 '15 at 18:43

0 Answers0