org.xsocket.datagram
Class UserDatagram

Package class diagram package UserDatagram
java.lang.Object
  extended by org.xsocket.datagram.UserDatagram
All Implemented Interfaces:
IDataSink, IDataSource

public final class UserDatagram
extends java.lang.Object
implements IDataSource, IDataSink

a datagram packet


Constructor Summary
UserDatagram(byte[] data)
          constructor. creates packet sets the content with the given byte array
UserDatagram(java.nio.ByteBuffer data)
          constructor. creates packet, and sets the content with the given buffer
UserDatagram(java.net.InetAddress remoteAddress, int remotePort, int size)
          constructor. creates an empty packet by setting the target remote address
UserDatagram(int size)
          constructor. creates an empty packet
UserDatagram(java.net.SocketAddress remoteSocketAddress, byte[] data)
          constructor. creates packet by setting the target remote address, and sets the content with the given byte array
UserDatagram(java.net.SocketAddress remoteSocketAddress, java.nio.ByteBuffer data)
          constructor. creates packet by setting the target remote address, and sets the content with the given buffer
UserDatagram(java.net.SocketAddress address, int size)
          constructor. creates an empty packet by setting the target remote address
UserDatagram(java.lang.String remoteHost, int remotePort, byte[] data)
          constructor. creates packet by setting the target remote address, and sets the content with the given byte array
UserDatagram(java.lang.String remoteHost, int remotePort, int size)
          constructor. creates an empty packet by setting the target remote address
 
Method Summary
 int getRemaining()
          get the remaining, unwritten packet size
 java.net.InetAddress getRemoteAddress()
          Returns the address of the machine to which this packet is being sent or from which the packet was received.
 int getRemotePort()
          Returns the port number of the machine to which this packet is being sent or from which the packet was received.
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the socket address of the machine to which this packet is being sent or from which the packet was received.
 int getSize()
          get the packet size
 byte readByte()
          read a byte
 java.nio.ByteBuffer readByteBuffer()
          read all remaining data as ByteBuffer
 java.nio.ByteBuffer readByteBufferByDelimiter(java.lang.String delimiter, int maxLength)
          read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 java.nio.ByteBuffer readByteBufferByLength(int length)
          read a ByteBuffer by using a length defintion
 byte[] readBytes()
          reads the remaining data as byte array
 byte[] readBytesByDelimiter(java.lang.String delimiter, int maxLength)
          read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 byte[] readBytesByLength(int length)
          read bytes by using a length defintion
 double readDouble()
          read a double
 int readInt()
          read an int
 long readLong()
          read a long
 short readShort()
          read an short
 java.lang.String readString()
          read the remaining data as String
 java.lang.String readString(java.lang.String encoding)
          read the remaining data as String
 java.lang.String readStringByDelimiter(java.lang.String delimiter)
          read a string by using a delimiter and the connection default encoding
 java.lang.String readStringByDelimiter(java.lang.String delimiter, int maxLength)
          read a string by using a delimiter and the connection default encoding
 java.lang.String readStringByDelimiter(java.lang.String delimiter, java.lang.String encoding, int maxLength)
          read a string by using a delimiter
 java.lang.String readStringByLength(int length)
          read a string by using a length definition and the connection default encoding
 java.lang.String readStringByLength(int length, java.lang.String encoding)
          read a string by using a length definition
 java.lang.String toString()
           
 int write(byte... bytes)
          writes bytes to the packet
 int write(byte b)
          writes a byte to the packet
 int write(byte[] bytes, int offset, int length)
          writes bytes to the packet
 int write(java.nio.ByteBuffer buffer)
          writes a byte buffer to the packet
 long write(java.nio.ByteBuffer[] buffers)
          writes a byte array to the packet
 int write(double d)
          writes a double to the packet
 int write(int i)
          writes a int to the packet
 int write(long l)
          writes a long to the packet
 int write(short s)
          writes a short to the packet
 int write(java.lang.String message)
          write a message
 int write(java.lang.String message, java.lang.String encoding)
          write a message
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UserDatagram

public UserDatagram(int size)
constructor. creates an empty packet

Parameters:
size - the packet size

UserDatagram

public UserDatagram(java.lang.String remoteHost,
                    int remotePort,
                    int size)
constructor. creates an empty packet by setting the target remote address

Parameters:
remoteHost - the destination hostname
remotePort - the destination port number
size - the packet size

UserDatagram

public UserDatagram(java.net.InetAddress remoteAddress,
                    int remotePort,
                    int size)
constructor. creates an empty packet by setting the target remote address

Parameters:
remoteAddress - the destination address
remotePort - the destination port number
size - the packet size

UserDatagram

public UserDatagram(java.net.SocketAddress address,
                    int size)
constructor. creates an empty packet by setting the target remote address

