Package totalcross.io
Class DataStreamLE
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.DataStream
-
- totalcross.io.DataStreamLE
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
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 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
Constructors Constructor Description DataStreamLE(Stream stream)Constructs a new DataStreamLE which sits upon the given stream using litle endian notation for multibyte values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description charreadChar()Reads a two-byte character.voidreadChars(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.intreadInt()Reads an integer from the stream as four bytes.longreadLong()Reads a long.shortreadShort()Reads a short from the stream as two bytes.intreadUnsignedShort()Reads an unsigned short from the stream as two bytes.intwriteChar(char c)Writes a two-byte character.intwriteChars(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).intwriteChars(java.lang.String s, int len)Writes the String as a char array.intwriteInt(int i)Writes an integer to the stream as four bytes.intwriteLong(long l)Writes a long.intwriteShort(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, readStringArray, readUnsignedByte, readUnsignedInt, skip, writeBigChars, writeBigString, writeBoolean, writeByte, writeByte, writeBytes, writeBytes, writeBytesInternal, writeChars, writeCString, writeDouble, writeFixedString, writeFixedString, writeFloat, writeObject, writeSmallString, writeSmallString8, writeString, writeStringArray
-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes
-
-
-
-
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 EOFException, IOExceptionDescription copied from class:DataStreamReads an integer from the stream as four bytes. The returned value will range from -2147483648 to 2147483647.- Overrides:
readIntin classDataStream- Returns:
- the integer value
- Throws:
EOFExceptionIOException
-
readShort
public short readShort() throws EOFException, IOExceptionDescription copied from class:DataStreamReads a short from the stream as two bytes. The returned value will range from -32768 to 32767.- Overrides:
readShortin classDataStream- Returns:
- the short value
- Throws:
EOFExceptionIOException
-
readLong
public long readLong() throws EOFException, IOExceptionDescription copied from class:DataStreamReads a long.- Overrides:
readLongin classDataStream- Returns:
- the long value
- Throws:
EOFExceptionIOException
-
readUnsignedShort
public int readUnsignedShort() throws EOFException, IOExceptionDescription copied from class:DataStreamReads 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:
readUnsignedShortin classDataStream- Returns:
- the short
- Throws:
EOFExceptionIOException- See Also:
DataStream.writeShort(int)
-
writeInt
public int writeInt(int i) throws IOExceptionDescription copied from class:DataStreamWrites an integer to the stream as four bytes.- Overrides:
writeIntin classDataStream- Parameters:
i- the integer to write- Returns:
- the number of bytes written: 4
- Throws:
IOException
-
writeShort
public int writeShort(int i) throws IOExceptionDescription copied from class:DataStreamWrites a short to the stream as two bytes.- Overrides:
writeShortin classDataStream- Parameters:
i- the short to write- Returns:
- the number of bytes written: 2
- Throws:
IOException
-
writeLong
public int writeLong(long l) throws IOExceptionDescription copied from class:DataStreamWrites a long.- Overrides:
writeLongin classDataStream- Throws:
IOException
-
readChar
public char readChar() throws EOFException, IOExceptionDescription copied from class:DataStreamReads a two-byte character.- Overrides:
readCharin classDataStream- Throws:
EOFExceptionIOException
-
writeChar
public int writeChar(char c) throws IOExceptionDescription copied from class:DataStreamWrites a two-byte character.- Overrides:
writeCharin classDataStream- 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 EOFException, IOExceptionReads a char array with the given length.- Overrides:
readCharsin classDataStream- Parameters:
chars- An already created chars array.len- The array length.- Throws:
EOFExceptionIOException- Since:
- TotalCross 1.01
-
readChars
protected char[] readChars(int len) throws EOFException, IOExceptionDescription copied from class:DataStreamReads a char array with the given length.- Overrides:
readCharsin classDataStream- Returns:
- Throws:
EOFExceptionIOException
-
writeChars
public int writeChars(char[] chars, int start, int len, int lenSize) throws IOExceptionDescription copied from class:DataStreamWrites 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:
writeCharsin classDataStream- Throws:
IOException
-
writeChars
public int writeChars(java.lang.String s, int len) throws IOExceptionDescription copied from class:DataStreamWrites the String as a char array. The chars are read using the charAt method from the String class. This 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:
writeCharsin classDataStream- Parameters:
s- The 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
-
-