FileDocCategorySizeDatePackage
DatagramPacket.javaAPI DocAndroid 1.5 API10185Wed May 06 22:41:04 BST 2009java.net

DatagramPacket

public final class DatagramPacket extends Object
This class represents a datagram packet which contains data either to be sent or received through a {@code DatagramSocket}. It holds additional information such as its source or destination host.
see
DatagramSocket
since
Android 1.0

Fields Summary
byte[]
data
int
length
InetAddress
address
int
port
int
offset
Constructors Summary
public DatagramPacket(byte[] data, int length)
Constructs a new {@code DatagramPacket} object to receive data up to {@code length} bytes.

param
data a byte array to store the read characters.
param
length the length of the data buffer.
since
Android 1.0


                                                                   
         
        this(data, 0, length);
    
public DatagramPacket(byte[] data, int length, SocketAddress sockAddr)
Constructs a new {@code DatagramPacket} object to send data to the address {@code sockAddr}. The {@code length} must be lesser than or equal to the size of {@code data}. The first {@code length} bytes of the data are sent.

param
data the byte array to store the data.
param
length the length of the data.
param
sockAddr the target host address and port.
throws
SocketException if an error in the underlying protocol occurs.
since
Android 1.0

        this(data, 0, length);
        setSocketAddress(sockAddr);
    
public DatagramPacket(byte[] data, int offset, int length, SocketAddress sockAddr)
Constructs a new {@code DatagramPacket} object to send data to the address {@code sockAddr}. The {@code length} must be lesser than or equal to the size of {@code data}. The first {@code length} bytes of the data are sent.

param
data the byte array to store the data.
param
offset the offset of the data.
param
length the length of the data.
param
sockAddr the target host address and port.
throws
SocketException if an error in the underlying protocol occurs.
since
Android 1.0

        this(data, offset, length);
        setSocketAddress(sockAddr);
    
public DatagramPacket(byte[] data, int offset, int length)
Constructs a new {@code DatagramPacket} object to receive data up to {@code length} bytes with a specified buffer offset.

param
data a byte array to store the read characters.
param
offset the offset of the byte array where the bytes is written.
param
length the length of the data.
since
Android 1.0

        super();
        setData(data, offset, length);
    
public DatagramPacket(byte[] data, int offset, int length, InetAddress host, int aPort)
Constructs a new {@code DatagramPacket} object to send data to the port {@code aPort} of the address {@code host}. The {@code length} must be lesser than or equal to the size of {@code data}. The first {@code length} bytes from the byte array position {@code offset} are sent.

param
data a byte array which stores the characters to be sent.
param
offset the offset of {@code data} where to read from.
param
length the length of data.
param
host the address of the target host.
param
aPort the port of the target host.
since
Android 1.0

        this(data, offset, length);
        setPort(aPort);
        address = host;
    
public DatagramPacket(byte[] data, int length, InetAddress host, int port)
Constructs a new {@code DatagramPacket} object to send data to the port {@code aPort} of the address {@code host}. The {@code length} must be lesser than or equal to the size of {@code data}. The first {@code length} bytes are sent.

param
data a byte array which stores the characters to be sent.
param
length the length of data.
param
host the address of the target host.
param
port the port of the target host.
since
Android 1.0

        this(data, 0, length, host, port);
    
Methods Summary
public synchronized java.net.InetAddressgetAddress()
Gets the sender or destination IP address of this datagram packet.

return
the address from where the datagram was received or to which it is sent.
since
Android 1.0

        return address;
    
public synchronized byte[]getData()
Gets the data of this datagram packet.

return
the received data or the data to be sent.
since
Android 1.0

        return data;
    
public synchronized intgetLength()
Gets the length of the data stored in this datagram packet.

return
the length of the received data or the data to be sent.
since
Android 1.0

        return length;
    
public synchronized intgetOffset()
Gets the offset of the data stored in this datagram packet.

return
the position of the received data or the data to be sent.
since
Android 1.0

        return offset;
    
public synchronized intgetPort()
Gets the port number of the target or sender host of this datagram packet.

return
the port number of the origin or target host.
since
Android 1.0

        return port;
    
public synchronized java.net.SocketAddressgetSocketAddress()
Gets the host address and the port to which this datagram packet is sent as a {@code SocketAddress} object.

return
the SocketAddress of the target host.
since
Android 1.0

        return new InetSocketAddress(getAddress(), getPort());
    
public synchronized voidsetAddress(java.net.InetAddress addr)
Sets the IP address of the target host.

param
addr the target host address.
since
Android 1.0

        address = addr;
    
public synchronized voidsetData(byte[] buf, int anOffset, int aLength)
Sets the data buffer for this datagram packet.

param
buf the buffer to store the data.
param
anOffset the buffer offset where the data is stored.
param
aLength the length of the data to be sent or the length of buffer to store the received data.
since
Android 1.0

        if (0 > anOffset || anOffset > buf.length || 0 > aLength
                || aLength > buf.length - anOffset) {
            throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        data = buf;
        offset = anOffset;
        length = aLength;
    
public synchronized voidsetData(byte[] buf)
Sets the data buffer for this datagram packet. The length of the datagram packet is set to the buffer length.

param
buf the buffer to store the data.
since
Android 1.0

        length = buf.length; // This will check for null
        data = buf;
        offset = 0;
    
public synchronized voidsetLength(int len)
Sets the length of the datagram packet. This length plus the offset must be lesser than or equal to the buffer size.

param
len the length of this datagram packet.
since
Android 1.0

        if (0 > len || offset + len > data.length) {
            throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        length = len;
    
public synchronized voidsetPort(int aPort)
Sets the port number of the target host of this datagram packet.

param
aPort the target host port number.
since
Android 1.0

        if (aPort < 0 || aPort > 65535) {
            throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
        }
        port = aPort;
    
public synchronized voidsetSocketAddress(java.net.SocketAddress sockAddr)
Sets the {@code SocketAddress} for this datagram packet.

param
sockAddr the SocketAddress of the target host.
since
Android 1.0

        if (!(sockAddr instanceof InetSocketAddress)) {
            throw new IllegalArgumentException(Msg.getString(
                    "K0316", sockAddr == null ? null : sockAddr.getClass())); //$NON-NLS-1$
        }
        InetSocketAddress inetAddr = (InetSocketAddress) sockAddr;
        port = inetAddr.getPort();
        address = inetAddr.getAddress();