001: /*
002: * @(#)DatagramPacket.java 1.41 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.net;
029:
030: /**
031: * This class represents a datagram packet.
032: * <p>
033: * Datagram packets are used to implement a connectionless packet
034: * delivery service. Each message is routed from one machine to
035: * another based solely on information contained within that packet.
036: * Multiple packets sent from one machine to another might be routed
037: * differently, and might arrive in any order. Packet delivery is
038: * not guaranteed.
039: *
040: * @author Pavani Diwanji
041: * @author Benjamin Renaud
042: * @version 1.30, 02/02/00
043: * @since JDK1.0
044: */
045: public final class DatagramPacket {
046:
047: /**
048: * Perform class initialization
049: */
050: static {
051: java.security.AccessController
052: .doPrivileged(new sun.security.action.LoadLibraryAction(
053: "net"));
054: init();
055: }
056:
057: /*
058: * The fields of this class are package-private since DatagramSocketImpl
059: * classes needs to access them.
060: */
061: byte[] buf;
062: int offset;
063: int length;
064: int bufLength;
065: InetAddress address;
066: int port;
067:
068: /**
069: * Constructs a <code>DatagramPacket</code> for receiving packets of
070: * length <code>length</code>, specifying an offset into the buffer.
071: * <p>
072: * The <code>length</code> argument must be less than or equal to
073: * <code>buf.length</code>.
074: *
075: * @param buf buffer for holding the incoming datagram.
076: * @param offset the offset for the buffer
077: * @param length the number of bytes to read.
078: *
079: * @since JDK1.2
080: */
081: public DatagramPacket(byte buf[], int offset, int length) {
082: setData(buf, offset, length);
083: this .address = null;
084: this .port = -1;
085: }
086:
087: /**
088: * Constructs a <code>DatagramPacket</code> for receiving packets of
089: * length <code>length</code>.
090: * <p>
091: * The <code>length</code> argument must be less than or equal to
092: * <code>buf.length</code>.
093: *
094: * @param buf buffer for holding the incoming datagram.
095: * @param length the number of bytes to read.
096: */
097: public DatagramPacket(byte buf[], int length) {
098: this (buf, 0, length);
099: }
100:
101: /**
102: * Constructs a datagram packet for sending packets of length
103: * <code>length</code> with offset <code>ioffset</code>to the
104: * specified port number on the specified host. The
105: * <code>length</code> argument must be less than or equal to
106: * <code>buf.length</code>.
107: *
108: * @param buf the packet data.
109: * @param offset the packet data offset.
110: * @param length the packet data length.
111: * @param address the destination address.
112: * @param port the destination port number.
113: * @see java.net.InetAddress
114: *
115: * @since JDK1.2
116: */
117: public DatagramPacket(byte buf[], int offset, int length,
118: InetAddress address, int port) {
119: setData(buf, offset, length);
120: setAddress(address);
121: setPort(port);
122: }
123:
124: /**
125: * Constructs a datagram packet for sending packets of length
126: * <code>length</code> with offset <code>ioffset</code>to the
127: * specified port number on the specified host. The
128: * <code>length</code> argument must be less than or equal to
129: * <code>buf.length</code>.
130: *
131: * @param buf the packet data.
132: * @param offset the packet data offset.
133: * @param length the packet data length.
134: * @param address the destination socket address.
135: * @throws IllegalArgumentException if address type is not supported
136: * @see java.net.InetAddress
137: *
138: * @since 1.4
139: */
140: public DatagramPacket(byte buf[], int offset, int length,
141: SocketAddress address) throws SocketException {
142: setData(buf, offset, length);
143: setSocketAddress(address);
144: }
145:
146: /**
147: * Constructs a datagram packet for sending packets of length
148: * <code>length</code> to the specified port number on the specified
149: * host. The <code>length</code> argument must be less than or equal
150: * to <code>buf.length</code>.
151: *
152: * @param buf the packet data.
153: * @param length the packet length.
154: * @param address the destination address.
155: * @param port the destination port number.
156: * @see java.net.InetAddress
157: */
158: public DatagramPacket(byte buf[], int length, InetAddress address,
159: int port) {
160: this (buf, 0, length, address, port);
161: }
162:
163: /**
164: * Constructs a datagram packet for sending packets of length
165: * <code>length</code> to the specified port number on the specified
166: * host. The <code>length</code> argument must be less than or equal
167: * to <code>buf.length</code>.
168: *
169: * @param buf the packet data.
170: * @param length the packet length.
171: * @param address the destination address.
172: * @throws IllegalArgumentException if address type is not supported
173: * @since 1.4
174: * @see java.net.InetAddress
175: */
176: public DatagramPacket(byte buf[], int length, SocketAddress address)
177: throws SocketException {
178: this (buf, 0, length, address);
179: }
180:
181: /**
182: * Returns the IP address of the machine to which this datagram is being
183: * sent or from which the datagram was received.
184: *
185: * @return the IP address of the machine to which this datagram is being
186: * sent or from which the datagram was received.
187: * @see java.net.InetAddress
188: * @see #setAddress(java.net.InetAddress)
189: */
190: public synchronized InetAddress getAddress() {
191: return address;
192: }
193:
194: /**
195: * Returns the port number on the remote host to which this datagram is
196: * being sent or from which the datagram was received.
197: *
198: * @return the port number on the remote host to which this datagram is
199: * being sent or from which the datagram was received.
200: * @see #setPort(int)
201: */
202: public synchronized int getPort() {
203: return port;
204: }
205:
206: /**
207: * Returns the data buffer. The data received or the data to be sent
208: * starts from the <code>offset</code> in the buffer,
209: * and runs for <code>length</code> long.
210: *
211: * @return the buffer used to receive or send data
212: * @see #setData(byte[], int, int)
213: */
214: public synchronized byte[] getData() {
215: return buf;
216: }
217:
218: /**
219: * Returns the offset of the data to be sent or the offset of the
220: * data received.
221: *
222: * @return the offset of the data to be sent or the offset of the
223: * data received.
224: *
225: * @since JDK1.2
226: */
227: public synchronized int getOffset() {
228: return offset;
229: }
230:
231: /**
232: * Returns the length of the data to be sent or the length of the
233: * data received.
234: *
235: * @return the length of the data to be sent or the length of the
236: * data received.
237: * @see #setLength(int)
238: */
239: public synchronized int getLength() {
240: return length;
241: }
242:
243: /**
244: * Set the data buffer for this packet. This sets the
245: * data, length and offset of the packet.
246: *
247: * @param buf the buffer to set for this packet
248: *
249: * @param offset the offset into the data
250: *
251: * @param length the length of the data
252: * and/or the length of the buffer used to receive data
253: *
254: * @exception NullPointerException if the argument is null
255: *
256: * @see #getData
257: * @see #getOffset
258: * @see #getLength
259: *
260: * @since JDK1.2
261: */
262: public synchronized void setData(byte[] buf, int offset, int length) {
263: /* this will check to see if buf is null */
264: if (length < 0 || offset < 0
265: || ((length + offset) > buf.length)) {
266: throw new IllegalArgumentException(
267: "illegal length or offset");
268: }
269: this .buf = buf;
270: this .length = length;
271: this .bufLength = length;
272: this .offset = offset;
273: }
274:
275: /**
276: * Sets the IP address of the machine to which this datagram
277: * is being sent.
278: * @param iaddr the <code>InetAddress</code>
279: * @since JDK1.1
280: * @see #getAddress()
281: */
282: public synchronized void setAddress(InetAddress iaddr) {
283: address = iaddr;
284: }
285:
286: /**
287: * Sets the port number on the remote host to which this datagram
288: * is being sent.
289: * @param iport the port number
290: * @since JDK1.1
291: * @see #getPort()
292: */
293: public synchronized void setPort(int iport) {
294: if (iport < 0 || iport > 0xFFFF) {
295: throw new IllegalArgumentException("Port out of range:"
296: + iport);
297: }
298: port = iport;
299: }
300:
301: /**
302: * Sets the SocketAddress (usually IP address + port number) of the remote
303: * host to which this datagram is being sent.
304: *
305: * @param address the <code>SocketAddress</code>
306: * @throws IllegalArgumentException if address is null or is a
307: * SocketAddress subclass not supported by this socket
308: *
309: * @since 1.4
310: * @see #getSocketAddress
311: */
312: public synchronized void setSocketAddress(SocketAddress address) {
313: if (address == null || !(address instanceof InetSocketAddress))
314: throw new IllegalArgumentException(
315: "unsupported address type");
316: InetSocketAddress addr = (InetSocketAddress) address;
317: setAddress(addr.getAddress());
318: setPort(addr.getPort());
319: }
320:
321: /**
322: * Gets the SocketAddress (usually IP address + port number) of the remote
323: * host that this packet is being sent to or is coming from.
324: *
325: * @return the <code>SocketAddress</code>
326: * @since 1.4
327: * @see #setSocketAddress
328: */
329: public synchronized SocketAddress getSocketAddress() {
330: return new InetSocketAddress(getAddress(), getPort());
331: }
332:
333: /**
334: * Set the data buffer for this packet. With the offset of
335: * this DatagramPacket set to 0, and the length set to
336: * the length of <code>buf</code>.
337: *
338: * @param buf the buffer to set for this packet.
339: *
340: * @exception NullPointerException if the argument is null.
341: *
342: * @see #getLength
343: * @see #getData
344: *
345: * @since JDK1.1
346: */
347: public synchronized void setData(byte[] buf) {
348: if (buf == null) {
349: throw new NullPointerException("null packet buffer");
350: }
351: this .buf = buf;
352: this .offset = 0;
353: this .length = buf.length;
354: this .bufLength = buf.length;
355: }
356:
357: /**
358: * Set the length for this packet. The length of the packet is
359: * the number of bytes from the packet's data buffer that will be
360: * sent, or the number of bytes of the packet's data buffer that
361: * will be used for receiving data. The length must be lesser or
362: * equal to the offset plus the length of the packet's buffer.
363: *
364: * @param length the length to set for this packet.
365: *
366: * @exception IllegalArgumentException if the length is negative
367: * of if the length is greater than the packet's data buffer
368: * length.
369: *
370: * @see #getLength
371: * @see #setData
372: *
373: * @since JDK1.1
374: */
375: public synchronized void setLength(int length) {
376: if ((length + offset) > buf.length || length < 0) {
377: throw new IllegalArgumentException("illegal length");
378: }
379: this .length = length;
380: this .bufLength = this .length;
381: }
382:
383: /**
384: * Perform class load-time initializations.
385: */
386: private native static void init();
387: }
|