waba.io
Class DataStream

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.DataStream

public class DataStream
extends Stream

DataStream is a wrapper you can place around any Stream such as a SerialPort, Catalog, or BufferStream which lets you read and write standard Waba data types like ints, floats, and Strings in a simple manner. Here's an example

   SerialPort port=new SerialPort(9600,0);
   DataStream ds=new DataStream(port);
   ds.writeString("Hello");
   int status=ds.readUnsignedByte();
   if (status==1)
   {
     ds.writeString("Pi");
     ds.writeFloat(3.14);
   }
   port.close();
 

ps: this class was created in the Waba Extras package, but i added a few methods for storing and retrieving strings. (guich@email.com)


Field Summary
protected  byte[] b
          a four byte array for reading and writing numbers
protected  Stream stream
          the underlying stream
 
Constructor Summary
DataStream(Stream stream)
          Constructs a new DataStream which sits upon the given stream using big endian notation for multibyte values.
 
Method Summary
 boolean close()
          closes the stream
 int pad(int n)
          pads the stream writting n bytes. all bytes will be 0. added by guich
 boolean readBoolean()
          Reads a boolean from the stream as a byte.
 byte readByte()
          Reads a single byte from the stream.
 int readBytes(byte[] buf, int start, int count)
          Reads bytes from the stream.
 float readFloat()
          Reads a float value from the stream as four bytes in IEEE 754 format.
 int readInt()
          Reads an integer from the stream as four bytes.
 short readShort()
          Reads a short from the stream as two bytes.
 java.lang.String readString()
          reads an string.
 java.lang.String[] readStringArray()
          reads an array of string.
 int readUnsignedByte()
          Reads a single unsigned byte from the stream.
 int readUnsignedShort()
          Reads an unsigned short from the stream as two bytes.
 void skip(int n)
          Skips reading the next n bytes in the stream
 int writeBoolean(boolean bool)
          Writes a boolean to the stream as a byte.
 int writeByte(byte by)
          Writes a single byte to the stream.
 int writeByte(int by)
          Writes a single byte to the stream.
 int writeBytes(byte[] buf, int start, int count)
          Writes bytes to the the stream.
 int writeFloat(float f)
          Writes a float value to the stream as four bytes in IEEE 754 format
 int writeInt(int i)
          Writes an integer to the stream as four bytes.
 int writeShort(int i)
          Writes an short to the stream as two bytes.
 int writeString(java.lang.String s)
          writes the string into the stream
 int writeStringArray(java.lang.String[] v)
          writes the string array into the stream
 
Methods inherited from class java.lang.Object
hashCode, toString
 

Field Detail

stream

protected Stream stream
the underlying stream

b

protected byte[] b
a four byte array for reading and writing numbers
Constructor Detail

DataStream

public DataStream(Stream stream)
Constructs a new DataStream which sits upon the given stream using big endian notation for multibyte values.
Parameters:
stream - the base stream
Method Detail

close

public boolean close()
closes the stream
Overrides:
close in class Stream

readInt

public int readInt()
Reads an integer from the stream as four bytes. The returned value will range from -2147483648 to 2147483647.
Returns:
the integer

writeInt

public int writeInt(int i)
Writes an integer to the stream as four bytes.
Parameters:
i - the integer to write
Returns:
the number of bytes written

readShort

public short readShort()
Reads a short from the stream as two bytes. The returned value will range from -32768 to 32767.
Returns:
the short

writeShort

public int writeShort(int i)
Writes an short to the stream as two bytes. As there is no short type in Waba but we often only want to only use two bytes in storage. an int is used but the upper two bytes are ignored.
Parameters:
i - the short to write
Returns:
the number of bytes written

readUnsignedShort

public int readUnsignedShort()
Reads an unsigned short from the stream as two bytes. The returned value will range from 0 to 65535.
Returns:
the short

readFloat

public float readFloat()
Reads a float value from the stream as four bytes in IEEE 754 format.
Returns:
the float value

writeFloat

public int writeFloat(float f)
Writes a float value to the stream as four bytes in IEEE 754 format
Parameters:
f - the float to write
Returns:
the number of bytes written

readBoolean

public boolean readBoolean()
Reads a boolean from the stream as a byte. True is returned if the byte is not zero, false if it is.

writeBoolean

public int writeBoolean(boolean bool)
Writes a boolean to the stream as a byte. True values are written as 1 and false values as 0.
Parameters:
b - the boolean to write
Returns:
the number of bytes written

readByte

public byte readByte()
Reads a single byte from the stream. The returned value will range from -128 to 127.

writeByte

public int writeByte(byte by)
Writes a single byte to the stream.
Parameters:
b - the byte to write
Returns:
the number of bytes written

writeByte

public int writeByte(int by)
Writes a single byte to the stream.
Parameters:
b - the byte to write (only least significant byte is written)
Returns:
the number of bytes written

readUnsignedByte

public int readUnsignedByte()
Reads a single unsigned byte from the stream. The returned value will range from 0 to 255.

skip

public void skip(int n)
Skips reading the next n bytes in the stream
Parameters:
n - the number of bytes to skip

readBytes

public int readBytes(byte[] buf,
                     int start,
                     int count)
Reads bytes from the stream. Returns the number of bytes actually read or -1 if an error prevented the read operation from occurring.
Overrides:
readBytes in class Stream
Parameters:
buf - the byte array to read data into
start - the start position in the array
count - the number of bytes to read

writeBytes

public int writeBytes(byte[] buf,
                      int start,
                      int count)
Writes bytes to the the stream. Returns the number of bytes actually written or -1 if an error prevented the write operation from occurring.
Overrides:
writeBytes in class Stream
Parameters:
buf - the byte array to write data from
start - the start position in the byte array
count - the number of bytes to write

pad

public int pad(int n)
pads the stream writting n bytes. all bytes will be 0. added by guich

readString

public java.lang.String readString()
reads an string.

readStringArray

public java.lang.String[] readStringArray()
reads an array of string.

writeStringArray

public int writeStringArray(java.lang.String[] v)
writes the string array into the stream

writeString

public int writeString(java.lang.String s)
writes the string into the stream