228 *
229 * @return the length of the data to be sent or the length of the
230 * data received.
231 * @see #setLength(int)
232 */
233 public synchronized int getLength() {
234 return length;
235 }
236
237 /**
238 * Set the data buffer for this packet. This sets the
239 * data, length and offset of the packet.
240 *
241 * @param buf the buffer to set for this packet
242 *
243 * @param offset the offset into the data
244 *
245 * @param length the length of the data
246 * and/or the length of the buffer used to receive data
247 *
248 * @exception NullPointerException if the argument is null
249 *
250 * @see #getData
251 * @see #getOffset
252 * @see #getLength
253 *
254 * @since 1.2
255 */
256 public synchronized void setData(byte[] buf, int offset, int length) {
257 /* this will check to see if buf is null */
258 if (length < 0 || offset < 0 ||
259 (length + offset) < 0 ||
260 ((length + offset) > buf.length)) {
261 throw new IllegalArgumentException("illegal length or offset");
262 }
263 this.buf = buf;
264 this.length = length;
265 this.bufLength = length;
266 this.offset = offset;
267 }
268
314
315 /**
316 * Gets the SocketAddress (usually IP address + port number) of the remote
317 * host that this packet is being sent to or is coming from.
318 *
319 * @return the {@code SocketAddress}
320 * @since 1.4
321 * @see #setSocketAddress
322 */
323 public synchronized SocketAddress getSocketAddress() {
324 return new InetSocketAddress(getAddress(), getPort());
325 }
326
327 /**
328 * Set the data buffer for this packet. With the offset of
329 * this DatagramPacket set to 0, and the length set to
330 * the length of {@code buf}.
331 *
332 * @param buf the buffer to set for this packet.
333 *
334 * @exception NullPointerException if the argument is null.
335 *
336 * @see #getLength
337 * @see #getData
338 *
339 * @since 1.1
340 */
341 public synchronized void setData(byte[] buf) {
342 if (buf == null) {
343 throw new NullPointerException("null packet buffer");
344 }
345 this.buf = buf;
346 this.offset = 0;
347 this.length = buf.length;
348 this.bufLength = buf.length;
349 }
350
351 /**
352 * Set the length for this packet. The length of the packet is
353 * the number of bytes from the packet's data buffer that will be
354 * sent, or the number of bytes of the packet's data buffer that
355 * will be used for receiving data. The length must be lesser or
356 * equal to the offset plus the length of the packet's buffer.
357 *
358 * @param length the length to set for this packet.
359 *
360 * @exception IllegalArgumentException if the length is negative
361 * of if the length is greater than the packet's data buffer
362 * length.
363 *
364 * @see #getLength
365 * @see #setData
366 *
367 * @since 1.1
368 */
369 public synchronized void setLength(int length) {
370 if ((length + offset) > buf.length || length < 0 ||
371 (length + offset) < 0) {
372 throw new IllegalArgumentException("illegal length");
373 }
374 this.length = length;
375 this.bufLength = this.length;
376 }
377
378 /**
379 * Perform class load-time initializations.
380 */
|
228 *
229 * @return the length of the data to be sent or the length of the
230 * data received.
231 * @see #setLength(int)
232 */
233 public synchronized int getLength() {
234 return length;
235 }
236
237 /**
238 * Set the data buffer for this packet. This sets the
239 * data, length and offset of the packet.
240 *
241 * @param buf the buffer to set for this packet
242 *
243 * @param offset the offset into the data
244 *
245 * @param length the length of the data
246 * and/or the length of the buffer used to receive data
247 *
248 * @throws NullPointerException if the argument is null
249 *
250 * @see #getData
251 * @see #getOffset
252 * @see #getLength
253 *
254 * @since 1.2
255 */
256 public synchronized void setData(byte[] buf, int offset, int length) {
257 /* this will check to see if buf is null */
258 if (length < 0 || offset < 0 ||
259 (length + offset) < 0 ||
260 ((length + offset) > buf.length)) {
261 throw new IllegalArgumentException("illegal length or offset");
262 }
263 this.buf = buf;
264 this.length = length;
265 this.bufLength = length;
266 this.offset = offset;
267 }
268
314
315 /**
316 * Gets the SocketAddress (usually IP address + port number) of the remote
317 * host that this packet is being sent to or is coming from.
318 *
319 * @return the {@code SocketAddress}
320 * @since 1.4
321 * @see #setSocketAddress
322 */
323 public synchronized SocketAddress getSocketAddress() {
324 return new InetSocketAddress(getAddress(), getPort());
325 }
326
327 /**
328 * Set the data buffer for this packet. With the offset of
329 * this DatagramPacket set to 0, and the length set to
330 * the length of {@code buf}.
331 *
332 * @param buf the buffer to set for this packet.
333 *
334 * @throws NullPointerException if the argument is null.
335 *
336 * @see #getLength
337 * @see #getData
338 *
339 * @since 1.1
340 */
341 public synchronized void setData(byte[] buf) {
342 if (buf == null) {
343 throw new NullPointerException("null packet buffer");
344 }
345 this.buf = buf;
346 this.offset = 0;
347 this.length = buf.length;
348 this.bufLength = buf.length;
349 }
350
351 /**
352 * Set the length for this packet. The length of the packet is
353 * the number of bytes from the packet's data buffer that will be
354 * sent, or the number of bytes of the packet's data buffer that
355 * will be used for receiving data. The length must be lesser or
356 * equal to the offset plus the length of the packet's buffer.
357 *
358 * @param length the length to set for this packet.
359 *
360 * @throws IllegalArgumentException if the length is negative
361 * of if the length is greater than the packet's data buffer
362 * length.
363 *
364 * @see #getLength
365 * @see #setData
366 *
367 * @since 1.1
368 */
369 public synchronized void setLength(int length) {
370 if ((length + offset) > buf.length || length < 0 ||
371 (length + offset) < 0) {
372 throw new IllegalArgumentException("illegal length");
373 }
374 this.length = length;
375 this.bufLength = this.length;
376 }
377
378 /**
379 * Perform class load-time initializations.
380 */
|