|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
totalcross.lang.Objecttotalcross.io.Stream
totalcross.io.DataStream
public class DataStream
DataStream 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();
DataStreamLE| Field Summary | |
|---|---|
protected byte[] |
buffer
a four byte array for reading and writing numbers |
static String |
EOSMessage
Message thrown when an end of stream is reached when reading. |
protected Stream |
stream
The underlying stream, from where bytes are read and written. |
protected static byte[] |
zeros
|
| Fields inherited from class totalcross.io.Stream |
|---|
skipBuffer |
| Constructor Summary | |
|---|---|
DataStream(Stream stream)
Constructs a new DataStream which sits upon the given stream using big endian notation for multibyte values. |
|
| Method Summary | |
|---|---|
void |
close()
Closes the stream. |
Stream |
getStream()
Returns the base stream attached to this stream. |
int |
pad(int n)
Pads the stream writing 0 the given number of times. |
char[] |
readBigChars()
Reads an array of chars, where its length is stored in the first four bytes as an int. |
String |
readBigString()
Reads a big string, converting from Pascal (the length is placed before the string) to Java format. |
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. |
protected void |
readBytesInternal(byte[] buf,
int start,
int count)
|
char |
readChar()
Reads a two-byte character. |
char[] |
readChars()
Reads an array of chars. |
void |
readChars(char[] chars,
int len)
Reads a char array with the given length. |
int |
readChars(char[] chars,
int start,
int count)
Reads 'count' chars from the stream to the given char array, starting from index 'start'. |
protected char[] |
readChars(int len)
Reads a char array with the given length. |
String |
readCString()
Reads a C-style string from the stream. |
double |
readDouble()
Reads a double. |
String |
readFixedString(int length)
Reads a fixed length string from the stream. |
double |
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. |
long |
readLong()
Reads a long. |
Object |
readObject()
Read a Storable object. |
short |
readShort()
Reads a short from the stream as two bytes. |
String |
readSmallString()
Read a small String. |
String |
readString()
Reads a string, converting from Pascal (the length is placed before the string) to Java format. |
String[] |
readStringArray()
Reads an array of Strings. |
int |
readUnsignedByte()
Reads a single unsigned byte from the stream. |
int |
readUnsignedShort()
Reads an unsigned short from the stream as two bytes. |
int |
skip(int n)
Deprecated. Use skipBytes instead. |
int |
writeBigChars(char[] chars,
int start,
int len)
Writes an array of chars, placing its length in the first four bytes, as an int. |
int |
writeBigString(String s)
Writes the string into the stream, converting it from Java format to Pascal format (the length is placed before the string). |
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 |
writeBytes(String str)
Writes the given totalcross.lang.String as a byte array, retrieved using String.getBytes. |
int |
writeChar(char c)
Writes a two-byte character. |
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 |
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 |
writeCString(String s)
Writes a C-style string to the stream. |
int |
writeDouble(double d)
Writes a double. |
void |
writeFixedString(String s,
int length)
Writes a fixed length string to the stream. |
void |
writeFixedString(String s,
int length,
char pad)
Writes a fixed length string to the stream. |
int |
writeFloat(double 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 |
writeLong(long l)
Writes a long. |
void |
writeObject(Storable s)
Write the given totalcross.lang.Object using the Storable class. |
int |
writeShort(int i)
Writes a short to the stream as two bytes. |
int |
writeSmallString(String s)
Write a small totalcross.lang.String comprised of only ASCII chars (each char is casted to byte). |
int |
writeString(String s)
Writes the string into the stream, converting it from Java format to Pascal format (the length is placed before the string). |
int |
writeStringArray(String[] v)
writes the string array into the stream |
| 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 |
| Field Detail |
|---|
protected Stream stream
protected byte[] buffer
protected static byte[] zeros
public static final String EOSMessage
| Constructor Detail |
|---|
public DataStream(Stream stream)
stream - the base stream, from where bytes are read and written.| Method Detail |
|---|
public void close()
throws IOException
IOException
IOException
public final int pad(int n)
throws IOException
n - The number of zeros to be written
IOException
public final boolean readBoolean()
throws IOException
IOException
public final byte readByte()
throws IOException
IOException
public final int readBytes(byte[] buf,
int start,
int count)
throws IOException
Stream
readBytes in class Streambuf - the byte array to read data intostart - the start position in the arraycount - the number of bytes to read
IOException
public final int readBytes(byte[] buf)
throws IOException
buf - the byte array to read data into
IOException - If an error prevented the read operation from occurring.
public double readFloat()
throws IOException
IOException
public int readInt()
throws IOException
IOException
public short readShort()
throws IOException
IOException
public double readDouble()
throws IOException
IOException
public long readLong()
throws IOException
IOException
public final int readUnsignedByte()
throws IOException
IOExceptionwriteByte(int)
public int readUnsignedShort()
throws IOException
IOExceptionwriteShort(int)
public final int writeBoolean(boolean bool)
throws IOException
bool - the boolean to write
IOException
public final int writeByte(byte by)
throws IOException
by - the byte to write
IOException
public final int writeByte(int by)
throws IOException
by - the byte to write (only least significant byte is written)
IOException
public final int writeBytes(byte[] buf,
int start,
int count)
throws IOException
Stream
writeBytes in class Streambuf - the byte array to write data fromstart - the start position in the byte arraycount - the number of bytes to write
IOException
public final int writeBytes(byte[] buf)
throws IOException
writeBytes in class Streambuf - the byte array to write data from
IOException
public final int writeBytes(String str)
throws IOException
writeBytes in class Streamstr - the totalcross.lang.String to get the bytes from
IOException
public int writeFloat(double f)
throws IOException
f - the float to write
IOException
public int writeInt(int i)
throws IOException
i - the integer to write
IOException
public int writeShort(int i)
throws IOException
i - the short to write
IOException
public String readString()
throws IOException
The totalcross.lang.String size is limited to 65535 characters.
IOException
public String readBigString()
throws IOException
The totalcross.lang.String size is limited to 2,147,483,647 characters.
IOException
public String[] readStringArray()
throws IOException
The array length is limited to 65535 elements.
IOException
public int writeString(String s)
throws IOException
The totalcross.lang.String size is limited to 65535 characters.
IOException
public int writeBigString(String s)
throws IOException
The totalcross.lang.String size is limited to 2,147,483,647 characters.
IOException
public int writeStringArray(String[] v)
throws IOException
IOException
public int writeDouble(double d)
throws IOException
IOException
public int writeLong(long l)
throws IOException
IOException
public String readCString()
throws IOException
IOException
public final int writeCString(String s)
throws IOException
s - the string to write
IOException
public char readChar()
throws IOException
IOException
public int writeChar(char c)
throws IOException
c - the character to be written.
IOException
public char[] readChars()
throws IOException
The char array size is limited to 65535 characters.
IOException
public void readChars(char[] chars,
int len)
throws IOException
chars - An already created chars array.len - The array length.
IOException
public int readChars(char[] chars,
int start,
int count)
throws IOException
chars - a char arraystart - starting position on the char arraycount - number of chars to be read from the stream
IOException
public char[] readBigChars()
throws IOException
The char array size is limited to 2,147,483,647 characters.
IOException
protected char[] readChars(int len)
throws IOException
IOException
public int writeChars(char[] chars,
int start,
int len)
throws IOException
The char array size is limited to 65535 characters.
len - the length to be written or -1 if it is to write the whole char arraychars - the char array to be written.start - the starting index (in most cases: 0).
IOException
public int writeBigChars(char[] chars,
int start,
int len)
throws IOException
The char array size is limited to 2,147,483,647 characters.
len - the length to be written or -1 if it is to write the whole char arraychars - the char array to be written.start - the starting index (in most cases: 0).
IOException
public int writeChars(char[] chars,
int start,
int len,
int lenSize)
throws IOException
IOException
public int writeChars(String s,
int len)
throws IOException
The char array size is limited to 65535 characters.
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.
IOExceptionpublic Stream getStream()
public String readFixedString(int length)
throws IOException
length - the number of characters to read
IOException
public void writeFixedString(String s,
int length)
throws IOException
s - the string to writelength - the length of the fixed string
IOException
public void writeFixedString(String s,
int length,
char pad)
throws IOException
s - the string to writelength - the length of the fixed stringpad - the character to pad if the string is shorter than the length
IOException
public void writeObject(Storable s)
throws IOException
IOExceptionStorable
public Object readObject()
throws totalcross.lang.ClassNotFoundException,
totalcross.lang.InstantiationException,
totalcross.lang.IllegalAccessException,
IOException
totalcross.lang.ClassNotFoundException
totalcross.lang.InstantiationException
totalcross.lang.IllegalAccessException
IOExceptionStorable
public String readSmallString()
throws IOException
The totalcross.lang.String size is limited to 255 characters.
IOException
public int writeSmallString(String s)
throws IOException
The totalcross.lang.String size is limited to 255 characters.
IOException
protected void readBytesInternal(byte[] buf,
int start,
int count)
throws IOException
IOException
public int skip(int n)
throws IOException
IOException
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||