org.instantreality.InstantIO
Class Encoder

java.lang.Object
  extended by org.instantreality.InstantIO.Encoder

public abstract class Encoder
extends java.lang.Object

Abstract superclass of all classes that encode data values.

The NetworkNode class uses encoders to encode values before they are sent to other components on the network. Before any data type can be transferred via the network, an Encoder has to be written that transforms the data values to a system and hardware independent byte stream. For some frequently used data types, encoders already exist in this package, but when you need to transfer your own data types, you have to implement an Encoder.

For example, let's say you want to transfer Integer values via the network (an Encoder for Integer of course already exists in this package, but for the sake of demonstrating the procedure whe nevertheless demonstrate how to create your own). First, you have to create a class that inherits from the Encoder class and implements the abstract encode method:

 public class MyIntEncoder extends Encoder
 {
    public final void encode(DataOutputStream dos, Object value)
    throws IOException
    {
      Integer i = (Integer)value;
      dos.writeInt(i.intValue());
    }
 }
 
Then, you have to create a corresponding Decoder class (see the documentation of this class for more information about how to do that). Finally, you have to create an instance of the Codec class so that the NetworkNode can actually access your Encoder (see documentation of the Codec class).

Author:
Patrick Dähne
See Also:
NetworkNode, Decoder, Codec

Constructor Summary
protected Encoder()
          Creates a new Encoder object.
 
Method Summary
abstract  void encode(java.io.DataOutputStream dos, java.lang.Object value)
          Encodes objects.
 void initialize(java.lang.String label, java.lang.Class type, java.lang.String description)
          Initializes the Encoder object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Encoder

protected Encoder()
Creates a new Encoder object.

Method Detail

initialize

public void initialize(java.lang.String label,
                       java.lang.Class type,
                       java.lang.String description)
Initializes the Encoder object.

Parameters:
label - The label of the InSlot
type - The type of the InSlot
description - The description of the InSlot

encode

public abstract void encode(java.io.DataOutputStream dos,
                            java.lang.Object value)
                     throws java.io.IOException
Encodes objects. This method encodes objects into their binary representation and writes this binary representation into an output stream. This method needs to be overwritten by your own implementations.

Parameters:
dos - The output stream you write the system and hardware independent byte representation to.
value - The object you have to encode.
Throws:
java.io.IOException - when the conversion fails (should never happen)