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:
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:
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 untilwritePendingValues
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.
writeException(ex)
Inserts a user exception.
startValue(slicedData)
endValue()
When marshaling the slices of a class instance, the application must first callstartValue
, then marshal the slices, and finally callendValue
. The caller can pass aSlicedData
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 callstartException
, then marshal the slices, and finally callendException
. The caller can pass aSlicedData
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 tostartSlice
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 callingstartEncapsulation
, 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 duringwriteValue
. This member function must only be called once. For backward compatibility with encoding version 1.0, this function must only be called when non-optional data members or parameters use class types.
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. TheOptionalFormat
enumeration is defined as follows:Slicemodule 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 valuev
is notundefined
, this method writes an optional value using the given tag and format. The helper functionwriteFn
is called to insert the value inv
. For example, you can passOutputStream.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. CallingstartSize
writes a placeholder value for the size and returns the starting position of the size value; after writing the data, callendSize
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 forsz
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.