Parameters:
address - the destination address
size - the packet size

UserDatagram

public UserDatagram(java.nio.ByteBuffer data)
constructor. creates packet, and sets the content with the given buffer

Parameters:
data - the data which will be written into the buffer

UserDatagram

public UserDatagram(java.net.SocketAddress remoteSocketAddress,
                    java.nio.ByteBuffer data)
constructor. creates packet by setting the target remote address, and sets the content with the given buffer

Parameters:
remoteSocketAddress - the destination address
data - the data which will be written into the buffer

UserDatagram

public UserDatagram(byte[] data)
constructor. creates packet sets the content with the given byte array

Parameters:
data - the data which will be written into the buffer

UserDatagram

public UserDatagram(java.lang.String remoteHost,
                    int remotePort,
                    byte[] data)
constructor. creates packet by setting the target remote address, and sets the content with the given byte array

Parameters:
remoteHost - the destination hostname
remotePort - the destination port number
data - the data which will be written into the buffer

UserDatagram

public UserDatagram(java.net.SocketAddress remoteSocketAddress,
                    byte[] data)
constructor. creates packet by setting the target remote address, and sets the content with the given byte array

Parameters:
remoteSocketAddress - the destination address
data - the data which will be written into the buffer
Method Detail

getRemoteSocketAddress

public java.net.SocketAddress getRemoteSocketAddress()
Returns the socket address of the machine to which this packet is being sent or from which the packet was received.

Returns:
the socket address

getRemoteAddress

public java.net.InetAddress getRemoteAddress()
Returns the address of the machine to which this packet is being sent or from which the packet was received.

Returns:
the address

getRemotePort

public int getRemotePort()
Returns the port number of the machine to which this packet is being sent or from which the packet was received.

Returns:
the port number

readByte

public byte readByte()
              throws java.io.IOException,
                     java.nio.BufferUnderflowException
read a byte

Specified by:
readByte in interface IDataSource
Returns:
the byte value
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readByteBufferByLength

public java.nio.ByteBuffer readByteBufferByLength(int length)
                                           throws java.io.IOException,
                                                  java.nio.BufferUnderflowException
read a ByteBuffer by using a length defintion

Parameters:
length - the amount of bytes to read
Returns:
the ByteBuffer
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readBytesByLength

public byte[] readBytesByLength(int length)
                         throws java.io.IOException,
                                java.nio.BufferUnderflowException
read bytes by using a length defintion

Specified by:
readBytesByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
Returns:
the read bytes
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readStringByLength

public java.lang.String readStringByLength(int length,
                                           java.lang.String encoding)
                                    throws java.io.IOException,
                                           java.nio.BufferUnderflowException,
                                           java.io.UnsupportedEncodingException
read a string by using a length definition

Specified by:
readStringByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
encoding - the encoding to use
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.UnsupportedEncodingException - if the given encoding is not supported

readStringByLength

public java.lang.String readStringByLength(int length)
                                    throws java.io.IOException,
                                           java.nio.BufferUnderflowException,
                                           java.io.UnsupportedEncodingException
read a string by using a length definition and the connection default encoding

Specified by:
readStringByLength in interface IDataSource
Parameters:
length - the amount of bytes to read
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.UnsupportedEncodingException - if the given encoding is not supported

readDouble

public double readDouble()
                  throws java.io.IOException,
                         java.nio.BufferUnderflowException
read a double

Specified by:
readDouble in interface IDataSource
Returns:
the double value c * @throws IOException If some other I/O error occurs
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferUnderflowException

readInt

public int readInt()
            throws java.io.IOException,
                   java.nio.BufferUnderflowException
read an int

Specified by:
readInt in interface IDataSource
Returns:
the int value
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readShort

public short readShort()
                throws java.io.IOException,
                       java.nio.BufferUnderflowException
read an short

Specified by:
readShort in interface IDataSource
Returns:
the short value
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readLong

public long readLong()
              throws java.io.IOException,
                     java.nio.BufferUnderflowException
read a long

Specified by:
readLong in interface IDataSource
Returns:
the long value
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

readByteBuffer

public java.nio.ByteBuffer readByteBuffer()
                                   throws java.io.IOException
read all remaining data as ByteBuffer

Returns:
the ByteBuffer
Throws:
java.io.IOException - If some other I/O error occurs

readBytes

public byte[] readBytes()
                 throws java.io.IOException
reads the remaining data as byte array

Returns:
the byte array
Throws:
java.io.IOException - If some other I/O error occurs

readString

public java.lang.String readString()
                            throws java.io.IOException,
                                   java.io.UnsupportedEncodingException
read the remaining data as String

Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.io.UnsupportedEncodingException - if the default encoding is not supported

readString

