diff -urN /home/mkoch/src/cvs/classpath/java/net/DatagramPacket.java /home/mkoch/src/classpath/java/net/DatagramPacket.java --- /home/mkoch/src/cvs/classpath/java/net/DatagramPacket.java 2003-11-25 11:15:13.000000000 +0100 +++ /home/mkoch/src/classpath/java/net/DatagramPacket.java 2003-12-04 16:44:14.000000000 +0100 @@ -78,13 +78,18 @@ private int offset; /** - * The length of the data buffer to send + * The length of the data buffer to send. */ - private int length; + int length; /** + * The maximal length of the buffer. + */ + int maxlen; + + /** * The address to which the packet should be sent or from which it - * was received + * was received. */ private InetAddress address; @@ -106,21 +111,9 @@ */ public DatagramPacket(byte[] buf, int offset, int length) { - if (buf == null) - throw new NullPointerException("Null buffer"); - if (offset < 0) - throw new IllegalArgumentException("Invalid offset: " + offset); - if (length < 0) - throw new IllegalArgumentException("Invalid length: " + length); - if (offset + length > buf.length) - throw new IllegalArgumentException("Potential buffer overflow - offset: " - + offset + " length: " + length); - - buffer = buf; - this.offset = offset; - this.length = length; - this.address = null; - this.port = -1; + setData(buf, offset, length); + address = null; + port = -1; } /** @@ -150,25 +143,9 @@ public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) { - if (buf == null) - throw new NullPointerException("Null buffer"); - if (offset < 0) - throw new IllegalArgumentException("Invalid offset: " + offset); - if (length < 0) - throw new IllegalArgumentException("Invalid length: " + length); - if (offset + length > buf.length) - throw new IllegalArgumentException("Potential buffer overflow - offset: " - + offset + " length: " + length); - if (port < 0 || port > 65535) - throw new IllegalArgumentException("Invalid port: " + port); - if (address == null) - throw new NullPointerException("Null address"); - - buffer = buf; - this.offset = offset; - this.length = length; - this.address = address; - this.port = port; + setData(buf, offset, length); + setAddress(address); + setPort(port); } /** @@ -203,8 +180,13 @@ SocketAddress address) throws SocketException { - this(buf, offset, length, ((InetSocketAddress)address).getAddress(), - ((InetSocketAddress)address).getPort()); + if (! (address instanceof InetSocketAddress)) + throw new IllegalArgumentException("unsupported address type"); + + InetSocketAddress tmp = (InetSocketAddress) address; + setData(buf, offset, length); + setAddress(tmp.getAddress()); + setPort(tmp.getPort()); } /** @@ -223,8 +205,7 @@ public DatagramPacket(byte[] buf, int length, SocketAddress address) throws SocketException { - this(buf, 0, length, ((InetSocketAddress)address).getAddress(), - ((InetSocketAddress)address).getPort()); + this(buf, 0, length, address); } /** @@ -330,9 +311,10 @@ public void setSocketAddress(SocketAddress address) throws IllegalArgumentException { - if (address == null) throw new IllegalArgumentException(); + if (address == null) + throw new IllegalArgumentException("address may not be null"); - InetSocketAddress tmp = (InetSocketAddress)address; + InetSocketAddress tmp = (InetSocketAddress) address; this.address = tmp.getAddress(); this.port = tmp.getPort(); } @@ -359,14 +341,9 @@ * * @since 1.1 */ - public synchronized void setData(byte[] buf) + public void setData(byte[] buf) { - // This form of setData requires setLength to be called separately - // and subsequently. - if (buf == null) - throw new NullPointerException("Null buffer"); - - buffer = buf; + setData(buf, 0, buf.length); } /** @@ -388,15 +365,10 @@ throw new NullPointerException("Null buffer"); if (offset < 0) throw new IllegalArgumentException("Invalid offset: " + offset); - if (length < 0) - throw new IllegalArgumentException("Invalid length: " + length); - if (offset + length > buf.length) - throw new IllegalArgumentException("Potential buffer overflow - offset: " - + offset + " length: " + length); buffer = buf; this.offset = offset; - this.length = length; + setLength(length); } /** @@ -418,6 +390,6 @@ + offset + " length: " + length); this.length = length; + this.maxlen = length; } -} // class DatagramPacket - +} diff -urN /home/mkoch/src/cvs/classpath/native/jni/java-net/gnu_java_net_PlainDatagramSocketImpl.c /home/mkoch/src/classpath/native/jni/java-net/gnu_java_net_PlainDatagramSocketImpl.c --- /home/mkoch/src/cvs/classpath/native/jni/java-net/gnu_java_net_PlainDatagramSocketImpl.c 2003-12-02 09:13:17.000000000 +0100 +++ /home/mkoch/src/classpath/native/jni/java-net/gnu_java_net_PlainDatagramSocketImpl.c 2003-12-04 16:42:33.000000000 +0100 @@ -163,6 +163,7 @@ #ifndef WITHOUT_NETWORK unsigned int addr, port, maxlen, offset, bytes_read; jclass cls, addr_cls; + jfieldID fid; jmethodID mid; jarray arr; unsigned char octets[4]; @@ -224,7 +225,14 @@ DBG("PlainDatagramSocketImpl.receive(): Got the offset\n"); /* Now get the maximal available length from the packet */ - maxlen = (*env)->GetArrayLength(env,arr) - offset; + fid = (*env)->GetFieldID(env, cls, "maxlen", "I"); + if (fid == NULL) + { + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: maxlen"); + return; + } + + maxlen = (*env)->GetIntField(env, packet, fid); /* Receive the packet */ /* should we try some sort of validation on the length? */ @@ -318,20 +326,15 @@ DBG("PlainDatagramSocketImpl.receive(): Stored the port\n"); /* Store back the length */ - mid = (*env)->GetMethodID(env, cls, "setLength", "(I)V"); - if (mid == NULL) + fid = (*env)->GetFieldID(env, cls, "length", "I"); + if (fid == NULL) { - JCL_ThrowException(env, IO_EXCEPTION, "Internal error: setLength"); - return; - } - - (*env)->CallVoidMethod(env, packet, mid, bytes_read); - if ((*env)->ExceptionOccurred(env)) - { - JCL_ThrowException(env, IO_EXCEPTION, "Internal error: call setLength"); + JCL_ThrowException(env, IO_EXCEPTION, "Internal error: length"); return; } + (*env)->SetIntField(env, packet, fid, bytes_read); + DBG("PlainDatagramSocketImpl.receive(): Stored the length\n"); #else /* not WITHOUT_NETWORK */ #endif /* not WITHOUT_NETWORK */