waba.io
Class DataStream

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.DataStream
Direct Known Subclasses:
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 SuperWaba 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 to the Waba Extras package, but SW added a few methods for storing and retrieving strings.


Field Summary
protected  byte[] b
          a four byte array for reading and writing numbers
protected static byte[] bytes
           
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
 boolean isOpen()
          Returns true if this Stream is open and false otherwise
 int pad(int n)
          pads the stream writting n bytes. all bytes will be 0.
 boolean readBoolean()
          Reads a boolean from the stream as a byte.
 byte readByte()
          Reads a single byte from the stream.
 int readBytes(byte[] buf)
          Reads bytes from the stream.
 int readBytes(byte[] buf, int start, int count)
          Reads bytes from the stream.
 char[] readChars()
          Reads an array of chars, where its length is stored in the first two bytes as an unsigned short
 String readCString()
          Reads a C-style string from the stream.
 double readDouble()
          Reads a double.
 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.
 int readIntLE()
          Reads an integer using little endian from the stream as four bytes.
 long readLong()
          Reads a long.
 short readShort()
          Reads a short from the stream as two bytes.
 short readShortLE()
          Reads a short from the stream as two bytes in the little endian format.
 String readString()
          reads a string, converting from palm to java format.
 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 (only forward!)
 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)
          Writes bytes to the stream.
 int writeBytes(byte[] buf, int start, int count)
          Writes bytes to the stream.
 int writeChars(char[] chars, int start, int len)
          Writes an array of chars, placing its length in the first two bytes, as an unsigned short.
 int writeCString(String s)
          Writes a C-style string to the stream.
 int writeDouble(double d)
          writes a double.
 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 writeIntLE(int i)
          Writes an integer to the stream as four bytes.
 int writeLong(long l)
          writes a long.
 int writeShort(int i)
          Writes an short to the stream as two bytes.
 int writeShortLE(int i)
          Writes a short to the stream as two bytes.
 int writeString(String s)
          writes the string into the stream, converting it from international format to palm format
 int writeStringArray(String[] v)
          writes the string array into the stream
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, toString, wait, wait
 

Field Detail

stream

protected Stream stream
the underlying stream

b

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

bytes

protected static byte[] bytes
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

pad

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

skip

public final void skip(int n)
Skips reading the next n bytes in the stream (only forward!)
Parameters:
n - the number of bytes to skip, must be > 0

readBoolean

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

readByte

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

readBytes

public final 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

readBytes

public final int readBytes(byte[] buf)
Reads bytes from the stream. Returns the number of bytes actually read or -1 if an error prevented the read operation from occurring.
Parameters:
buf - the byte array to read data into

readFloat

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

readIntLE

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

readShortLE

public final short readShortLE()
Reads a short from the stream as two bytes in the little endian format. The returned value will range from -32768 to 32767.
Returns:
the short

readInt

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

readShort

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

readDouble

public final double readDouble()
Reads a double.
Since:
2.0

readLong

public final long readLong()
Reads a long.
Since:
2.0

readUnsignedByte

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

readUnsignedShort

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

writeIntLE

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

writeShortLE

public final int writeShortLE(int i)
Writes a short to the stream as two bytes. As there is no short type in SuperWaba but we often only want to only use two bytes in storage. An int is used but the upper two bytes are ignored, so this method can be used to store a signed or an unsigned short.
Parameters:
i - the short to write
Returns:
the number of bytes written

writeBoolean

public final 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

writeByte

public final 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 final 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

writeBytes

public final int writeBytes(byte[] buf,
                            int start,
                            int count)
Writes bytes to 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

writeBytes

public final int writeBytes(byte[] buf)
Writes bytes to the stream. Returns the number of bytes actually written or -1 if an error prevented the write operation from occurring.
Parameters:
buf - the byte array to write data from

writeFloat

public final 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

writeInt

public final 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

writeShort

public final int writeShort(int i)
Writes an short to the stream as two bytes. As there is no short type in SuperWaba 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

readString

public final String readString()
reads a string, converting from palm to java format.
Returns:
a zero or more length string. null is never returned.

readStringArray

public final String[] readStringArray()
reads an array of string.
Returns:
a zero or more length array. null is never returned.

writeString

public final int writeString(String s)
writes the string into the stream, converting it from international format to palm format

writeStringArray

public final int writeStringArray(String[] v)
writes the string array into the stream

writeDouble

public final int writeDouble(double d)
writes a double.
Since:
2.0

writeLong

public final int writeLong(long l)
writes a long.
Since:
2.0

readCString

public String readCString()
Reads a C-style string from the stream. This is a NUL (0) terminated series of characters. This format is commonly used by other Palm applications. Note that if you're creating your own stream, choose readString instead of readCString, because readCString is *much* slower. Also, this method does not handle correctly international characters.
Returns:
the loaded String

writeCString

public final int writeCString(String s)
Writes a C-style string to the stream. This means that all the characters of the string are written out, followed by a NUL (0) character. This format is commonly used by other Palm applications. Note that if you're creating your own stream, choose writeString instead of writeCString, because writeCString is *much* slower. Also, this method does not handle correctly international characters.
Parameters:
s - the string to write
Returns:
the number of bytes written.

isOpen

public boolean isOpen()
Description copied from class: Stream
Returns true if this Stream is open and false otherwise
Overrides:
isOpen in class Stream

readChars

public final char[] readChars()
Reads an array of chars, where its length is stored in the first two bytes as an unsigned short
Since:
SuperWaba 3.5

writeChars

public final int writeChars(char[] chars,
                            int start,
                            int len)
Writes an array of chars, placing its length in the first two bytes, as an unsigned short.
Parameters:
len - the length to be written or -1 if it is to write the whole char array
chars - the char array to be written.
start - the starting index (in most cases: 0).
Since:
SuperWaba 3.5