3 Replies Latest reply on Apr 25, 2008 4:46 AM by Patrick Leckey

    SOAP.streamEncode loses trailing nulls

    BR001 Adobe Community Professional & MVP
      I am trying to base64 encode data for a web service call using the SOAP.streamEncode method but am losing any trailing characters.

      The following code, that encodes a string and then decodes the stream shows the problem I am having;

      >var string2 = new String("\xF4\x09\xB2\x0q\x00\x00\x00\x00");
      var encode2 = SOAP.streamEncode(SOAP.streamFromString(string2), "base64");
      var decode2 = SOAP.streamDecode(SOAP.streamEncode(SOAP.streamFromString(string2), "base64"), "base64");
      console.println("string2.length " + string2.length);
      console.println("string2 " + string2.toSource());
      var encode2String = SOAP.stringFromStream(encode2);
      console.println("encode2.length " + encode2String.length);
      console.println("encode2 " + encode2String);
      var decode2String = SOAP.stringFromStream(decode2);
      console.println("decode2.length " + decode2String.length);
      console.println("decode2 " + decode2String.toSource());

      This displays the following on the console;

      >string2.length 10
      string2 (new String("\xF4\t\xB2x0q\x00\x00\x00\x00"))
      encode2.length 8
      encode2 9AmyeDBx
      decode2.length 6
      decode2 (new String("\xF4\t\xB2x0q"))

      The last line should have returned the same string as I started with.

      Has anyone else encountered this problem and found a workaround?

      My initial aim was to submit attached documents to my web service. These are generally Word documents which appear to always contain trailing spaces. I know I could use hex encoding (and am currently doing this) but hex encoding doubles the size of attachment and I am getting complaints about the response times
        • 1. Re: SOAP.streamEncode loses trailing nulls
          Patrick Leckey Level 3
          Strings are null-terminated. You can prove this by changing the 'B2' character to null (00) and your returned string will only be '\xF4\t'. It follows the C-String standard.
          • 2. Re: SOAP.streamEncode loses trailing nulls
            BR001 Adobe Community Professional & MVP
            Having played with this problem some more it seems the SOAP.stringFromStream function stops when it encounters the first null character (as PDL mentioned above, though JavaScript handles nulls as the length of string2 in the example above is 10). This also seems to be the case when using a ReadStream object as a parameter to a web service call or when using Message Transmission Optimization Mechanism (MTOM), which I assume use the same function.

            I have written my own Base64 encoding but have not yet worked out if the time taken is worth saving the 33% of the transmission size.

            If anyone is interested;

            var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");
            function EncodeHexString(hexString)
            {
            var result = "";
            var padding = "";
            for (i = (hexString.length * 2) % 3; i > 0 && i < 3; i++)
            {
            padding += '=';
            hexString += "00";
            }
            for (i = 0; i < hexString.length; i += 6)
            {
            var n = (parseInt(hexString.substring(i, i+2), 16) << 16) + (parseInt(hexString.substring(i+2, i+4), 16) << 8 ) + (parseInt(hexString.substring(i+4, i+6), 16));
            result += base64chars[(n >>> 18) & 63] + base64chars[(n >>> 12) & 63] + base64chars[(n >>> 6) & 63] + base64chars[n & 63];
            }
            return result.substring(0, result.length - padding.length) + padding;
            }


            This takes the hex string that you would get from a call to SOAP.streamEncode, e.g.

            >SOAP.stringFromStream(SOAP.streamEncode(pdf.getDataObjectContents("file"), "hex"))
            • 3. Re: SOAP.streamEncode loses trailing nulls
              Patrick Leckey Level 3
              > though JavaScript handles nulls as the length of string2 in the example above is 10

              JavaScript is a procedural language, so when you instantiate a variable it will contain all the data you provide it, even nulls. The nulls will get chopped when any of the data is moved to another variable.

              Example:
              var string2 = new String("\xF4\x09\xB2\x0q\x00\x00\x00\x00");
              var string3 = string2;

              string2 will have the nulls, string3 will not.