Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Znav
nextStream Helper Functions in Java
prevThe InputStream Interface in Java

An OutputStream is created using the following function:

Wiki Markup
{zcode:java}
package Ice;

public class Util {
    public static OutputStream createOutputStream(
        Communicator communicator);
}
{zcode}

The OutputStream class is shown below.

Wiki Markup
{zcode:java}
package Ice;

public interface OutputStream {
    Communicator communicator();

    void writeBool(boolean v);
    void writeBoolSeq(boolean[] v);

    void writeByte(byte v);
    void writeByteSeq(byte[] v);

    void writeShort(short v);
    void writeShortSeq(short[] v);

    void writeInt(int v);
    void writeIntSeq(int[] v);

    void writeLong(long v);
    void writeLongSeq(long[] v);

    void writeFloat(float v);
    void writeFloatSeq(float[] v);

    void writeDouble(double v);
    void writeDoubleSeq(double[] v);

    void writeString(String v);
    void writeStringSeq(String[] v);

    void writeSize(int sz);

    void writeProxy(ObjectPrx v);

    void writeObject(Ice.Object v);

    void writeTypeId(String id);

    void writeException(UserException ex);

    void startSlice();
    void endSlice();

    void startEncapsulation();
    void endEncapsulation();

    void writePendingObjects();

    byte[] finished();

    void reset(boolean clearBuffer);

    void writeSerializable(java.io.Serializable o);

    void destroy();
}
{zcode}

Member functions are provided for inserting all of the primitive types, as well as sequences of primitive types; these are self-explanatory. The remaining member functions have the following semantics:

  • void writeSize(int sz)
    The Ice encoding has a compact representation to indicate size. This function converts the given non-negative integer into the proper encoded representation.
  • void writeProxy(Ice.ObjectPrx v)
    Inserts a proxy.
  • void writeObject(Ice.Object v)
    Inserts an Ice object. The Ice encoding for class instances may cause the insertion of this object to be delayed, in which case the stream retains a reference to the given object and does not insert its state it until writePendingObjects is invoked on the stream.
  • void writeTypeId(String id)
    A table of Slice type IDs is used to save space when encoding Ice objects. This function adds the given type ID to the table and encodes the type ID. writeTypeId may only be invoked in the context of a call to writePendingObjects (see below).
  • void startSlice()
    void endSlice()
    Starts and ends a slice of object or exception member data.
  • void startEncapsulation()
    void endEncapsulation()
    Starts and ends an encapsulation, respectively.
  • void writePendingObjects()
    Encodes the state of Ice objects whose insertion was delayed during writeObject. This member function must only be called once.
  • byte[] finished()
    Indicates that marshaling is complete and returns the encoded byte sequence. This member function must only be called once.
  • void reset(boolean clearBuffer)
    Resets the writing position of the stream to the beginning. The boolean argument clearBuffer determines whether the stream releases the internal buffer it allocated to hold the encoded data. If clearBuffer is true, the stream releases the buffer in order to make it eligible for garbage collection. If clearBuffer is false, the stream retains the buffer to avoid generating unnecessary garbage.
  • void destroy()
    Applications must call this function in order to reclaim resources.

Here is a simple example that demonstrates how to insert a boolean and a sequence of strings into a stream:

Wiki Markup
{zcode:java}
final String[] seq = { "Ice", "rocks!" };
Ice.OutputStream out = Ice.Util.createOutputStream(communicator);
try {
    out.writeBool(true);
    out.writeStringSeq(seq);
    byte[] data = out.finished();
} finally {
    out.destroy();
}
{zcode}
Ztop
See Also
Zret
Znav
nextStream Helper Functions in Java
prevThe InputStream Interface in Java