1 // DatagramPacket.java - Represents packets in a connectionless protocol.
3 /* Copyright (C) 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Warren Levy <warrenl@cygnus.com>
15 * @date April 28, 1999.
19 * Written using on-line Java Platform 1.2 API Specification, as well
20 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
21 * Status: Believed complete and correct.
24 public final class DatagramPacket
26 private byte[] buffer;
29 private InetAddress address;
33 public DatagramPacket(byte[] buf, int offset, int length)
36 throw new NullPointerException("Null buffer");
38 throw new IllegalArgumentException("Invalid offset: " + offset);
40 throw new IllegalArgumentException("Invalid length: " + length);
41 if (offset + length > buf.length)
42 throw new IllegalArgumentException("Potential buffer overflow - offset: "
43 + offset + " length: " + length);
52 public DatagramPacket(byte[] buf, int length)
58 public DatagramPacket(byte[] buf, int offset, int length,
59 InetAddress address, int port)
62 throw new NullPointerException("Null buffer");
64 throw new IllegalArgumentException("Invalid offset: " + offset);
66 throw new IllegalArgumentException("Invalid length: " + length);
67 if (offset + length > buf.length)
68 throw new IllegalArgumentException("Potential buffer overflow - offset: "
69 + offset + " length: " + length);
70 if (port < 0 || port > 65535)
71 throw new IllegalArgumentException("Invalid port: " + port);
73 throw new NullPointerException("Null address");
78 this.address = address;
82 public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
84 this(buf, 0, length, address, port);
87 public synchronized InetAddress getAddress()
92 public synchronized int getPort()
97 public synchronized byte[] getData()
103 public synchronized int getOffset()
108 public synchronized int getLength()
113 public synchronized void setAddress(InetAddress iaddr)
116 throw new NullPointerException("Null address");
121 public synchronized void setPort(int iport)
123 if (iport < 0 || iport > 65535)
124 throw new IllegalArgumentException("Invalid port: " + iport);
129 public synchronized void setData(byte[] buf)
131 // This form of setData requires setLength to be called separately
134 throw new NullPointerException("Null buffer");
140 public synchronized void setData(byte[] buf, int offset, int length)
142 // This form of setData must be used if offset is to be changed.
145 throw new NullPointerException("Null buffer");
147 throw new IllegalArgumentException("Invalid offset: " + offset);
149 throw new IllegalArgumentException("Invalid length: " + length);
150 if (offset + length > buf.length)
151 throw new IllegalArgumentException("Potential buffer overflow - offset: "
152 + offset + " length: " + length);
155 this.offset = offset;
156 this.length = length;
159 public synchronized void setLength(int length)
162 throw new IllegalArgumentException("Invalid length: " + length);
163 if (offset + length > buffer.length)
164 throw new IllegalArgumentException("Potential buffer overflow - offset: "
165 + offset + " length: " + length);
167 this.length = length;