1. /*
  2. * @(#)DatagramPacket.java 1.43 03/12/19
  3. *
  4. * Copyright 2004 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. Packet delivery is
  16. * not guaranteed.
  17. *
  18. * @author Pavani Diwanji
  19. * @author Benjamin Renaud
  20. * @version 1.43, 12/19/03
  21. * @since JDK1.0
  22. */
  23. public final
  24. class DatagramPacket {
  25. /**
  26. * Perform class initialization
  27. */
  28. static {
  29. java.security.AccessController.doPrivileged(
  30. new sun.security.action.LoadLibraryAction("net"));
  31. init();
  32. }
  33. /*
  34. * The fields of this class are package-private since DatagramSocketImpl
  35. * classes needs to access them.
  36. */
  37. byte[] buf;
  38. int offset;
  39. int length;
  40. int bufLength;
  41. InetAddress address;
  42. int port;
  43. /**
  44. * Constructs a <code>DatagramPacket</code> for receiving packets of
  45. * length <code>length</code>, specifying an offset into the buffer.
  46. * <p>
  47. * The <code>length</code> argument must be less than or equal to
  48. * <code>buf.length</code>.
  49. *
  50. * @param buf buffer for holding the incoming datagram.
  51. * @param offset the offset for the buffer
  52. * @param length the number of bytes to read.
  53. *
  54. * @since JDK1.2
  55. */
  56. public DatagramPacket(byte buf[], int offset, int length) {
  57. setData(buf, offset, length);
  58. this.address = null;
  59. this.port = -1;
  60. }
  61. /**
  62. * Constructs a <code>DatagramPacket</code> for receiving packets of
  63. * length <code>length</code>.
  64. * <p>
  65. * The <code>length</code> argument must be less than or equal to
  66. * <code>buf.length</code>.
  67. *
  68. * @param buf buffer for holding the incoming datagram.
  69. * @param length the number of bytes to read.
  70. */
  71. public DatagramPacket(byte buf[], int length) {
  72. this (buf, 0, length);
  73. }
  74. /**
  75. * Constructs a datagram packet for sending packets of length
  76. * <code>length</code> with offset <code>ioffset</code>to the
  77. * specified port number on the specified host. The
  78. * <code>length</code> argument must be less than or equal to
  79. * <code>buf.length</code>.
  80. *
  81. * @param buf the packet data.
  82. * @param offset the packet data offset.
  83. * @param length the packet data length.
  84. * @param address the destination address.
  85. * @param port the destination port number.
  86. * @see java.net.InetAddress
  87. *
  88. * @since JDK1.2
  89. */
  90. public DatagramPacket(byte buf[], int offset, int length,
  91. InetAddress address, int port) {
  92. setData(buf, offset, length);
  93. setAddress(address);
  94. setPort(port);
  95. }
  96. /**
  97. * Constructs a datagram packet for sending packets of length
  98. * <code>length</code> with offset <code>ioffset</code>to the
  99. * specified port number on the specified host. The
  100. * <code>length</code> argument must be less than or equal to
  101. * <code>buf.length</code>.
  102. *
  103. * @param buf the packet data.
  104. * @param offset the packet data offset.
  105. * @param length the packet data length.
  106. * @param address the destination socket address.
  107. * @throws IllegalArgumentException if address type is not supported
  108. * @see java.net.InetAddress
  109. *
  110. * @since 1.4
  111. */
  112. public DatagramPacket(byte buf[], int offset, int length,
  113. SocketAddress address) throws SocketException {
  114. setData(buf, offset, length);
  115. setSocketAddress(address);
  116. }
  117. /**
  118. * Constructs a datagram packet for sending packets of length
  119. * <code>length</code> to the specified port number on the specified
  120. * host. The <code>length</code> argument must be less than or equal
  121. * to <code>buf.length</code>.
  122. *
  123. * @param buf the packet data.
  124. * @param length the packet length.
  125. * @param address the destination address.
  126. * @param port the destination port number.
  127. * @see java.net.InetAddress
  128. */
  129. public DatagramPacket(byte buf[], int length,
  130. InetAddress address, int port) {
  131. this(buf, 0, length, address, port);
  132. }
  133. /**
  134. * Constructs a datagram packet for sending packets of length
  135. * <code>length</code> to the specified port number on the specified
  136. * host. The <code>length</code> argument must be less than or equal
  137. * to <code>buf.length</code>.
  138. *
  139. * @param buf the packet data.
  140. * @param length the packet length.
  141. * @param address the destination address.
  142. * @throws IllegalArgumentException if address type is not supported
  143. * @since 1.4
  144. * @see java.net.InetAddress
  145. */
  146. public DatagramPacket(byte buf[], int length,
  147. SocketAddress address) throws SocketException {
  148. this(buf, 0, length, address);
  149. }
  150. /**
  151. * Returns the IP address of the machine to which this datagram is being
  152. * sent or from which the datagram was received.
  153. *
  154. * @return the IP address of the machine to which this datagram is being
  155. * sent or from which the datagram was received.
  156. * @see java.net.InetAddress
  157. * @see #setAddress(java.net.InetAddress)
  158. */
  159. public synchronized InetAddress getAddress() {
  160. return address;
  161. }
  162. /**
  163. * Returns the port number on the remote host to which this datagram is
  164. * being sent or from which the datagram was received.
  165. *
  166. * @return the port number on the remote host to which this datagram is
  167. * being sent or from which the datagram was received.
  168. * @see #setPort(int)
  169. */
  170. public synchronized int getPort() {
  171. return port;
  172. }
  173. /**
  174. * Returns the data buffer. The data received or the data to be sent
  175. * starts from the <code>offset</code> in the buffer,
  176. * and runs for <code>length</code> long.
  177. *
  178. * @return the buffer used to receive or send data
  179. * @see #setData(byte[], int, int)
  180. */
  181. public synchronized byte[] getData() {
  182. return buf;
  183. }
  184. /**
  185. * Returns the offset of the data to be sent or the offset of the
  186. * data received.
  187. *
  188. * @return the offset of the data to be sent or the offset of the
  189. * data received.
  190. *
  191. * @since JDK1.2
  192. */
  193. public synchronized int getOffset() {
  194. return offset;
  195. }
  196. /**
  197. * Returns the length of the data to be sent or the length of the
  198. * data received.
  199. *
  200. * @return the length of the data to be sent or the length of the
  201. * data received.
  202. * @see #setLength(int)
  203. */
  204. public synchronized int getLength() {
  205. return length;
  206. }
  207. /**
  208. * Set the data buffer for this packet. This sets the
  209. * data, length and offset of the packet.
  210. *
  211. * @param buf the buffer to set for this packet
  212. *
  213. * @param offset the offset into the data
  214. *
  215. * @param length the length of the data
  216. * and/or the length of the buffer used to receive data
  217. *
  218. * @exception NullPointerException if the argument is null
  219. *
  220. * @see #getData
  221. * @see #getOffset
  222. * @see #getLength
  223. *
  224. * @since JDK1.2
  225. */
  226. public synchronized void setData(byte[] buf, int offset, int length) {
  227. /* this will check to see if buf is null */
  228. if (length < 0 || offset < 0 ||
  229. ((length + offset) > buf.length)) {
  230. throw new IllegalArgumentException("illegal length or offset");
  231. }
  232. this.buf = buf;
  233. this.length = length;
  234. this.bufLength = length;
  235. this.offset = offset;
  236. }
  237. /**
  238. * Sets the IP address of the machine to which this datagram
  239. * is being sent.
  240. * @param iaddr the <code>InetAddress</code>
  241. * @since JDK1.1
  242. * @see #getAddress()
  243. */
  244. public synchronized void setAddress(InetAddress iaddr) {
  245. address = iaddr;
  246. }
  247. /**
  248. * Sets the port number on the remote host to which this datagram
  249. * is being sent.
  250. * @param iport the port number
  251. * @since JDK1.1
  252. * @see #getPort()
  253. */
  254. public synchronized void setPort(int iport) {
  255. if (iport < 0 || iport > 0xFFFF) {
  256. throw new IllegalArgumentException("Port out of range:"+ iport);
  257. }
  258. port = iport;
  259. }
  260. /**
  261. * Sets the SocketAddress (usually IP address + port number) of the remote
  262. * host to which this datagram is being sent.
  263. *
  264. * @param address the <code>SocketAddress</code>
  265. * @throws IllegalArgumentException if address is null or is a
  266. * SocketAddress subclass not supported by this socket
  267. *
  268. * @since 1.4
  269. * @see #getSocketAddress
  270. */
  271. public synchronized void setSocketAddress(SocketAddress address) {
  272. if (address == null || !(address instanceof InetSocketAddress))
  273. throw new IllegalArgumentException("unsupported address type");
  274. InetSocketAddress addr = (InetSocketAddress) address;
  275. setAddress(addr.getAddress());
  276. setPort(addr.getPort());
  277. }
  278. /**
  279. * Gets the SocketAddress (usually IP address + port number) of the remote
  280. * host that this packet is being sent to or is coming from.
  281. *
  282. * @return the <code>SocketAddress</code>
  283. * @since 1.4
  284. * @see #setSocketAddress
  285. */
  286. public synchronized SocketAddress getSocketAddress() {
  287. return new InetSocketAddress(getAddress(), getPort());
  288. }
  289. /**
  290. * Set the data buffer for this packet. With the offset of
  291. * this DatagramPacket set to 0, and the length set to
  292. * the length of <code>buf</code>.
  293. *
  294. * @param buf the buffer to set for this packet.
  295. *
  296. * @exception NullPointerException if the argument is null.
  297. *
  298. * @see #getLength
  299. * @see #getData
  300. *
  301. * @since JDK1.1
  302. */
  303. public synchronized void setData(byte[] buf) {
  304. if (buf == null) {
  305. throw new NullPointerException("null packet buffer");
  306. }
  307. this.buf = buf;
  308. this.offset = 0;
  309. this.length = buf.length;
  310. this.bufLength = buf.length;
  311. }
  312. /**
  313. * Set the length for this packet. The length of the packet is
  314. * the number of bytes from the packet's data buffer that will be
  315. * sent, or the number of bytes of the packet's data buffer that
  316. * will be used for receiving data. The length must be lesser or
  317. * equal to the offset plus the length of the packet's buffer.
  318. *
  319. * @param length the length to set for this packet.
  320. *
  321. * @exception IllegalArgumentException if the length is negative
  322. * of if the length is greater than the packet's data buffer
  323. * length.
  324. *
  325. * @see #getLength
  326. * @see #setData
  327. *
  328. * @since JDK1.1
  329. */
  330. public synchronized void setLength(int length) {
  331. if ((length + offset) > buf.length || length < 0) {
  332. throw new IllegalArgumentException("illegal length");
  333. }
  334. this.length = length;
  335. this.bufLength = this.length;
  336. }
  337. /**
  338. * Perform class load-time initializations.
  339. */
  340. private native static void init();
  341. }