VMware Aria Automation offers a byte buffer class, in the context of the JavaScript runtime environment. This is a wrapper around a byte array and used for passing references to binary content. To use the methods of the this class outside of Aria Automation, a library is available here to emulate it.

This mock-up of the byte buffer class bases on the same JavaScript engine used in Aria Automation, the Mozilla Rhino engine. To use this file classes it is necessary to use the Rhino engine, because their constructor access Java classes directly. This ensures us a consistent basis for simulation and operation.

To use this library in your code it is necessary to add e.g.  load("bytebuffer.class.js");  at the beginning of the program. After that, all functions can be used seamlessly just like in Aria Automation.

You can download the source from my GitHub account.

/**
 * Mock-up of the ByteBuffer class from VMware Aria Automation.
 * Wrapper around byte array. Used for passing references to binary
 * content.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Hint: This mock-up works only with the Mozilla Rhino JavaScript
 * engine.
 *
 * Checked with Rhino engines version 1.7R4 and 1.7.14
 */

var ByteBuffer = function(arg) {

  /**
   * Creates an empty byte buffer, byte buffer from a base64 string or
   * from a byte buffer.
   */

  var context;

  try {

    var contextFactory = org.mozilla.javascript.ContextFactory();
    context = contextFactory.getGlobal().enterContext();

    if (typeof arg === "undefined" || arg === null) {

      this._byteBuffer = context.jsToJava(
        java.lang.String("").getBytes(),
        java.lang.Class.forName("[B")
      );

    } else if (typeof arg === "string") {

      this._byteBuffer = context.jsToJava(
        java.util.Base64.getDecoder().decode(
          java.lang.String(arg).getBytes()
        ),
        java.lang.Class.forName("[B")
      );

    } else if (
      typeof arg === "object" &&
      arg.getClassName() === "ByteBuffer"
    ) {

      this._byteBuffer = System.clone(arg);

    }

  } catch (exception) {
    System.log(exception);
  } finally {
    context.exit();
  }

};

ByteBuffer.prototype = {

  /**
   * Returns the class name.<br>
   * Hint: This method is a standard.
   *
   * @function getClassName
   * @returns {string}
   *
   * @example
   * var byteBuffer = new ByteBuffer();
   * var result = byteBuffer.getClassName();
   * System.log(result);
   */
  getClassName : function() {
    return "ByteBuffer";
  },

  /**
   * Gets the length of the buffer.
   *
   * @function length
   * @returns {number}
   *
   * @example
   * // Delivers 14
   * var base64 = System.stringToBase64("This is a test");
   * var byteBuffer = new ByteBuffer(base64);
   * System.log(byteBuffer.length());
   */
  length : function() {
    return this._byteBuffer.length;
  }

};



This site is part of blog.stschnell.de