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