src/java.base/share/classes/java/net/DatagramPacket.java
author chegar
Fri, 18 Oct 2019 17:08:59 +0100
branchdatagramsocketimpl-branch
changeset 58688 2b1e684c3ce6
parent 58679 9c3209ff7550
permissions -rw-r--r--
datagramsocketimpl-branch: fix issue with receive packet buf size
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53132
d976ee345d11 8182992: Typo in DatagramPacket constructor API doc
coffeys
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2019, 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
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
    28
import jdk.internal.access.JavaNetDatagramPacketAccess;
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
    29
import jdk.internal.access.SharedSecrets;
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class represents a datagram packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Datagram packets are used to implement a connectionless packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * delivery service. Each message is routed from one machine to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * another based solely on information contained within that packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Multiple packets sent from one machine to another might be routed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * differently, and might arrive in any order. Packet delivery is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * not guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Benjamin Renaud
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19414
diff changeset
    43
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class DatagramPacket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Perform class initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static {
55693
9a97b1393e72 8227587: Add internal privileged System.loadLibrary
redestad
parents: 53132
diff changeset
    52
        jdk.internal.loader.BootLoader.loadLibrary("net");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The fields of this class are package-private since DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * classes needs to access them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int bufLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    68
     * Constructs a {@code DatagramPacket} for receiving packets of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    69
     * length {@code length}, specifying an offset into the buffer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    71
     * The {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    72
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param   offset   the offset for the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public DatagramPacket(byte buf[], int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    87
     * Constructs a {@code DatagramPacket} for receiving packets of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    88
     * length {@code length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    90
     * The {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    91
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param   buf      buffer for holding the incoming datagram.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param   length   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public DatagramPacket(byte buf[], int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this (buf, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a datagram packet for sending packets of length
53132
d976ee345d11 8182992: Typo in DatagramPacket constructor API doc
coffeys
parents: 47216
diff changeset
   102
     * {@code length} with offset {@code offset} to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * specified port number on the specified host. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   104
     * {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   105
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public DatagramPacket(byte buf[], int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        setAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        setPort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Constructs a datagram packet for sending packets of length
53132
d976ee345d11 8182992: Typo in DatagramPacket constructor API doc
coffeys
parents: 47216
diff changeset
   125
     * {@code length} with offset {@code offset} to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * specified port number on the specified host. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   127
     * {@code length} argument must be less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   128
     * {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param   offset   the packet data offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param   length   the packet data length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param   address  the destination socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
19414
931c01c5c3aa 8022126: Remove throws SocketException from DatagramPacket constructors accepting SocketAddress
chegar
parents: 19069
diff changeset
   139
    public DatagramPacket(byte buf[], int offset, int length, SocketAddress address) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        setData(buf, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        setSocketAddress(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   146
     * {@code length} to the specified port number on the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   147
     * host. The {@code length} argument must be less than or equal
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   148
     * to {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param   port     the destination port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public DatagramPacket(byte buf[], int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                          InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this(buf, 0, length, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Constructs a datagram packet for sending packets of length
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   163
     * {@code length} to the specified port number on the specified
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   164
     * host. The {@code length} argument must be less than or equal
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   165
     * to {@code buf.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param   buf      the packet data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param   length   the packet length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param   address  the destination address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws  IllegalArgumentException if address type is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see     java.net.InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
19414
931c01c5c3aa 8022126: Remove throws SocketException from DatagramPacket constructors accepting SocketAddress
chegar
parents: 19069
diff changeset
   174
    public DatagramPacket(byte buf[], int length, SocketAddress address) {
2
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
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   205
     * starts from the {@code offset} in the buffer,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   206
     * and runs for {@code length} long.
2
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
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55693
diff changeset
   251
     * @throws    NullPointerException if the argument is null
2
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.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   275
     * @param iaddr the {@code InetAddress}
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19414
diff changeset
   276
     * @since   1.1
2
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
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19414
diff changeset
   287
     * @since   1.1
2
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
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   301
     * @param address the {@code SocketAddress}
2
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
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   322
     * @return the {@code SocketAddress}
2
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
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   333
     * the length of {@code buf}.
2
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
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55693
diff changeset
   337
     * @throws    NullPointerException if the argument is null.
2
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
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19414
diff changeset
   342
     * @since 1.1
2
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
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55693
diff changeset
   363
     * @throws    IllegalArgumentException if the length is negative
2
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
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 19414
diff changeset
   370
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public synchronized void setLength(int length) {
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   373
        setLengthField(length);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   374
        this.bufLength = this.length;
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   375
    }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   376
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   377
    private synchronized void setLengthField(int length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if ((length + offset) > buf.length || length < 0 ||
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   379
                (length + offset) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("illegal length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        this.length = length;
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   383
    }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   384
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   385
    private synchronized int getBufLength() {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   386
        return bufLength;
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   387
    }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   388
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   389
    static {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   390
        SharedSecrets.setJavaNetDatagrtamPacketAccess(
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   391
            new JavaNetDatagramPacketAccess() {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   392
                @Override
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   393
                public int getBufLengthField(DatagramPacket packet) {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   394
                    return packet.getBufLength();
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   395
                }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   396
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   397
                @Override
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   398
                public void setLengthField(DatagramPacket packet, int length) {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   399
                    packet.setLengthField(length);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   400
                }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   401
            }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 58679
diff changeset
   402
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Perform class load-time initializations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29986
diff changeset
   408
    private static native void init();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}