1 7 8 package java.net; 9 10 25 public final 26 class DatagramPacket { 27 28 31 static { 32 java.security.AccessController.doPrivileged( 33 new sun.security.action.LoadLibraryAction("net")); 34 init(); 35 } 36 37 41 byte[] buf; 42 int offset; 43 int length; 44 int bufLength; 45 InetAddress address; 46 int port; 47 48 61 public DatagramPacket(byte buf[], int offset, int length) { 62 setData(buf, offset, length); 63 this.address = null; 64 this.port = -1; 65 } 66 67 77 public DatagramPacket(byte buf[], int length) { 78 this (buf, 0, length); 79 } 80 81 97 public DatagramPacket(byte buf[], int offset, int length, 98 InetAddress address, int port) { 99 setData(buf, offset, length); 100 setAddress(address); 101 setPort(port); 102 } 103 104 120 public DatagramPacket(byte buf[], int offset, int length, 121 SocketAddress address) throws SocketException { 122 setData(buf, offset, length); 123 setSocketAddress(address); 124 } 125 126 138 public DatagramPacket(byte buf[], int length, 139 InetAddress address, int port) { 140 this(buf, 0, length, address, port); 141 } 142 143 156 public DatagramPacket(byte buf[], int length, 157 SocketAddress address) throws SocketException { 158 this(buf, 0, length, address); 159 } 160 161 170 public synchronized InetAddress getAddress() { 171 return address; 172 } 173 174 182 public synchronized int getPort() { 183 return port; 184 } 185 186 194 public synchronized byte[] getData() { 195 return buf; 196 } 197 198 207 public synchronized int getOffset() { 208 return offset; 209 } 210 211 219 public synchronized int getLength() { 220 return length; 221 } 222 223 242 public synchronized void setData(byte[] buf, int offset, int length) { 243 244 if (length < 0 || offset < 0 || 245 ((length + offset) > buf.length)) { 246 throw new IllegalArgumentException ("illegal length or offset"); 247 } 248 this.buf = buf; 249 this.length = length; 250 this.bufLength = length; 251 this.offset = offset; 252 } 253 254 261 public synchronized void setAddress(InetAddress iaddr) { 262 address = iaddr; 263 } 264 265 272 public synchronized void setPort(int iport) { 273 if (iport < 0 || iport > 0xFFFF) { 274 throw new IllegalArgumentException ("Port out of range:"+ iport); 275 } 276 port = iport; 277 } 278 279 290 public synchronized void setSocketAddress(SocketAddress address) { 291 if (address == null || !(address instanceof InetSocketAddress )) 292 throw new IllegalArgumentException ("unsupported address type"); 293 InetSocketAddress addr = (InetSocketAddress ) address; 294 if (addr.isUnresolved()) 295 throw new IllegalArgumentException ("unresolved address"); 296 setAddress(addr.getAddress()); 297 setPort(addr.getPort()); 298 } 299 300 308 public synchronized SocketAddress getSocketAddress() { 309 return new InetSocketAddress (getAddress(), getPort()); 310 } 311 312 326 public synchronized void setData(byte[] buf) { 327 if (buf == null) { 328 throw new NullPointerException ("null packet buffer"); 329 } 330 this.buf = buf; 331 this.offset = 0; 332 this.length = buf.length; 333 this.bufLength = buf.length; 334 } 335 336 354 public synchronized void setLength(int length) { 355 if ((length + offset) > buf.length || length < 0) { 356 throw new IllegalArgumentException ("illegal length"); 357 } 358 this.length = length; 359 this.bufLength = this.length; 360 } 361 362 365 private native static void init(); 366 } 367 368 369 370 | Popular Tags |