jdk/src/share/classes/java/net/DatagramPacket.java
author chegar
Thu, 15 Aug 2013 15:16:31 +0100
changeset 19414 931c01c5c3aa
parent 19069 1d9cb0d080e3
child 24865 09b1d992ca72
permissions -rw-r--r--
8022126: Remove throws SocketException from DatagramPacket constructors accepting SocketAddress Reviewed-by: smarks, alanb, michaelm, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
     2
 * Copyright (c) 1995, 2013, 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(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    50
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    51
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    52
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    53
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    54
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    55
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The fields of this class are package-private since DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * classes needs to access them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int bufLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    71
     * Constructs a {@code DatagramPacket} for receiving packets of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    72
     * length {@code length}, specifying an offset into the buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    74
     * The {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    75
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param   offset   the offset for the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public DatagramPacket(byte buf[], int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    90
     * Constructs a {@code DatagramPacket} for receiving packets of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    91
     * length {@code length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    93
     * The {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    94
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public DatagramPacket(byte buf[], int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this (buf, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   105
     * {@code length} with offset {@code ioffset}to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * specified port number on the specified host. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   107
     * {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   108
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public DatagramPacket(byte buf[], int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        setAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        setPort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   128
     * {@code length} with offset {@code ioffset}to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * specified port number on the specified host. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   130
     * {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   131
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param   address  the destination socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
19414
931c01c5c3aa 8022126: Remove throws SocketException from DatagramPacket constructors accepting SocketAddress
chegar
parents: 19069
diff changeset
   142
    public DatagramPacket(byte buf[], int offset, int length, SocketAddress address) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        setSocketAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   149
     * {@code length} to the specified port number on the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   150
     * host. The {@code length} argument must be less than or equal
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   151
     * to {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public DatagramPacket(byte buf[], int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this(buf, 0, length, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   166
     * {@code length} to the specified port number on the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   167
     * host. The {@code length} argument must be less than or equal
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   168
     * to {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
19414
931c01c5c3aa 8022126: Remove throws SocketException from DatagramPacket constructors accepting SocketAddress
chegar
parents: 19069
diff changeset
   177
    public DatagramPacket(byte buf[], int length, SocketAddress address) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this(buf, 0, length, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return  the IP address of the machine to which this datagram is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *          sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see #setAddress(java.net.InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public synchronized InetAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return  the port number on the remote host to which this datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *          being sent or from which the datagram was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see #setPort(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public synchronized int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the data buffer. The data received or the data to be sent
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   208
     * starts from the {@code offset} in the buffer,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   209
     * and runs for {@code length} long.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return  the buffer used to receive or  send data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see #setData(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public synchronized byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns 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
     * @return  the offset of the data to be sent or the offset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *          data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public synchronized int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return  the length of the data to be sent or the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          data received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see #setLength(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public synchronized int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Set the data buffer for this packet. This sets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * data, length and offset of the packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param buf the buffer to set for this packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param offset the offset into the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param length the length of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *       and/or the length of the buffer used to receive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @exception NullPointerException if the argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @see #getData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see #getOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public synchronized void setData(byte[] buf, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /* this will check to see if buf is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (length < 0 || offset < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            (length + offset) < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ((length + offset) > buf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new IllegalArgumentException("illegal length or offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.bufLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Sets the IP address of the machine to which this datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * is being sent.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   278
     * @param iaddr the {@code InetAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see #getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public synchronized void setAddress(InetAddress iaddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        address = iaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Sets the port number on the remote host to which this datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * is being sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param iport the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public synchronized void setPort(int iport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (iport < 0 || iport > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new IllegalArgumentException("Port out of range:"+ iport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        port = iport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Sets the SocketAddress (usually IP address + port number) of the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * host to which this datagram is being sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   304
     * @param address the {@code SocketAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @throws  IllegalArgumentException if address is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see #getSocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public synchronized void setSocketAddress(SocketAddress address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (address == null || !(address instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new IllegalArgumentException("unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        InetSocketAddress addr = (InetSocketAddress) address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (addr.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new IllegalArgumentException("unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        setAddress(addr.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        setPort(addr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Gets the SocketAddress (usually IP address + port number) of the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * host that this packet is being sent to or is coming from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   325
     * @return the {@code SocketAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see #setSocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public synchronized SocketAddress getSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return new InetSocketAddress(getAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Set the data buffer for this packet. With the offset of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * this DatagramPacket set to 0, and the length set to
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   336
     * the length of {@code buf}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param buf the buffer to set for this packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @exception NullPointerException if the argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @see #getData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public synchronized void setData(byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new NullPointerException("null packet buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        this.offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        this.length = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        this.bufLength = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Set the length for this packet. The length of the packet is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * the number of bytes from the packet's data buffer that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * sent, or the number of bytes of the packet's data buffer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * will be used for receiving data. The length must be lesser or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * equal to the offset plus the length of the packet's buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param length the length to set for this packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @exception IllegalArgumentException if the length is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * of if the length is greater than the packet's data buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see #setData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public synchronized void setLength(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if ((length + offset) > buf.length || length < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            (length + offset) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new IllegalArgumentException("illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        this.bufLength = this.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private native static void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
}