0N/A/*
2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/A/**
0N/A * This class represents a datagram packet.
0N/A * <p>
0N/A * Datagram packets are used to implement a connectionless packet
0N/A * delivery service. Each message is routed from one machine to
0N/A * another based solely on information contained within that packet.
0N/A * Multiple packets sent from one machine to another might be routed
0N/A * differently, and might arrive in any order. Packet delivery is
0N/A * not guaranteed.
0N/A *
0N/A * @author Pavani Diwanji
0N/A * @author Benjamin Renaud
0N/A * @since JDK1.0
0N/A */
0N/Apublic final
0N/Aclass DatagramPacket {
0N/A
0N/A /**
0N/A * Perform class initialization
0N/A */
0N/A static {
0N/A java.security.AccessController.doPrivileged(
0N/A new sun.security.action.LoadLibraryAction("net"));
0N/A init();
0N/A }
0N/A
0N/A /*
0N/A * The fields of this class are package-private since DatagramSocketImpl
0N/A * classes needs to access them.
0N/A */
0N/A byte[] buf;
0N/A int offset;
0N/A int length;
0N/A int bufLength;
0N/A InetAddress address;
0N/A int port;
0N/A
0N/A /**
0N/A * Constructs a <code>DatagramPacket</code> for receiving packets of
0N/A * length <code>length</code>, specifying an offset into the buffer.
0N/A * <p>
0N/A * The <code>length</code> argument must be less than or equal to
0N/A * <code>buf.length</code>.
0N/A *
0N/A * @param buf buffer for holding the incoming datagram.
0N/A * @param offset the offset for the buffer
0N/A * @param length the number of bytes to read.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public DatagramPacket(byte buf[], int offset, int length) {
0N/A setData(buf, offset, length);
0N/A this.address = null;
0N/A this.port = -1;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>DatagramPacket</code> for receiving packets of
0N/A * length <code>length</code>.
0N/A * <p>
0N/A * The <code>length</code> argument must be less than or equal to
0N/A * <code>buf.length</code>.
0N/A *
0N/A * @param buf buffer for holding the incoming datagram.
0N/A * @param length the number of bytes to read.
0N/A */
0N/A public DatagramPacket(byte buf[], int length) {
0N/A this (buf, 0, length);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a datagram packet for sending packets of length
0N/A * <code>length</code> with offset <code>ioffset</code>to the
0N/A * specified port number on the specified host. The
0N/A * <code>length</code> argument must be less than or equal to
0N/A * <code>buf.length</code>.
0N/A *
0N/A * @param buf the packet data.
0N/A * @param offset the packet data offset.
0N/A * @param length the packet data length.
0N/A * @param address the destination address.
0N/A * @param port the destination port number.
0N/A * @see java.net.InetAddress
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public DatagramPacket(byte buf[], int offset, int length,
0N/A InetAddress address, int port) {
0N/A setData(buf, offset, length);
0N/A setAddress(address);
0N/A setPort(port);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a datagram packet for sending packets of length
0N/A * <code>length</code> with offset <code>ioffset</code>to the
0N/A * specified port number on the specified host. The
0N/A * <code>length</code> argument must be less than or equal to
0N/A * <code>buf.length</code>.
0N/A *
0N/A * @param buf the packet data.
0N/A * @param offset the packet data offset.
0N/A * @param length the packet data length.
0N/A * @param address the destination socket address.
0N/A * @throws IllegalArgumentException if address type is not supported
0N/A * @see java.net.InetAddress
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public DatagramPacket(byte buf[], int offset, int length,
0N/A SocketAddress address) throws SocketException {
0N/A setData(buf, offset, length);
0N/A setSocketAddress(address);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a datagram packet for sending packets of length
0N/A * <code>length</code> to the specified port number on the specified
0N/A * host. The <code>length</code> argument must be less than or equal
0N/A * to <code>buf.length</code>.
0N/A *
0N/A * @param buf the packet data.
0N/A * @param length the packet length.
0N/A * @param address the destination address.
0N/A * @param port the destination port number.
0N/A * @see java.net.InetAddress
0N/A */
0N/A public DatagramPacket(byte buf[], int length,
0N/A InetAddress address, int port) {
0N/A this(buf, 0, length, address, port);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a datagram packet for sending packets of length
0N/A * <code>length</code> to the specified port number on the specified
0N/A * host. The <code>length</code> argument must be less than or equal
0N/A * to <code>buf.length</code>.
0N/A *
0N/A * @param buf the packet data.
0N/A * @param length the packet length.
0N/A * @param address the destination address.
0N/A * @throws IllegalArgumentException if address type is not supported
0N/A * @since 1.4
0N/A * @see java.net.InetAddress
0N/A */
0N/A public DatagramPacket(byte buf[], int length,
0N/A SocketAddress address) throws SocketException {
0N/A this(buf, 0, length, address);
0N/A }
0N/A
0N/A /**
0N/A * Returns the IP address of the machine to which this datagram is being
0N/A * sent or from which the datagram was received.
0N/A *
0N/A * @return the IP address of the machine to which this datagram is being
0N/A * sent or from which the datagram was received.
0N/A * @see java.net.InetAddress
0N/A * @see #setAddress(java.net.InetAddress)
0N/A */
0N/A public synchronized InetAddress getAddress() {
0N/A return address;
0N/A }
0N/A
0N/A /**
0N/A * Returns the port number on the remote host to which this datagram is
0N/A * being sent or from which the datagram was received.
0N/A *
0N/A * @return the port number on the remote host to which this datagram is
0N/A * being sent or from which the datagram was received.
0N/A * @see #setPort(int)
0N/A */
0N/A public synchronized int getPort() {
0N/A return port;
0N/A }
0N/A
0N/A /**
0N/A * Returns the data buffer. The data received or the data to be sent
0N/A * starts from the <code>offset</code> in the buffer,
0N/A * and runs for <code>length</code> long.
0N/A *
0N/A * @return the buffer used to receive or send data
0N/A * @see #setData(byte[], int, int)
0N/A */
0N/A public synchronized byte[] getData() {
0N/A return buf;
0N/A }
0N/A
0N/A /**
0N/A * Returns the offset of the data to be sent or the offset of the
0N/A * data received.
0N/A *
0N/A * @return the offset of the data to be sent or the offset of the
0N/A * data received.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public synchronized int getOffset() {
0N/A return offset;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of the data to be sent or the length of the
0N/A * data received.
0N/A *
0N/A * @return the length of the data to be sent or the length of the
0N/A * data received.
0N/A * @see #setLength(int)
0N/A */
0N/A public synchronized int getLength() {
0N/A return length;
0N/A }
0N/A
0N/A /**
0N/A * Set the data buffer for this packet. This sets the
0N/A * data, length and offset of the packet.
0N/A *
0N/A * @param buf the buffer to set for this packet
0N/A *
0N/A * @param offset the offset into the data
0N/A *
0N/A * @param length the length of the data
0N/A * and/or the length of the buffer used to receive data
0N/A *
0N/A * @exception NullPointerException if the argument is null
0N/A *
0N/A * @see #getData
0N/A * @see #getOffset
0N/A * @see #getLength
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public synchronized void setData(byte[] buf, int offset, int length) {
0N/A /* this will check to see if buf is null */
0N/A if (length < 0 || offset < 0 ||
0N/A (length + offset) < 0 ||
0N/A ((length + offset) > buf.length)) {
0N/A throw new IllegalArgumentException("illegal length or offset");
0N/A }
0N/A this.buf = buf;
0N/A this.length = length;
0N/A this.bufLength = length;
0N/A this.offset = offset;
0N/A }
0N/A
0N/A /**
0N/A * Sets the IP address of the machine to which this datagram
0N/A * is being sent.
0N/A * @param iaddr the <code>InetAddress</code>
0N/A * @since JDK1.1
0N/A * @see #getAddress()
0N/A */
0N/A public synchronized void setAddress(InetAddress iaddr) {
0N/A address = iaddr;
0N/A }
0N/A
0N/A /**
0N/A * Sets the port number on the remote host to which this datagram
0N/A * is being sent.
0N/A * @param iport the port number
0N/A * @since JDK1.1
0N/A * @see #getPort()
0N/A */
0N/A public synchronized void setPort(int iport) {
0N/A if (iport < 0 || iport > 0xFFFF) {
0N/A throw new IllegalArgumentException("Port out of range:"+ iport);
0N/A }
0N/A port = iport;
0N/A }
0N/A
0N/A /**
0N/A * Sets the SocketAddress (usually IP address + port number) of the remote
0N/A * host to which this datagram is being sent.
0N/A *
0N/A * @param address the <code>SocketAddress</code>
0N/A * @throws IllegalArgumentException if address is null or is a
0N/A * SocketAddress subclass not supported by this socket
0N/A *
0N/A * @since 1.4
0N/A * @see #getSocketAddress
0N/A */
0N/A public synchronized void setSocketAddress(SocketAddress address) {
0N/A if (address == null || !(address instanceof InetSocketAddress))
0N/A throw new IllegalArgumentException("unsupported address type");
0N/A InetSocketAddress addr = (InetSocketAddress) address;
0N/A if (addr.isUnresolved())
0N/A throw new IllegalArgumentException("unresolved address");
0N/A setAddress(addr.getAddress());
0N/A setPort(addr.getPort());
0N/A }
0N/A
0N/A /**
0N/A * Gets the SocketAddress (usually IP address + port number) of the remote
0N/A * host that this packet is being sent to or is coming from.
0N/A *
0N/A * @return the <code>SocketAddress</code>
0N/A * @since 1.4
0N/A * @see #setSocketAddress
0N/A */
0N/A public synchronized SocketAddress getSocketAddress() {
0N/A return new InetSocketAddress(getAddress(), getPort());
0N/A }
0N/A
0N/A /**
0N/A * Set the data buffer for this packet. With the offset of
0N/A * this DatagramPacket set to 0, and the length set to
0N/A * the length of <code>buf</code>.
0N/A *
0N/A * @param buf the buffer to set for this packet.
0N/A *
0N/A * @exception NullPointerException if the argument is null.
0N/A *
0N/A * @see #getLength
0N/A * @see #getData
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A public synchronized void setData(byte[] buf) {
0N/A if (buf == null) {
0N/A throw new NullPointerException("null packet buffer");
0N/A }
0N/A this.buf = buf;
0N/A this.offset = 0;
0N/A this.length = buf.length;
0N/A this.bufLength = buf.length;
0N/A }
0N/A
0N/A /**
0N/A * Set the length for this packet. The length of the packet is
0N/A * the number of bytes from the packet's data buffer that will be
0N/A * sent, or the number of bytes of the packet's data buffer that
0N/A * will be used for receiving data. The length must be lesser or
0N/A * equal to the offset plus the length of the packet's buffer.
0N/A *
0N/A * @param length the length to set for this packet.
0N/A *
0N/A * @exception IllegalArgumentException if the length is negative
0N/A * of if the length is greater than the packet's data buffer
0N/A * length.
0N/A *
0N/A * @see #getLength
0N/A * @see #setData
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A public synchronized void setLength(int length) {
0N/A if ((length + offset) > buf.length || length < 0 ||
0N/A (length + offset) < 0) {
0N/A throw new IllegalArgumentException("illegal length");
0N/A }
0N/A this.length = length;
0N/A this.bufLength = this.length;
0N/A }
0N/A
0N/A /**
0N/A * Perform class load-time initializations.
0N/A */
0N/A private native static void init();
0N/A}