public java.lang.String readString(java.lang.String encoding)
                            throws java.io.IOException,
                                   java.io.UnsupportedEncodingException
read the remaining data as String

Parameters:
encoding - the encoding to use
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.io.UnsupportedEncodingException - if the default encoding is not supported

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter)
                                       throws java.io.IOException,
                                              java.nio.BufferUnderflowException,
                                              java.io.UnsupportedEncodingException
read a string by using a delimiter and the connection default encoding

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.UnsupportedEncodingException - if the default encoding is not supported

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter,
                                              int maxLength)
                                       throws java.io.IOException,
                                              java.nio.BufferUnderflowException,
                                              java.io.UnsupportedEncodingException,
                                              MaxReadSizeExceededException
read a string by using a delimiter and the connection default encoding

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit will be exceeded a MaxReadSizeExceededException will been thrown
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
java.io.UnsupportedEncodingException - if the default encoding is not supported

readStringByDelimiter

public java.lang.String readStringByDelimiter(java.lang.String delimiter,
                                              java.lang.String encoding,
                                              int maxLength)
                                       throws java.io.IOException,
                                              java.nio.BufferUnderflowException,
                                              java.io.UnsupportedEncodingException,
                                              MaxReadSizeExceededException
read a string by using a delimiter

Specified by:
readStringByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
encoding - the encodin to use
maxLength - the max length of bytes that should be read. If the limit will be exceeded a MaxReadSizeExceededException will been thrown
Returns:
the string
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found
java.io.UnsupportedEncodingException - if the given encoding is not supported

readByteBufferByDelimiter

public java.nio.ByteBuffer readByteBufferByDelimiter(java.lang.String delimiter,
                                                     int maxLength)
                                              throws java.io.IOException,
                                                     java.nio.BufferUnderflowException,
                                                     MaxReadSizeExceededException
read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the ByteBuffer
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found
java.io.IOException - If some other I/O error occurs

readBytesByDelimiter

public byte[] readBytesByDelimiter(java.lang.String delimiter,
                                   int maxLength)
                            throws java.io.IOException,
                                   java.nio.BufferUnderflowException,
                                   MaxReadSizeExceededException
read a byte array by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readBytesByDelimiter in interface IDataSource
Parameters:
delimiter - the delimiter
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the read bytes
Throws:
java.nio.BufferUnderflowException - if the buffer`s limit has been reached
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found
java.io.IOException - If some other I/O error occurs

write

public int write(byte b)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a byte to the packet

Specified by:
write in interface IDataSink
Parameters:
b - the byte to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(short s)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a short to the packet

Specified by:
write in interface IDataSink
Parameters:
s - the short to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(byte... bytes)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes bytes to the packet

Specified by:
write in interface IDataSink
Parameters:
bytes - the bytes to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(byte[] bytes,
                 int offset,
                 int length)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes bytes to the packet

Specified by:
write in interface IDataSink
Parameters:
bytes - the bytes to write
offset - The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer`s position will be set to this value.
length - The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer`s limit will be set to offset + length.
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(java.nio.ByteBuffer buffer)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a byte buffer to the packet

Specified by:
write in interface IDataSink
Parameters:
buffer - the bytes to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public long write(java.nio.ByteBuffer[] buffers)
           throws java.io.IOException,
                  java.nio.BufferOverflowException
writes a byte array to the packet

Specified by:
write in interface IDataSink
Parameters:
buffers - the bytes to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(double d)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a double to the packet

Specified by:
write in interface IDataSink
Parameters:
d - the int value to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`'s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(int i)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a int to the packet

Specified by:
write in interface IDataSink
Parameters:
i - the int value to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(long l)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
writes a long to the packet

Specified by:
write in interface IDataSink
Parameters:
l - the int value to write
Returns:
the number of send bytes
Throws:
java.nio.BufferOverflowException - if the buffer`s limit has been reached
java.io.IOException - If some other I/O error occurs

write

public int write(java.lang.String message)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
write a message

Specified by:
write in interface IDataSink
Parameters:
message - the message to write
Returns:
the number of send bytes
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferOverflowException - if the buffer`s limit has been reached

write

public int write(java.lang.String message,
                 java.lang.String encoding)
          throws java.io.IOException,
                 java.nio.BufferOverflowException
write a message

Specified by:
write in interface IDataSink
Parameters:
message - the message to write
encoding - the encoding which should be used th encode the chars into byte (e.g. `US-ASCII` or `UTF-8`)
Returns:
the number of send bytes
Throws:
java.io.IOException - If some other I/O error occurs
java.nio.BufferOverflowException - if the buffer`s limit has been reached

getSize

public int getSize()
get the packet size

Returns:
the packet size

getRemaining

public int getRemaining()
get the remaining, unwritten packet size

Returns:
the remaining, unwritten packet size

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object