public class DataStream extends Stream
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
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer
a four byte array for reading and writing numbers
|
static java.lang.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 |
skipBuffer
Constructor and Description |
---|
DataStream(Stream stream)
Constructs a new DataStream which sits upon the given stream using big
endian notation for multibyte values.
|
DataStream(Stream stream,
boolean ensureWrite)
Constructs a new DataStream which sits upon the given stream using big endian notation for multibyte values.
|
Modifier and Type | Method and Description |
---|---|
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.
|
java.lang.String |
readBigString()
Reads a big string, converting from Pascal (the length is placed before the
string) to Java format.
|
boolean |
readBoolean()
Reads a byte from the stream as a boolean.
|
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 int |
readBytesInternal(byte[] buf,
int start,
int count,
boolean throwEOF)
Blocks until the requested number of bytes is read from the underlying stream, only returning less than count if
the end of the stream is reached.
|
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.
|
java.lang.String |
readCString()
Reads a C-style string from the stream.
|
double |
readDouble()
Reads a double.
|
java.lang.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.
|
java.lang.Object |
readObject()
Read a Storable object.
|
short |
readShort()
Reads a short from the stream as two bytes.
|
java.lang.String |
readSmallString()
Read a small String.
|
java.lang.String |
readString()
Reads a string, converting from Pascal (the length is placed before the
string) to Java format.
|
java.lang.String[] |
readStringArray()
Reads an array of Strings.
|
java.lang.String[] |
readStringArray(int size)
Reads an array of Strings.
|
int |
readUnsignedByte()
Reads a single unsigned byte from the stream.
|
long |
readUnsignedInt()
Reads an unsigned integer from the stream as four bytes.
The returned value will range from 0 to 4294967295. |
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(java.lang.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.
|
protected int |
writeBytesInternal(byte[] buf,
int start,
int count) |
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(java.lang.String s,
int len)
Writes the String as a char array.
|
int |
writeCString(java.lang.String s)
Writes a C-style string to the stream.
|
int |
writeDouble(double d)
Writes a double.
|
void |
writeFixedString(java.lang.String s,
int length)
Writes a fixed length string to the stream.
|
void |
writeFixedString(java.lang.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 Object using the Storable class.
|
int |
writeShort(int i)
Writes a short to the stream as two bytes.
|
int |
writeSmallString(java.lang.String s)
Write a small String comprised of only ASCII chars (each char is casted to byte).
|
int |
writeSmallString8(java.lang.String s)
Write a small String taking each char as a byte.
|
int |
writeString(java.lang.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(java.lang.String[] v)
writes the string array into the stream
|
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes
protected Stream stream
protected byte[] buffer
protected static byte[] zeros
public static final java.lang.String EOSMessage
public DataStream(Stream stream)
stream
- the base stream, from where bytes are read and written.public DataStream(Stream stream, boolean ensureWrite)
stream
- the base stream, from where bytes are read and written.ensureWrite
- if true, write operations are blocked until all the requested data is written to the underlying stream.public void close() throws IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IOException
IOException
public final int pad(int n) throws IOException
n
- The number of zeros to be writtenIOException
public final boolean readBoolean() throws EOFException, IOException
EOFException
IOException
public final byte readByte() throws EOFException, IOException
EOFException
IOException
public final int readBytes(byte[] buf, int start, int count) throws IOException
Stream
readBytes
in class Stream
buf
- the byte array to read data intostart
- the start position in the arraycount
- the number of bytes to readIOException
public final int readBytes(byte[] buf) throws IOException
buf
- the byte array to read data intoIOException
- If an error prevented the read operation from occurring.public double readFloat() throws EOFException, IOException
EOFException
IOException
public int readInt() throws EOFException, IOException
EOFException
IOException
public short readShort() throws EOFException, IOException
EOFException
IOException
public double readDouble() throws EOFException, IOException
EOFException
IOException
public long readLong() throws EOFException, IOException
EOFException
IOException
public final int readUnsignedByte() throws EOFException, IOException
EOFException
IOException
writeByte(int)
public int readUnsignedShort() throws EOFException, IOException
EOFException
IOException
writeShort(int)
public long readUnsignedInt() throws EOFException, IOException
EOFException
IOException
public final int writeBoolean(boolean bool) throws IOException
bool
- the boolean to writeIOException
public final int writeByte(byte by) throws IOException
by
- the byte to writeIOException
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
IOException
if an error prevented the write
operation from occurring.writeBytes
in class Stream
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to writeIOException
public final int writeBytes(byte[] buf) throws IOException
writeBytes
in class Stream
buf
- the byte array to write data fromIOException
public int writeFloat(double f) throws IOException
f
- the float to writeIOException
public int writeInt(int i) throws IOException
i
- the integer to writeIOException
public int writeShort(int i) throws IOException
i
- the short to writeIOException
public java.lang.String readString() throws EOFException, IOException
The String size is limited to 65535 characters.
EOFException
IOException
public java.lang.String readBigString() throws EOFException, IOException
The String size is limited to 2,147,483,647 characters.
EOFException
IOException
public java.lang.String[] readStringArray() throws EOFException, IOException
The array length is limited to 65535 elements.
EOFException
IOException
public java.lang.String[] readStringArray(int size) throws EOFException, IOException
size
- The of the string array.EOFException
IOException
public int writeString(java.lang.String s) throws IOException
The String size is limited to 65535 characters.
IOException
public int writeBigString(java.lang.String s) throws IOException
The String size is limited to 2,147,483,647 characters.
IOException
public int writeStringArray(java.lang.String[] v) throws IOException
IOException
public int writeDouble(double d) throws IOException
IOException
public int writeLong(long l) throws IOException
IOException
public java.lang.String readCString() throws EOFException, IOException
EOFException
IOException
public final int writeCString(java.lang.String s) throws IOException
s
- the string to writeIOException
public char readChar() throws EOFException, IOException
EOFException
IOException
public int writeChar(char c) throws IOException
c
- the character to be written.IOException
public char[] readChars() throws EOFException, IOException
The char array size is limited to 65535 characters.
EOFException
IOException
public void readChars(char[] chars, int len) throws EOFException, IOException
chars
- An already created chars array.len
- The array length.EOFException
IOException
public int readChars(char[] chars, int start, int count) throws EOFException, IOException
chars
- a char arraystart
- starting position on the char arraycount
- number of chars to be read from the streamEOFException
IOException
public char[] readBigChars() throws EOFException, IOException
The char array size is limited to 2,147,483,647 characters.
EOFException
IOException
protected char[] readChars(int len) throws EOFException, IOException
len
- EOFException
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(java.lang.String s, int len) throws IOException
The char array size is limited to 65535 characters.
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.IOException
public Stream getStream()
public java.lang.String readFixedString(int length) throws EOFException, IOException
length
- the number of characters to readEOFException
IOException
public void writeFixedString(java.lang.String s, int length) throws IOException
s
- the string to writelength
- the length of the fixed stringIOException
public void writeFixedString(java.lang.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 lengthIOException
public void writeObject(Storable s) throws IOException
IOException
Storable
public java.lang.Object readObject() throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException, EOFException, IOException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException
EOFException
IOException
Storable
public java.lang.String readSmallString() throws EOFException, IOException
The String size is limited to 255 characters.
EOFException
IOException
public int writeSmallString(java.lang.String s) throws IOException
The String size is limited to 255 characters.
IOException
public int writeSmallString8(java.lang.String s) throws IOException
IOException
protected int writeBytesInternal(byte[] buf, int start, int count) throws IOException
IOException
protected int readBytesInternal(byte[] buf, int start, int count, boolean throwEOF) throws EOFException, IOException
buf
- start
- count
- EOFException
IOException
@Deprecated public int skip(int n) throws IOException
IOException