org.iperg.part.core
Class IpIoStream

java.lang.Object
  extended by org.iperg.part.core.IpIoStream
All Implemented Interfaces:
IpInputStream, IpOutputStream

public class IpIoStream
extends java.lang.Object
implements IpInputStream, IpOutputStream

The IpIoStream class can be used to create io streams that support encoding and decoding of data to and from a byte stream. The stream use an byte array to store encoded data.

The reason why we don't use a native Java io stream is that we need to control the way in which data is encoded since we can't assume that all PART processes are Java processes.

If a stream has been created via the empty constructor and data has been encoded via the

write

methods, the same stream can then be used to read the data using the

read

methods. For example:
 IpIoStream s = new IpIoStream();
 s.writeInt(5);
 s.writeString("hello");
 // Now read the data encoded into the stream
 int i = s.readInt();
 String str = s.readString(); 

Note that it is not recomennded to mix several writes and reads. Ideally, all writes should be completed before the reads commence.

A stream that has been created using already encoded data can not be used to encode additional data. This will cause an

RuntimeException

to be thrown:
 IpIoStream s = new IpIoStream(data);
 s.writeInt(5); <= will throw an exception

Version:
1.0.0 Initial version
Author:
Olov Stahl - olovs@sics.se

Constructor Summary
IpIoStream()
          Create an empty io stream instance that can be used for encoding data.
IpIoStream(byte[] data)
          Create a io stream that can be used for decoding data and set its content to the given byte array.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this input stream.
static int decodeInt(byte[] buf)
          Convert an array of four bytes into an integer
static void encodeInt(int val, byte[] buf)
          Convert an integer into an array of four bytes and write bytes into given buffer
 byte[] getEncodedData()
          Get the stream "content" (encoded data) as an array of bytes
 boolean readBoolean()
          Reads a boolean.
 byte readByte()
          Reads a byte.
 byte[] readBytes()
          Reads a byte array
 char readChar()
          Reads a 16 bits char.
 double readDouble()
          Reads a double.
 float readFloat()
          Reads a float.
 int readInt()
          Reads an int.
 long readLong()
          Reads a long.
 java.lang.Object readObject()
          Reads an object
 void readObject(java.lang.Object obj)
          Read serialised object data into an existing object.
 short readShort()
          Reads a short.
 java.lang.String readString()
          Reads a string
 void writeBoolean(boolean b)
          Writes a boolean.
 void writeByte(byte b)
          Writes a byte.
 void writeBytes(byte[] data)
          Writes a byte array
 void writeChar(char c)
          Writes a 16 bits char.
 void writeDouble(double d)
          Writes a double.
 void writeFloat(float f)
          Writes a float.
 void writeInt(int i)
          Writes an int.
 void writeLong(long l)
          Writes a long.
 void writeObject(java.lang.Object obj)
          Writes an object
 void writeShort(short s)
          Writes a short.
 void writeString(java.lang.String s)
          Writes a string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IpIoStream

public IpIoStream()
Create an empty io stream instance that can be used for encoding data.


IpIoStream

public IpIoStream(byte[] data)
Create a io stream that can be used for decoding data and set its content to the given byte array.

Parameters:
data - The byte array that will be used as the content of the stream
Method Detail

writeByte

public void writeByte(byte b)
Writes a byte.

Specified by:
writeByte in interface IpOutputStream
Parameters:
b - a byte;

writeChar

public void writeChar(char c)
Writes a 16 bits char.

Specified by:
writeChar in interface IpOutputStream
Parameters:
c - a char;

writeBoolean

public void writeBoolean(boolean b)
Writes a boolean.

Specified by:
writeBoolean in interface IpOutputStream
Parameters:
b - a boolean;

writeShort

public void writeShort(short s)
Writes a short.

Specified by:
writeShort in interface IpOutputStream
Parameters:
s - a short;

encodeInt

public static void encodeInt(int val,
                             byte[] buf)
Convert an integer into an array of four bytes and write bytes into given buffer


decodeInt

public static int decodeInt(byte[] buf)
Convert an array of four bytes into an integer


writeInt

public void writeInt(int i)
Writes an int.

Specified by:
writeInt in interface IpOutputStream
Parameters:
i - an int;

writeLong

public void writeLong(long l)
Writes a long.

Specified by:
writeLong in interface IpOutputStream
Parameters:
l - a long;

writeFloat

public void writeFloat(float f)
Writes a float.

Specified by:
writeFloat in interface IpOutputStream
Parameters:
f - a float;

writeDouble

public void writeDouble(double d)
Writes a double.

Specified by:
writeDouble in interface IpOutputStream
Parameters:
d - a double;

writeString

public void writeString(java.lang.String s)
Writes a string

Specified by:
writeString in interface IpOutputStream
Parameters:
s - a string;

writeBytes

public void writeBytes(byte[] data)
Writes a byte array

Specified by:
writeBytes in interface IpOutputStream
Parameters:
data - The byte array

writeObject

public void writeObject(java.lang.Object obj)
                 throws java.io.IOException
Writes an object

Specified by:
writeObject in interface IpOutputStream
Parameters:
obj - The object
Throws:
java.io.IOException - if an io exception occurs while writing the object to the stream

getEncodedData

public byte[] getEncodedData()
Get the stream "content" (encoded data) as an array of bytes


readByte

public byte readByte()
Reads a byte.

Specified by:
readByte in interface IpInputStream
Returns:
a byte.

readBoolean

public boolean readBoolean()
Reads a boolean.

Specified by:
readBoolean in interface IpInputStream
Returns:
a boolean.

readChar

public char readChar()
Reads a 16 bits char.

Specified by:
readChar in interface IpInputStream
Returns:
a char.

readShort

public short readShort()
Reads a short.

Specified by:
readShort in interface IpInputStream
Returns:
a short.

readInt

public int readInt()
Reads an int.

Specified by:
readInt in interface IpInputStream
Returns:
an int.

readLong

public long readLong()
Reads a long.

Specified by:
readLong in interface IpInputStream
Returns:
a long.

readFloat

public float readFloat()
Reads a float.

Specified by:
readFloat in interface IpInputStream
Returns:
a float.

readDouble

public double readDouble()
Reads a double.

Specified by:
readDouble in interface IpInputStream
Returns:
a double.

readString

public java.lang.String readString()
Reads a string

Specified by:
readString in interface IpInputStream
Returns:
a string.

readBytes

public byte[] readBytes()
Reads a byte array

Specified by:
readBytes in interface IpInputStream
Returns:
a byte array.

readObject

public java.lang.Object readObject()
                            throws java.io.IOException
Reads an object

Specified by:
readObject in interface IpInputStream
Returns:
The object
Throws:
java.io.IOException

readObject

public void readObject(java.lang.Object obj)
                throws java.io.IOException
Read serialised object data into an existing object. The object must implement

Parameters:
obj - The object
Throws:
java.io.IOException

available

public int available()
Returns the number of bytes that can be read from this input stream.

Specified by:
available in interface IpInputStream
Returns:
The number of bytes that can be read from the stream