B2G/gecko/dom/mobilemessage/src/ril/WspPduHelper.jsm
/**
   * @param multiStream
   *        An exsiting nsIMultiplexInputStream.
   * @param parts
   *        An array of objects representing multipart entries.
   *
   * @see WAP-230-WSP-20010705-a section 8.5
   */
@param multiStream
An exsiting nsIMultiplexInputStream.
@param parts
An array of objects representing multipart entries.


@param multiStream An exsiting nsIMultiplexInputStream . @param parts An array of objects representing multipart entries .
function composeMultiPart(multiStream, parts) {
    {
      let data = {array: [], offset: 0};
      UintVar.encode(data, parts.length);
      debug("Encoded multipart header: " + JSON.stringify(data.array));
      this.appendArrayToMultiStream(multiStream, data.array, data.offset);
    }
    for (let i = 0; i < parts.length; i++) {
      let part = parts[i];
      let data = {array: [], offset: 0};
      let contentType = part.headers["content-type"];
      ContentTypeValue.encode(data, contentType);
      if (Object.keys(part).length > 1) {
        delete part.headers["content-type"];
        for (let name in part.headers) {
          Header.encode(data, {name: name, value: part.headers[name]});
        }
        part.headers["content-type"] = contentType;
      }
      let headersLen = data.offset;
      UintVar.encode(data, headersLen);
      if (typeof part.content === "string") {
        let charset;
        if (contentType && contentType.params && contentType.params.charset &&
          contentType.params.charset.charset) {
          charset = contentType.params.charset.charset;
        }
        part.content = this.encodeStringContent(part.content, charset);
        UintVar.encode(data, part.content.length);
      } else if (part.content instanceof Uint8Array) {
        UintVar.encode(data, part.content.length);
      } else {
        throw new TypeError();
      }
      let slice1 = data.array.slice(headersLen);
      let slice2 = data.array.slice(0, headersLen);
      data.array = slice1.concat(slice2);
      debug("Encoded per-part header: " + JSON.stringify(data.array));
      this.appendArrayToMultiStream(multiStream, data.array, data.offset);
      this.appendArrayToMultiStream(multiStream, part.content, part.content.length);
    }
  }
function composeMultiPart ( multiStream , parts ) { { let data = { array : [ ] , offset : 0 } ; UintVar . encode ( data , parts . length ) ; debug ( "Encoded multipart header: " + JSON . stringify ( data . array ) ) ; this . appendArrayToMultiStream ( multiStream , data . array , data . offset ) ; } for ( let i = 0 ; i < parts . length ; i ++ ) { let part = parts [ i ] ; let data = { array : [ ] , offset : 0 } ; let contentType = part . headers [ "content-type" ] ; ContentTypeValue . encode ( data , contentType ) ; if ( Object . keys ( part ) . length > 1 ) { delete part . headers [ "content-type" ] ; for ( let name in part . headers ) { Header . encode ( data , { name : name , value : part . headers [ name ] } ) ; } part . headers [ "content-type" ] = contentType ; } let headersLen = data . offset ; UintVar . encode ( data , headersLen ) ; if ( typeof part . content === "string" ) { let charset ; if ( contentType && contentType . params && contentType . params . charset && contentType . params . charset . charset ) { charset = contentType . params . charset . charset ; } part . content = this . encodeStringContent ( part . content , charset ) ; UintVar . encode ( data , part . content . length ) ; } else if ( part . content instanceof Uint8Array ) { UintVar . encode ( data , part . content . length ) ; } else { throw new TypeError ( ) ; } let slice1 = data . array . slice ( headersLen ) ; let slice2 = data . array . slice ( 0 , headersLen ) ; data . array = slice1 . concat ( slice2 ) ; debug ( "Encoded per-part header: " + JSON . stringify ( data . array ) ) ; this . appendArrayToMultiStream ( multiStream , data . array , data . offset ) ; this . appendArrayToMultiStream ( multiStream , part . content , part . content . length ) ; } }
@param multiStream
        An exsiting nsIMultiplexInputStream.
@param multiStream An exsiting nsIMultiplexInputStream .
// Encode multipart header // Encode each part // Encode Content-Type // Encode other headers // Remove Content-Type temporarily // Restore Content-Type back // Encode headersLen, DataLen // Move them to the beginning of encoded octet array. // Append per-part header // Append part content