public class BlaubotKingdomConnection extends AbstractBlaubotConnection implements IBlaubotConnection
connectionListeners
Modifier and Type | Method and Description |
---|---|
static BlaubotKingdomConnection |
createFromInboundConnection(IBlaubotConnection connection)
Creates this conneciton upon another connection and awaits data to be sent instantly.
|
static BlaubotKingdomConnection |
createFromOutboundConnection(IBlaubotConnection connection,
java.lang.String kingUniqueDeviceId)
Creates this connection upon another connection and ensures to send the handshake before first write.
|
void |
disconnect()
Disconnects the connection (if connected), nothing otherwise
|
boolean |
equals(java.lang.Object o) |
java.lang.String |
getKingUniqueDeviceId()
Get the uniqueDeviceId of the king
|
IBlaubotDevice |
getRemoteDevice()
The remote device connected to our device.
|
int |
hashCode() |
boolean |
isConnected() |
static void |
main(java.lang.String[] args) |
int |
read()
Reads the next byte of data from the input stream.
|
int |
read(byte[] buffer)
Reads some number of bytes from the contained input stream and
stores them into the buffer array
b . |
int |
read(byte[] buffer,
int byteOffset,
int byteCount)
Reads up to
len bytes of data from the contained
input stream into an array of bytes. |
void |
readFully(byte[] buffer)
See the general contract of the
readFully
method of DataInput . |
void |
readFully(byte[] buffer,
int offset,
int byteCount)
See the general contract of the
readFully
method of DataInput . |
java.lang.String |
toString() |
void |
write(byte[] bytes)
Writes
b.length bytes from the specified byte array
to this output stream. |
void |
write(byte[] bytes,
int byteOffset,
int byteCount)
Writes
len bytes from the specified byte array
starting at offset off to this output stream. |
void |
write(int b)
Writes the specified byte to this output stream.
|
addConnectionListener, notifyDisconnected, removeConnectionListener
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addConnectionListener, removeConnectionListener
public static BlaubotKingdomConnection createFromOutboundConnection(IBlaubotConnection connection, java.lang.String kingUniqueDeviceId)
connection
- the outbound connection to be wrappedkingUniqueDeviceId
- the current kingdom's king unique device idpublic static BlaubotKingdomConnection createFromInboundConnection(IBlaubotConnection connection) throws java.io.IOException
connection
- the inbound connectionjava.io.IOException
- if the initial handshake or something connection related failedpublic void disconnect()
IBlaubotConnection
disconnect
in interface IBlaubotConnection
public boolean isConnected()
isConnected
in interface IBlaubotConnection
public IBlaubotDevice getRemoteDevice()
IBlaubotConnection
getRemoteDevice
in interface IBlaubotConnection
public void write(int b) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
write
is that one byte is written
to the output stream. The byte to be written is the eight
low-order bits of the argument b
. The 24
high-order bits of b
are ignored.
Subclasses of OutputStream
must provide an
implementation for this method.write
in interface IBlaubotConnection
b
- the byte
.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if an I/O error occurs. In particular,
an IOException
may be thrown if the
output stream has been closed.public void write(byte[] bytes) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
b.length
bytes from the specified byte array
to this output stream. The general contract for write(b)
is that it should have exactly the same effect as the call
write(b, 0, b.length)
.write
in interface IBlaubotConnection
bytes
- the data.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if an I/O error occurs.OutputStream.write(byte[], int, int)
public void write(byte[] bytes, int byteOffset, int byteCount) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
len
bytes from the specified byte array
starting at offset off
to this output stream.
The general contract for write(b, off, len)
is that
some of the bytes in the array b
are written to the
output stream in order; element b[off]
is the first
byte written and b[off+len-1]
is the last byte written
by this operation.
The write
method of OutputStream
calls
the write method of one argument on each of the bytes to be
written out. Subclasses are encouraged to override this method and
provide a more efficient implementation.
If b
is null
, a
NullPointerException
is thrown.
If off
is negative, or len
is negative, or
off+len
is greater than the length of the array
b
, then an IndexOutOfBoundsException is thrown.write
in interface IBlaubotConnection
bytes
- the data.byteOffset
- the start offset in the data.byteCount
- the number of bytes to write.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if an I/O error occurs. In particular,
an IOException
is thrown if the output
stream is closed.public int read() throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
int
in the range 0
to
255
. If no byte is available because the end of the stream
has been reached, the value -1
is returned. This method
blocks until input data is available, the end of the stream is detected,
or an exception is thrown.
A subclass must provide an implementation of this method.read
in interface IBlaubotConnection
-1
if the end of the
stream is reached.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if an I/O error occurs.public int read(byte[] buffer) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
b
. The number of
bytes actually read is returned as an integer. This method blocks
until input data is available, end of file is detected, or an
exception is thrown.
If b
is null, a NullPointerException
is
thrown. If the length of b
is zero, then no bytes are
read and 0
is returned; otherwise, there is an attempt
to read at least one byte. If no byte is available because the
stream is at end of file, the value -1
is returned;
otherwise, at least one byte is read and stored into b
.
The first byte read is stored into element b[0]
, the
next one into b[1]
, and so on. The number of bytes read
is, at most, equal to the length of b
. Let k
be the number of bytes actually read; these bytes will be stored in
elements b[0]
through b[k-1]
, leaving
elements b[k]
through b[b.length-1]
unaffected.
The read(b)
method has the same effect as:
read(b, 0, b.length)
read
in interface IBlaubotConnection
buffer
- the buffer into which the data is read.-1
if there is no more data because the end
of the stream has been reached.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if the first byte cannot be read for any reason
other than end of file, the stream has been closed and the underlying
input stream does not support reading after close, or another I/O
error occurs.FilterInputStream.in
,
InputStream.read(byte[], int, int)
public int read(byte[] buffer, int byteOffset, int byteCount) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
len
bytes of data from the contained
input stream into an array of bytes. An attempt is made to read
as many as len
bytes, but a smaller number may be read,
possibly zero. The number of bytes actually read is returned as an
integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.
If len
is zero, then no bytes are read and
0
is returned; otherwise, there is an attempt to read at
least one byte. If no byte is available because the stream is at end of
file, the value -1
is returned; otherwise, at least one
byte is read and stored into b
.
The first byte read is stored into element b[off]
, the
next one into b[off+1]
, and so on. The number of bytes read
is, at most, equal to len
. Let k be the number of
bytes actually read; these bytes will be stored in elements
b[off]
through b[off+
k-1]
,
leaving elements b[off+
k]
through
b[off+len-1]
unaffected.
In every case, elements b[0]
through
b[off]
and elements b[off+len]
through
b[b.length-1]
are unaffected.
read
in interface IBlaubotConnection
buffer
- the buffer into which the data is read.byteOffset
- the start offset in the destination array b
byteCount
- the maximum number of bytes read.-1
if there is no more data because the end
of the stream has been reached.java.net.SocketTimeoutException
- if the socket timed outjava.io.IOException
- if the first byte cannot be read for any reason
other than end of file, the stream has been closed and the underlying
input stream does not support reading after close, or another I/O
error occurs.FilterInputStream.in
,
InputStream.read(byte[], int, int)
public void readFully(byte[] buffer) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
readFully
method of DataInput
.
Bytes
for this operation are read from the contained
input stream.readFully
in interface IBlaubotConnection
buffer
- the buffer into which the data is read.java.net.SocketTimeoutException
- if the socket timed outjava.io.EOFException
- if this input stream reaches the end before
reading all the bytes.java.io.IOException
- the stream has been closed and the contained
input stream does not support reading after close, or
another I/O error occurs.FilterInputStream.in
public void readFully(byte[] buffer, int offset, int byteCount) throws java.net.SocketTimeoutException, java.io.IOException
IBlaubotConnection
readFully
method of DataInput
.
Bytes
for this operation are read from the contained
input stream.readFully
in interface IBlaubotConnection
buffer
- the buffer into which the data is read.offset
- the start offset of the data.byteCount
- the number of bytes to read.java.net.SocketTimeoutException
- if the socket timed outjava.io.EOFException
- if this input stream reaches the end before
reading all the bytes.java.io.IOException
- the stream has been closed and the contained
input stream does not support reading after close, or
another I/O error occurs.FilterInputStream.in
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class AbstractBlaubotConnection
public int hashCode()
hashCode
in class AbstractBlaubotConnection
public static void main(java.lang.String[] args) throws java.lang.InterruptedException
java.lang.InterruptedException
public java.lang.String getKingUniqueDeviceId()