jdk/src/share/classes/java/net/DatagramPacket.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 12559 9456ceada8b1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class represents a datagram packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Datagram packets are used to implement a connectionless packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * delivery service. Each message is routed from one machine to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * another based solely on information contained within that packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Multiple packets sent from one machine to another might be routed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * differently, and might arrive in any order. Packet delivery is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * not guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class DatagramPacket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Perform class initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                  new sun.security.action.LoadLibraryAction("net"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The fields of this class are package-private since DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * classes needs to access them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int bufLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructs a <code>DatagramPacket</code> for receiving packets of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * length <code>length</code>, specifying an offset into the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The <code>length</code> argument must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param   offset   the offset for the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public DatagramPacket(byte buf[], int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs a <code>DatagramPacket</code> for receiving packets of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * length <code>length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The <code>length</code> argument must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public DatagramPacket(byte buf[], int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this (buf, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Constructs a datagram packet for sending packets of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>length</code> with offset <code>ioffset</code>to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * specified port number on the specified host. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>length</code> argument must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public DatagramPacket(byte buf[], int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        setPort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Constructs a datagram packet for sending packets of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>length</code> with offset <code>ioffset</code>to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * specified port number on the specified host. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>length</code> argument must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param   address  the destination socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public DatagramPacket(byte buf[], int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                          SocketAddress address) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        setSocketAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Constructs a datagram packet for sending packets of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>length</code> to the specified port number on the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * host. The <code>length</code> argument must be less than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * to <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public DatagramPacket(byte buf[], int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this(buf, 0, length, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Constructs a datagram packet for sending packets of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>length</code> to the specified port number on the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * host. The <code>length</code> argument must be less than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * to <code>buf.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public DatagramPacket(byte buf[], int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                          SocketAddress address) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this(buf, 0, length, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the IP address of the machine to which this datagram is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return  the IP address of the machine to which this datagram is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *          sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see #setAddress(java.net.InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public synchronized InetAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the port number on the remote host to which this datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * being sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return  the port number on the remote host to which this datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          being sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see #setPort(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public synchronized int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Returns the data buffer. The data received or the data to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * starts from the <code>offset</code> in the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * and runs for <code>length</code> long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return  the buffer used to receive or  send data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see #setData(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public synchronized byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Returns the offset of the data to be sent or the offset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return  the offset of the data to be sent or the offset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *          data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public synchronized int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Returns the length of the data to be sent or the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return  the length of the data to be sent or the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *          data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see #setLength(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public synchronized int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Set the data buffer for this packet. This sets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * data, length and offset of the packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param buf the buffer to set for this packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param offset the offset into the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param length the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *       and/or the length of the buffer used to receive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @exception NullPointerException if the argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #getData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see #getOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public synchronized void setData(byte[] buf, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /* this will check to see if buf is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (length < 0 || offset < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            (length + offset) < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            ((length + offset) > buf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new IllegalArgumentException("illegal length or offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        this.bufLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Sets the IP address of the machine to which this datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * is being sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param iaddr the <code>InetAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see #getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public synchronized void setAddress(InetAddress iaddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        address = iaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Sets the port number on the remote host to which this datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * is being sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param iport the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public synchronized void setPort(int iport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (iport < 0 || iport > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new IllegalArgumentException("Port out of range:"+ iport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        port = iport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Sets the SocketAddress (usually IP address + port number) of the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * host to which this datagram is being sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param address the <code>SocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @throws  IllegalArgumentException if address is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see #getSocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public synchronized void setSocketAddress(SocketAddress address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (address == null || !(address instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new IllegalArgumentException("unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        InetSocketAddress addr = (InetSocketAddress) address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (addr.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new IllegalArgumentException("unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        setAddress(addr.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        setPort(addr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Gets the SocketAddress (usually IP address + port number) of the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * host that this packet is being sent to or is coming from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return the <code>SocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #setSocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public synchronized SocketAddress getSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return new InetSocketAddress(getAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Set the data buffer for this packet. With the offset of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * this DatagramPacket set to 0, and the length set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the length of <code>buf</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param buf the buffer to set for this packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception NullPointerException if the argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see #getData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public synchronized void setData(byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new NullPointerException("null packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        this.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        this.length = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        this.bufLength = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Set the length for this packet. The length of the packet is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the number of bytes from the packet's data buffer that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * sent, or the number of bytes of the packet's data buffer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * will be used for receiving data. The length must be lesser or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * equal to the offset plus the length of the packet's buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param length the length to set for this packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @exception IllegalArgumentException if the length is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * of if the length is greater than the packet's data buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #setData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public synchronized void setLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if ((length + offset) > buf.length || length < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            (length + offset) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new IllegalArgumentException("illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        this.bufLength = this.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    private native static void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}