1. /*
  2. * @(#)DatagramPacket.java 1.26 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. /**
  9. * This class represents a datagram packet.
  10. * <p>
  11. * Datagram packets are used to implement a connectionless packet
  12. * delivery service. Each message is routed from one machine to
  13. * another based solely on information contained within that packet.
  14. * Multiple packets sent from one machine to another might be routed
  15. * differently, and might arrive in any order.
  16. *
  17. * @author Pavani Diwanji
  18. * @author Benjamin Renaud
  19. * @version 1.26, 11/29/01
  20. * @since JDK1.0
  21. */
  22. public final
  23. class DatagramPacket {
  24. /**
  25. * Perform class initialization
  26. */
  27. static {
  28. java.security.AccessController.doPrivileged(
  29. new sun.security.action.LoadLibraryAction("net"));
  30. init();
  31. }
  32. /*
  33. * The fields of this class are package-private since DatagramSocketImpl
  34. * classes needs to access them.
  35. */
  36. byte[] buf;
  37. int offset;
  38. int length;
  39. InetAddress address;
  40. int port;
  41. /**
  42. * Constructs a <code>DatagramPacket</code> for receiving packets of
  43. * length <code>length</code>, specifying an offset into the buffer.
  44. * <p>
  45. * The <code>length</code> argument must be less than or equal to
  46. * <code>buf.length</code>.
  47. *
  48. * @param buf buffer for holding the incoming datagram.
  49. * @param offset the offset for the buffer
  50. * @param length the number of bytes to read.
  51. */
  52. public DatagramPacket(byte buf[], int offset, int length) {
  53. setData(buf, offset, length);
  54. this.address = null;
  55. this.port = -1;
  56. }
  57. /**
  58. * Constructs a <code>DatagramPacket</code> for receiving packets of
  59. * length <code>length</code>.
  60. * <p>
  61. * The <code>length</code> argument must be less than or equal to
  62. * <code>buf.length</code>.
  63. *
  64. * @param buf buffer for holding the incoming datagram.
  65. * @param length the number of bytes to read.
  66. */
  67. public DatagramPacket(byte buf[], int length) {
  68. this (buf, 0, length);
  69. }
  70. /**
  71. * Constructs a datagram packet for sending packets of length
  72. * <code>length</code> with offset <code>ioffset</code>to the
  73. * specified port number on the specified host. The
  74. * <code>length</code> argument must be less than or equal to
  75. * <code>buf.length</code>.
  76. *
  77. * @param buf the packet data.
  78. * @param offset the packet data offset.
  79. * @param length the packet data length.
  80. * @param addr the destination address.
  81. * @param port the destination port number.
  82. * @see java.net.InetAddress
  83. */
  84. public DatagramPacket(byte buf[], int offset, int length,
  85. InetAddress address, int port) {
  86. setData(buf, offset, length);
  87. setAddress(address);
  88. setPort(port);
  89. }
  90. /**
  91. * Constructs a datagram packet for sending packets of length
  92. * <code>length</code> to the specified port number on the specified
  93. * host. The <code>length</code> argument must be less than or equal
  94. * to <code>buf.length</code>.
  95. *
  96. * @param buf the packet data.
  97. * @param length the packet length.
  98. * @param addr the destination address.
  99. * @param port the destination port number.
  100. * @see java.net.InetAddress
  101. */
  102. public DatagramPacket(byte buf[], int length,
  103. InetAddress address, int port) {
  104. this(buf, 0, length, address, port);
  105. }
  106. /**
  107. * Returns the IP address of the machine to which this datagram is being
  108. * sent or from which the datagram was received.
  109. *
  110. * @return the IP address of the machine to which this datagram is being
  111. * sent or from which the datagram was received.
  112. * @see java.net.InetAddress
  113. */
  114. public synchronized InetAddress getAddress() {
  115. return address;
  116. }
  117. /**
  118. * Returns the port number on the remote host to which this datagram is
  119. * being sent or from which the datagram was received.
  120. *
  121. * @return the port number on the remote host to which this datagram is
  122. * being sent or from which the datagram was received.
  123. */
  124. public synchronized int getPort() {
  125. return port;
  126. }
  127. /**
  128. * Returns the data received or the data to be sent.
  129. *
  130. * @return the data received or the data to be sent.
  131. */
  132. public synchronized byte[] getData() {
  133. return buf;
  134. }
  135. /**
  136. * Returns the offset of the data to be sent or the offset of the
  137. * data received.
  138. *
  139. * @return the offset of the data to be sent or the offset of the
  140. * data received.
  141. */
  142. public synchronized int getOffset() {
  143. return offset;
  144. }
  145. /**
  146. * Returns the length of the data to be sent or the length of the
  147. * data received.
  148. *
  149. * @return the length of the data to be sent or the length of the
  150. * data received.
  151. */
  152. public synchronized int getLength() {
  153. return length;
  154. }
  155. /**
  156. * Set the data buffer for this packet. This sets the
  157. * data, length and offset of the packet.
  158. *
  159. * @param buf the buffer to set for this packet
  160. *
  161. * @param offset the offset into the data
  162. *
  163. * @param length the length of the data
  164. *
  165. * @exception NullPointerException if the argument is null
  166. *
  167. * @see #getData
  168. * @see #getOffset
  169. * @see #getLength
  170. *
  171. * @since JDK1.1
  172. */
  173. public synchronized void setData(byte[] buf, int offset, int length) {
  174. /* this will check to see if buf is null */
  175. if (length < 0 || offset < 0 ||
  176. ((length + offset) > buf.length)) {
  177. throw new IllegalArgumentException("illegal length or offset");
  178. }
  179. this.buf = buf;
  180. this.length = length;
  181. this.offset = offset;
  182. }
  183. /**
  184. * @since JDK1.1
  185. */
  186. public synchronized void setAddress(InetAddress iaddr) {
  187. address = iaddr;
  188. }
  189. /**
  190. * @since JDK1.1
  191. */
  192. public synchronized void setPort(int iport) {
  193. if (iport < 0 || iport > 0xFFFF) {
  194. throw new IllegalArgumentException("Port out of range:"+ iport);
  195. }
  196. port = iport;
  197. }
  198. /**
  199. * Set the data buffer for this packet. If the length of the
  200. * packet length is greater than the length of argument to this
  201. * method, the length is reset to the the length of the argument.
  202. *
  203. * @param buf the buffer to set for this packet.
  204. *
  205. * @exception NullPointerException if the argument is null.
  206. *
  207. * @see #getLength
  208. * @see #getData
  209. *
  210. * @since JDK1.1
  211. */
  212. public synchronized void setData(byte[] buf) {
  213. if (buf == null) {
  214. throw new NullPointerException("null packet buffer");
  215. }
  216. this.buf = buf;
  217. if (length > buf.length) {
  218. setLength(buf.length);
  219. }
  220. }
  221. /**
  222. * Set the length for this packet. The length of the packet is
  223. * the number of bytes from the packet's data buffer that will be
  224. * sent, or the number of bytes of the packet's data buffer that
  225. * will be used for receiving data. The length must be lesser or
  226. * equal to the length of the packet's buffer.
  227. *
  228. * @param length the length to set for this packet.
  229. *
  230. * @exception IllegalArgumentException if the length is negative
  231. * of if the length is greater than the packet's data buffer
  232. * length.
  233. *
  234. * @see #getLength
  235. * @see #setData
  236. *
  237. * @since JDK1.1
  238. */
  239. public synchronized void setLength(int length) {
  240. if (length > buf.length || length < 0) {
  241. throw new IllegalArgumentException("illegal length");
  242. }
  243. this.length = length;
  244. }
  245. /**
  246. * Perform class load-time initializations.
  247. */
  248. private native static void init();
  249. }