The OutputStream Interface in JavaScript

On this page:

Initializing an OutputStream in JavaScript

The OutputStream class provides a constructor that accepts up to two arguments:

  • A communicator instance
  • An encoding version

We recommend supplying a communicator instance. The stream inspects the communicator's settings to configure several of its own default settings, but you can optionally configure these settings manually as we describe below.

If you omit an encoding version, the stream uses the default encoding version of the communicator (if provided) or the most recent encoding version.

Use the following property to manually configure the stream:

JavaScript
var stream = new Ice.OutputStream(...);
stream.format = ...;

For instances of Slice classes, the format determines how the slices of an instance are encoded. If the stream is initialized with a communicator, this setting defaults to the value of Ice.Default.SlicedFormat, otherwise the setting defaults to the compact format.

Inserting into an OutputStream in JavaScript

OutputStream provides a number of write methods that allow you to insert Slice types into the stream.

For example, you can insert a boolean and a string into a stream as follows:

Java
Ice.OutputStream out = new Ice.OutputStream(communicator);
out.writeBool(true);
out.writeString("Ice rocks!");
var data = out.finished();

Here are the methods for inserting data into a stream:

  • writeByte(v)
    writeBool(v)
    writeShort(v)
    writeInt(v)
    writeLong(v)
    writeFloat(v)
    writeDouble(v)
    writeString(v)
    Inserts a primitive value.
  • writeByteSeq(v)
    Inserts an array as a byte sequence.
  • rewriteByte(v, dest)
    rewriteBool(v, dest)
    rewriteInt(v, dest)
    Overwrites the byte(s) at an existing location in the buffer at the given destination with a value. These methods do not change the current position of the stream.

  • writeSize(sz)
    The Ice encoding has a compact representation to indicate size. This function converts the given non-negative integer into the proper encoded representation.
  • writeProxy(v)
    writeOptionalProxy(tag, v)

    Inserts a proxy or an optional proxy.
  • writeValue(v)
    writeOptionalValue(tag, v)

    Inserts an instance of a Slice class or an optional instance. The Ice encoding for class instances may cause the insertion to be delayed, in which case the stream retains a reference to the given instance and does not insert its state it until writePendingValues is invoked on the stream.
  • writeEnum(v)
    Writes the integer value of an enumerator.

  • writeBlob(v)
    Copies the given array of bytes directly to the stream's internal buffer without modification.

  • startValue(slicedData)
    endValue()
    When marshaling the slices of a class instance, the application must first call startValue, then marshal the slices, and finally call endValue. The caller can pass a SlicedData object containing the preserved slices of unknown more-derived types, or null if there are no preserved slices.

  • startException(slicedData)
    endException()
    When marshaling the slices of an exception, the application must first call startException, then marshal the slices, and finally call endException. The caller can pass a SlicedData object containing the preserved slices of unknown more-derived types, or 0 if there are no preserved slices.
  • startSlice(typeId, compactId, last)
    endSlice()
    Starts and ends a slice of class or exception member data. The call to startSlice must include the type ID for the current slice, the corresponding compact ID for the type (if any), and a boolean indicating whether this is the last slice of the class instance or exception. The compact ID is only relevant for class instances; pass a negative value to indicate the encoding should use the string type ID.
  • startEncapsulation(encoding, fmt)
    endEncapsulation()
    Starts and ends an encapsulation, respectively. When calling startEncapsulation, you can optionally specify the encoding version as well as the format to use for any class instances and exceptions marshaled within this encapsulation.
  • writeEmptyEncapsulation(encoding)
    Writes an encapsulation having the given encoding version with no encoded data.
  • writeEncapsulation(v)
    Copies the bytes representing an encapsulation from the given array into the stream.
  • getEncoding()
    Returns the encoding version currently being used by the stream.
  • writePendingValues()
    Encodes the state of class instances whose insertion was delayed during writeValue. With encoding version 1.0, this method must only be called exactly once when non-optional data members or parameters use class types. This method is no-op with encoding version 1.1.
  • writeOptional(tag, fmt)
    Prepares the stream to write an optional value with the given tag and format. Returns true if the value should be written, or false otherwise. A return value of false indicates that the encoding version in use by the stream does not support optional values. If this method returns true, the data associated with that optional value must be written next. Optional values must be written in order by tag from least to greatest. The OptionalFormat enumeration is defined as follows:

    Slice
    module Ice
    {
        enum OptionalFormat
        {
            OptionalFormatF1, OptionalFormatF2, OptionalFormatF4, OptionalFormatF8,
            OptionalFormatSize, OptionalFormatVSize, OptionalFormatFSize,
            OptionalFormatEndMarker
        }
    }

    Refer to the encoding discussion for more information on the meaning of these values.

  • writeOptionalHelper(tag, fmt, writeFn, v)
    If the value v is not undefined, this method writes an optional value using the given tag and format. The helper function writeFn is called to insert the value in v. For example, you can pass OutputStream.prototype.writeByte as the helper function when writing an optional byte.
  • startSize()
    endSize(n)
    The encoding for optional values uses a 32-bit integer to hold the size of variable-length types. Calling startSize writes a placeholder value for the size and returns the starting position of the size value; after writing the data, call endSize to patch the placeholder with the actual size at the given position.
  • finished()
    Indicates that marshaling is complete and returns the encoded byte sequence as an array. This member function must only be called once.
  • resize(sz)
    Allocates space for sz more bytes in the buffer. The stream implementation internally uses this function prior to copying more data into the buffer.

Finally, you can use the pos property to get and set the stream's current position.

See Also