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, writeBytesprotected 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.Closeableclose in interface java.lang.AutoCloseableIOExceptionIOExceptionpublic final int pad(int n)
throws IOException
n - The number of zeros to be writtenIOExceptionpublic final boolean readBoolean()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic final byte readByte()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic final int readBytes(byte[] buf,
int start,
int count)
throws IOException
StreamreadBytes in class Streambuf - the byte array to read data intostart - the start position in the arraycount - the number of bytes to readIOExceptionpublic 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
EOFExceptionIOExceptionpublic int readInt()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic short readShort()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic double readDouble()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic long readLong()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic final int readUnsignedByte()
throws EOFException,
IOException
EOFExceptionIOExceptionwriteByte(int)public int readUnsignedShort()
throws EOFException,
IOException
EOFExceptionIOExceptionwriteShort(int)public long readUnsignedInt()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic final int writeBoolean(boolean bool)
throws IOException
bool - the boolean to writeIOExceptionpublic final int writeByte(byte by)
throws IOException
by - the byte to writeIOExceptionpublic final int writeByte(int by)
throws IOException
by - the byte to write (only least significant byte is written)IOExceptionpublic final int writeBytes(byte[] buf,
int start,
int count)
throws IOException
StreamIOException if an error prevented the write
operation from occurring.writeBytes in class Streambuf - the byte array to write data fromstart - the start position in the byte arraycount - the number of bytes to writeIOExceptionpublic final int writeBytes(byte[] buf)
throws IOException
writeBytes in class Streambuf - the byte array to write data fromIOExceptionpublic int writeFloat(double f)
throws IOException
f - the float to writeIOExceptionpublic int writeInt(int i)
throws IOException
i - the integer to writeIOExceptionpublic int writeShort(int i)
throws IOException
i - the short to writeIOExceptionpublic java.lang.String readString()
throws EOFException,
IOException
The String size is limited to 65535 characters.
EOFExceptionIOExceptionpublic java.lang.String readBigString()
throws EOFException,
IOException
The String size is limited to 2,147,483,647 characters.
EOFExceptionIOExceptionpublic java.lang.String[] readStringArray()
throws EOFException,
IOException
The array length is limited to 65535 elements.
EOFExceptionIOExceptionpublic java.lang.String[] readStringArray(int size)
throws EOFException,
IOException
size - The of the string array.EOFExceptionIOExceptionpublic int writeString(java.lang.String s)
throws IOException
The String size is limited to 65535 characters.
IOExceptionpublic int writeBigString(java.lang.String s)
throws IOException
The String size is limited to 2,147,483,647 characters.
IOExceptionpublic int writeStringArray(java.lang.String[] v)
throws IOException
IOExceptionpublic int writeDouble(double d)
throws IOException
IOExceptionpublic int writeLong(long l)
throws IOException
IOExceptionpublic java.lang.String readCString()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic final int writeCString(java.lang.String s)
throws IOException
s - the string to writeIOExceptionpublic char readChar()
throws EOFException,
IOException
EOFExceptionIOExceptionpublic int writeChar(char c)
throws IOException
c - the character to be written.IOExceptionpublic char[] readChars()
throws EOFException,
IOException
The char array size is limited to 65535 characters.
EOFExceptionIOExceptionpublic void readChars(char[] chars,
int len)
throws EOFException,
IOException
chars - An already created chars array.len - The array length.EOFExceptionIOExceptionpublic 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 streamEOFExceptionIOExceptionpublic char[] readBigChars()
throws EOFException,
IOException
The char array size is limited to 2,147,483,647 characters.
EOFExceptionIOExceptionprotected char[] readChars(int len)
throws EOFException,
IOException
len - EOFExceptionIOExceptionpublic 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).IOExceptionpublic 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).IOExceptionpublic int writeChars(char[] chars,
int start,
int len,
int lenSize)
throws IOException
IOExceptionpublic 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.IOExceptionpublic Stream getStream()
public java.lang.String readFixedString(int length)
throws EOFException,
IOException
length - the number of characters to readEOFExceptionIOExceptionpublic void writeFixedString(java.lang.String s,
int length)
throws IOException
s - the string to writelength - the length of the fixed stringIOExceptionpublic 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 lengthIOExceptionpublic void writeObject(Storable s) throws IOException
IOExceptionStorablepublic java.lang.Object readObject()
throws java.lang.ClassNotFoundException,
java.lang.InstantiationException,
java.lang.IllegalAccessException,
EOFException,
IOException
java.lang.ClassNotFoundExceptionjava.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionEOFExceptionIOExceptionStorablepublic java.lang.String readSmallString()
throws EOFException,
IOException
The String size is limited to 255 characters.
EOFExceptionIOExceptionpublic int writeSmallString(java.lang.String s)
throws IOException
The String size is limited to 255 characters.
IOExceptionpublic int writeSmallString8(java.lang.String s)
throws IOException
IOExceptionprotected int writeBytesInternal(byte[] buf,
int start,
int count)
throws IOException
IOExceptionprotected int readBytesInternal(byte[] buf,
int start,
int count,
boolean throwEOF)
throws EOFException,
IOException
buf - start - count - EOFExceptionIOException@Deprecated
public int skip(int n)
throws IOException
IOException