totalcross.io
Class DataStreamLE

totalcross.lang.Object
  extended by totalcross.io.Stream
      extended by totalcross.io.DataStream
          extended by totalcross.io.DataStreamLE

public class DataStreamLE
extends DataStream

DataStreamLE can be attached to any Stream such as a PortConnector, PDBFile, or ByteArrayStream, which lets you read and write standard Java data types like int, double, and totalcross.lang.String in a simple manner. Here's an example:

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

Important!: All methods read and write numeric data in the little endian format (Intel format). For more information, see this.

See Also:
DataStream

Field Summary
 
Fields inherited from class totalcross.io.DataStream
buffer, EOSMessage, stream, zeros
 
Fields inherited from class totalcross.io.Stream
skipBuffer
 
Constructor Summary
DataStreamLE(Stream stream)
          Constructs a new DataStreamLE which sits upon the given stream using litle endian notation for multibyte values.
 
Method Summary
 char readChar()
          Reads a two-byte character.
 void readChars(char[] chars, int len)
          Reads a char array with the given length.
protected  char[] readChars(int len)
          Reads a char array with the given length.
 int readInt()
          Reads an integer from the stream as four bytes.
 long readLong()
          Reads a long.
 short readShort()
          Reads a short from the stream as two bytes.
 int readUnsignedShort()
          Reads an unsigned short from the stream as two bytes.
 int writeChar(char c)
          Writes a two-byte character.
 int writeChars(char[] chars, int start, int len, int lenSize)
          Writes the given char array, writting the length as an int or as a short or as a byte or don't writting the length, depending on the number of bytes given (4,2,1,0).
 int writeChars(String s, int len)
          Writes the totalcross.lang.String as a char array.
 int writeInt(int i)
          Writes an integer to the stream as four bytes.
 int writeLong(long l)
          Writes a long.
 int writeShort(int i)
          Writes a short to the stream as two bytes.
 
Methods inherited from class totalcross.io.DataStream
close, getStream, pad, readBigChars, readBigString, readBoolean, readByte, readBytes, readBytes, readBytesInternal, readChars, readChars, readCString, readDouble, readFixedString, readFloat, readObject, readSmallString, readString, readStringArray, readUnsignedByte, skip, writeBigChars, writeBigString, writeBoolean, writeByte, writeByte, writeBytes, writeBytes, writeBytes, writeChars, writeCString, writeDouble, writeFixedString, writeFixedString, writeFloat, writeObject, writeSmallString, writeString, writeStringArray
 
Methods inherited from class totalcross.io.Stream
skipBytes
 
Methods inherited from class totalcross.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataStreamLE

public DataStreamLE(Stream stream)
Constructs a new DataStreamLE which sits upon the given stream using litle endian notation for multibyte values.

Parameters:
stream - the base stream
Method Detail

readInt

public int readInt()
            throws IOException
Description copied from class: DataStream
Reads an integer from the stream as four bytes. The returned value will range from -2147483648 to 2147483647.

Overrides:
readInt in class DataStream
Returns:
the integer value
Throws:
IOException

readShort

public short readShort()
                throws IOException
Description copied from class: DataStream
Reads a short from the stream as two bytes. The returned value will range from -32768 to 32767.

Overrides:
readShort in class DataStream
Returns:
the short value
Throws:
IOException

readLong

public long readLong()
              throws IOException
Description copied from class: DataStream
Reads a long.

Overrides:
readLong in class DataStream
Returns:
the long value
Throws:
IOException

readUnsignedShort

public int readUnsignedShort()
                      throws IOException
Description copied from class: DataStream
Reads an unsigned short from the stream as two bytes. The returned value will range from 0 to 65535. Use writeShort to write the unsigned short.

Overrides:
readUnsignedShort in class DataStream
Returns:
the short
Throws:
IOException
See Also:
DataStream.writeShort(int)

writeInt

public int writeInt(int i)
             throws IOException
Description copied from class: DataStream
Writes an integer to the stream as four bytes.

Overrides:
writeInt in class DataStream
Parameters:
i - the integer to write
Returns:
the number of bytes written: 4
Throws:
IOException

writeShort

public int writeShort(int i)
               throws IOException
Description copied from class: DataStream
Writes a short to the stream as two bytes.

Overrides:
writeShort in class DataStream
Parameters:
i - the short to write
Returns:
the number of bytes written: 2
Throws:
IOException

writeLong

public int writeLong(long l)
              throws IOException
Description copied from class: DataStream
Writes a long.

Overrides:
writeLong in class DataStream
Throws:
IOException

readChar

public char readChar()
              throws IOException
Description copied from class: DataStream
Reads a two-byte character.

Overrides:
readChar in class DataStream
Throws:
IOException

writeChar

public int writeChar(char c)
              throws IOException
Description copied from class: DataStream
Writes a two-byte character.

Overrides:
writeChar in class DataStream
Parameters:
c - the character to be written.
Returns:
the number of bytes written: 2
Throws:
IOException

readChars

public void readChars(char[] chars,
                      int len)
               throws IOException
Reads a char array with the given length.

Overrides:
readChars in class DataStream
Parameters:
chars - An already created chars array.
len - The array length.
Throws:
IOException
Since:
TotalCross 1.01

readChars

protected char[] readChars(int len)
                    throws IOException
Description copied from class: DataStream
Reads a char array with the given length.

Overrides:
readChars in class DataStream
Throws:
IOException

writeChars

public int writeChars(char[] chars,
                      int start,
                      int len,
                      int lenSize)
               throws IOException
Description copied from class: DataStream
Writes the given char array, writting the length as an int or as a short or as a byte or don't writting the length, depending on the number of bytes given (4,2,1,0).

Overrides:
writeChars in class DataStream
Throws:
IOException

writeChars

public int writeChars(String s,
                      int len)
               throws IOException
Description copied from class: DataStream
Writes the totalcross.lang.String as a char array. The chars are read using the charAt method from the totalcross.lang.String class. This is method is faster than the other writeChars method on blackberry, but slower on other devices.

The char array size is limited to 65535 characters.

Overrides:
writeChars in class DataStream
Parameters:
s - The totalcross.lang.String to be written. Must not be null!
len - The maximum number of chars to be written. Must be less than the String's length.
Throws:
IOException