PlainSocketImpl.java: Reworked imports.

2002-11-18  Michael Koch <konqueror@gmx.de>

	* java/net/PlainSocketImpl.java: Reworked imports.
	* java/net/ServerSocket.java
	(ServerSocket): Create socket.
	* java/net/SocketAddress.java: Documentation added.
	* java/net/natPlainSocketImpl.cc: Reindented.
	* java/nio/ReadOnlyBufferException.java: New file
	* java/nio/channels/ClosedChannelException.java: Documentation added.
	* java/nio/channels/ClosedSelectorException.java: New file.

From-SVN: r59214
This commit is contained in:
Michael Koch 2002-11-18 13:22:55 +00:00 committed by Michael Koch
parent 3503150c4c
commit 6602dd4a3b
8 changed files with 395 additions and 241 deletions

View file

@ -1,3 +1,14 @@
2002-11-18 Michael Koch <konqueror@gmx.de>
* java/net/PlainSocketImpl.java: Reworked imports.
* java/net/ServerSocket.java
(ServerSocket): Create socket.
* java/net/SocketAddress.java: Documentation added.
* java/net/natPlainSocketImpl.cc: Reindented.
* java/nio/ReadOnlyBufferException.java: New file
* java/nio/channels/ClosedChannelException.java: Documentation added.
* java/nio/channels/ClosedSelectorException.java: New file.
2002-11-17 Mark Wielaard <mark@klomp.org> 2002-11-17 Mark Wielaard <mark@klomp.org>
* java/net/HttpURLConnection.java ((getPermission): Take port * java/net/HttpURLConnection.java ((getPermission): Take port

View file

@ -9,8 +9,8 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */ details. */
package java.net; package java.net;
import java.io.*;
import java.io.IOException;
/** /**
* The standard GCJ socket implementation. * The standard GCJ socket implementation.

View file

@ -60,16 +60,11 @@ import java.nio.channels.ServerSocketChannel;
*/ */
public class ServerSocket public class ServerSocket
{ {
// Class Variables
/** /**
* This is the user defined SocketImplFactory, if one is supplied * This is the user defined SocketImplFactory, if one is supplied
*/ */
private static SocketImplFactory factory; private static SocketImplFactory factory;
// Instance Variables
/** /**
* This is the SocketImp object to which most instance methods in this * This is the SocketImp object to which most instance methods in this
* class are redirected * class are redirected
@ -95,6 +90,8 @@ public class ServerSocket
impl = factory.createSocketImpl(); impl = factory.createSocketImpl();
else else
impl = new PlainSocketImpl(); impl = new PlainSocketImpl();
impl.create(true);
} }
/** /**

View file

@ -37,20 +37,23 @@ exception statement from your version. */
package java.net; package java.net;
import java.io.*; import java.io.Serializable;
/** /**
* Abstract base class for InetSocketAddress. * Abstract base class for InetSocketAddress.
* InetSocketAddress is to my knowledge the only derived * InetSocketAddress is to my knowledge the only derived
* class. [Ronald] * class. [Ronald]
*
* @since 1.4
*/ */
public abstract class SocketAddress implements Serializable public abstract class SocketAddress implements Serializable
{ {
static final long serialVersionUID = 5215720748342549866L; static final long serialVersionUID = 5215720748342549866L;
/**
* Initializes the socket address.
*/
public SocketAddress() public SocketAddress()
{ {
} }
} }

View file

@ -259,6 +259,7 @@ void
java::net::PlainSocketImpl::create (jboolean stream) java::net::PlainSocketImpl::create (jboolean stream)
{ {
int sock = ::socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0); int sock = ::socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
if (sock < 0) if (sock < 0)
{ {
char* strerr = strerror (errno); char* strerr = strerror (errno);
@ -285,10 +286,12 @@ java::net::PlainSocketImpl::bind (java::net::InetAddress *host, jint lport)
if (len == 4) if (len == 4)
{ {
u.address.sin_family = AF_INET; u.address.sin_family = AF_INET;
if (host != NULL) if (host != NULL)
memcpy (&u.address.sin_addr, bytes, len); memcpy (&u.address.sin_addr, bytes, len);
else else
u.address.sin_addr.s_addr = htonl (INADDR_ANY); u.address.sin_addr.s_addr = htonl (INADDR_ANY);
len = sizeof (struct sockaddr_in); len = sizeof (struct sockaddr_in);
u.address.sin_port = htons (lport); u.address.sin_port = htons (lport);
} }
@ -311,14 +314,17 @@ java::net::PlainSocketImpl::bind (java::net::InetAddress *host, jint lport)
{ {
address = host; address = host;
socklen_t addrlen = sizeof(u); socklen_t addrlen = sizeof(u);
if (lport != 0) if (lport != 0)
localport = lport; localport = lport;
else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0) else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
localport = ntohs (u.address.sin_port); localport = ntohs (u.address.sin_port);
else else
goto error; goto error;
return; return;
} }
error: error:
char* strerr = strerror (errno); char* strerr = strerror (errno);
throw new java::net::BindException (JvNewStringUTF (strerr)); throw new java::net::BindException (JvNewStringUTF (strerr));
@ -378,8 +384,8 @@ java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr,
if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0) if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
goto error; goto error;
else if (retval == 0) else if (retval == 0)
throw new java::net::SocketTimeoutException ( throw new java::net::SocketTimeoutException
JvNewStringUTF("Connect timed out")); (JvNewStringUTF ("Connect timed out"));
} }
else else
#endif #endif
@ -390,6 +396,7 @@ java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr,
address = host; address = host;
port = rport; port = rport;
// A bind may not have been done on this socket; if so, set localport now. // A bind may not have been done on this socket; if so, set localport now.
if (localport == 0) if (localport == 0)
{ {
@ -398,7 +405,9 @@ java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr,
else else
goto error; goto error;
} }
return; return;
error: error:
char* strerr = strerror (errno); char* strerr = strerror (errno);
throw new java::net::ConnectException (JvNewStringUTF (strerr)); throw new java::net::ConnectException (JvNewStringUTF (strerr));
@ -442,6 +451,7 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
#endif /* WIN32 */ #endif /* WIN32 */
new_socket = _Jv_accept (fnum, (sockaddr*) &u, &addrlen); new_socket = _Jv_accept (fnum, (sockaddr*) &u, &addrlen);
if (new_socket < 0) if (new_socket < 0)
goto error; goto error;
@ -471,6 +481,7 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
s->address = new InetAddress (raddr, NULL); s->address = new InetAddress (raddr, NULL);
s->port = rport; s->port = rport;
return; return;
error: error:
char* strerr = strerror (errno); char* strerr = strerror (errno);
throw new java::io::IOException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr));
@ -537,9 +548,11 @@ java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
jbyte *bytes = elements (b) + offset; jbyte *bytes = elements (b) + offset;
int written = 0; int written = 0;
while (len > 0) while (len > 0)
{ {
int r = ::write (fnum, bytes, len); int r = ::write (fnum, bytes, len);
if (r == -1) if (r == -1)
{ {
if (java::lang::Thread::interrupted()) if (java::lang::Thread::interrupted())
@ -555,6 +568,7 @@ java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
throw new java::io::IOException (JvNewStringUTF (strerror (errno))); throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
break; break;
} }
written += r; written += r;
len -= r; len -= r;
bytes += r; bytes += r;
@ -604,6 +618,7 @@ java::net::PlainSocketImpl::read(void)
if (r == 0) if (r == 0)
return -1; return -1;
if (java::lang::Thread::interrupted()) if (java::lang::Thread::interrupted())
{ {
java::io::InterruptedIOException *iioe = java::io::InterruptedIOException *iioe =
@ -617,9 +632,11 @@ java::net::PlainSocketImpl::read(void)
// Some errors cause us to return end of stream... // Some errors cause us to return end of stream...
if (errno == ENOTCONN) if (errno == ENOTCONN)
return -1; return -1;
// Other errors need to be signalled. // Other errors need to be signalled.
throw new java::io::IOException (JvNewStringUTF (strerror (errno))); throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
} }
return b & 0xFF; return b & 0xFF;
} }
@ -629,9 +646,12 @@ java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
{ {
if (! buffer) if (! buffer)
throw new java::lang::NullPointerException; throw new java::lang::NullPointerException;
jsize bsize = JvGetArrayLength (buffer); jsize bsize = JvGetArrayLength (buffer);
if (offset < 0 || count < 0 || offset + count > bsize) if (offset < 0 || count < 0 || offset + count > bsize)
throw new java::lang::ArrayIndexOutOfBoundsException; throw new java::lang::ArrayIndexOutOfBoundsException;
jbyte *bytes = elements (buffer) + offset; jbyte *bytes = elements (buffer) + offset;
// FIXME: implement timeout support for Win32 // FIXME: implement timeout support for Win32
@ -666,8 +686,10 @@ java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
// Read the socket. // Read the socket.
int r = ::recv (fnum, (char *) bytes, count, 0); int r = ::recv (fnum, (char *) bytes, count, 0);
if (r == 0) if (r == 0)
return -1; return -1;
if (java::lang::Thread::interrupted()) if (java::lang::Thread::interrupted())
{ {
java::io::InterruptedIOException *iioe = java::io::InterruptedIOException *iioe =
@ -681,9 +703,11 @@ java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
// Some errors cause us to return end of stream... // Some errors cause us to return end of stream...
if (errno == ENOTCONN) if (errno == ENOTCONN)
return -1; return -1;
// Other errors need to be signalled. // Other errors need to be signalled.
throw new java::io::IOException (JvNewStringUTF (strerror (errno))); throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
} }
return r; return r;
} }
@ -698,6 +722,7 @@ java::net::PlainSocketImpl::available(void)
#if defined(FIONREAD) #if defined(FIONREAD)
r = ::ioctl (fnum, FIONREAD, &num); r = ::ioctl (fnum, FIONREAD, &num);
if (r == -1 && errno == ENOTTY) if (r == -1 && errno == ENOTTY)
{ {
// If the ioctl doesn't work, we don't care. // If the ioctl doesn't work, we don't care.
@ -718,7 +743,6 @@ java::net::PlainSocketImpl::available(void)
{ {
posix_error: posix_error:
throw new java::io::IOException(JvNewStringUTF(strerror(errno))); throw new java::io::IOException(JvNewStringUTF(strerror(errno)));
} }
// If we didn't get anything we can use select. // If we didn't get anything we can use select.
@ -744,8 +768,7 @@ java::net::PlainSocketImpl::available(void)
#else #else
throw new java::io::IOException (JvNewStringUTF ("unimplemented")); throw new java::io::IOException (JvNewStringUTF ("unimplemented"));
#endif #endif
} }
void void
java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value) java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
@ -790,8 +813,8 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
val_len) != 0) val_len) != 0)
goto error; goto error;
#else #else
throw new java::lang::InternalError ( throw new java::lang::InternalError
JvNewStringUTF ("TCP_NODELAY not supported")); (JvNewStringUTF ("TCP_NODELAY not supported"));
#endif /* TCP_NODELAY */ #endif /* TCP_NODELAY */
return; return;
@ -802,8 +825,8 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
break; break;
case _Jv_SO_BROADCAST_ : case _Jv_SO_BROADCAST_ :
throw new java::net::SocketException ( throw new java::net::SocketException
JvNewStringUTF ("SO_BROADCAST not valid for TCP")); (JvNewStringUTF ("SO_BROADCAST not valid for TCP"));
break; break;
case _Jv_SO_OOBINLINE_ : case _Jv_SO_OOBINLINE_ :
@ -817,6 +840,7 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
struct linger l_val; struct linger l_val;
l_val.l_onoff = (val != -1); l_val.l_onoff = (val != -1);
l_val.l_linger = val; l_val.l_linger = val;
if (::setsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val, if (::setsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
sizeof(l_val)) != 0) sizeof(l_val)) != 0)
goto error; goto error;
@ -825,6 +849,7 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
JvNewStringUTF ("SO_LINGER not supported")); JvNewStringUTF ("SO_LINGER not supported"));
#endif /* SO_LINGER */ #endif /* SO_LINGER */
return; return;
case _Jv_SO_SNDBUF_ : case _Jv_SO_SNDBUF_ :
case _Jv_SO_RCVBUF_ : case _Jv_SO_RCVBUF_ :
#if defined(SO_SNDBUF) && defined(SO_RCVBUF) #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
@ -837,10 +862,12 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported")); JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
#endif #endif
return; return;
case _Jv_SO_BINDADDR_ : case _Jv_SO_BINDADDR_ :
throw new java::net::SocketException ( throw new java::net::SocketException (
JvNewStringUTF ("SO_BINDADDR: read only option")); JvNewStringUTF ("SO_BINDADDR: read only option"));
return; return;
case _Jv_IP_MULTICAST_IF_ : case _Jv_IP_MULTICAST_IF_ :
throw new java::net::SocketException ( throw new java::net::SocketException (
JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP")); JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
@ -866,9 +893,11 @@ java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
throw new java::net::SocketException ( throw new java::net::SocketException (
JvNewStringUTF ("SO_REUSEADDR: not valid for TCP")); JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
return; return;
case _Jv_SO_TIMEOUT_ : case _Jv_SO_TIMEOUT_ :
timeout = val; timeout = val;
return; return;
default : default :
errno = ENOPROTOOPT; errno = ENOPROTOOPT;
} }
@ -898,8 +927,8 @@ java::net::PlainSocketImpl::getOption (jint optID)
else else
return new java::lang::Boolean (val != 0); return new java::lang::Boolean (val != 0);
#else #else
throw new java::lang::InternalError ( throw new java::lang::InternalError
JvNewStringUTF ("TCP_NODELAY not supported")); (JvNewStringUTF ("TCP_NODELAY not supported"));
#endif #endif
break; break;
@ -908,13 +937,14 @@ java::net::PlainSocketImpl::getOption (jint optID)
if (::getsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val, if (::getsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
&l_val_len) != 0) &l_val_len) != 0)
goto error; goto error;
if (l_val.l_onoff) if (l_val.l_onoff)
return new java::lang::Integer (l_val.l_linger); return new java::lang::Integer (l_val.l_linger);
else else
return new java::lang::Boolean ((jboolean)false); return new java::lang::Boolean ((jboolean)false);
#else #else
throw new java::lang::InternalError ( throw new java::lang::InternalError
JvNewStringUTF ("SO_LINGER not supported")); (JvNewStringUTF ("SO_LINGER not supported"));
#endif #endif
break; break;
@ -947,8 +977,8 @@ java::net::PlainSocketImpl::getOption (jint optID)
else else
return new java::lang::Integer (val); return new java::lang::Integer (val);
#else #else
throw new java::lang::InternalError ( throw new java::lang::InternalError
JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported")); (JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
#endif #endif
break; break;
case _Jv_SO_BINDADDR_: case _Jv_SO_BINDADDR_:
@ -956,8 +986,10 @@ java::net::PlainSocketImpl::getOption (jint optID)
if (localAddress == NULL) if (localAddress == NULL)
{ {
jbyteArray laddr; jbyteArray laddr;
if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0) if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0)
goto error; goto error;
if (u.address.sin_family == AF_INET) if (u.address.sin_family == AF_INET)
{ {
laddr = JvNewByteArray (4); laddr = JvNewByteArray (4);
@ -971,25 +1003,26 @@ java::net::PlainSocketImpl::getOption (jint optID)
} }
#endif #endif
else else
throw new java::net::SocketException ( throw new java::net::SocketException
JvNewStringUTF ("invalid family")); (JvNewStringUTF ("invalid family"));
localAddress = new java::net::InetAddress (laddr, NULL); localAddress = new java::net::InetAddress (laddr, NULL);
} }
return localAddress; return localAddress;
break; break;
case _Jv_IP_MULTICAST_IF_ : case _Jv_IP_MULTICAST_IF_ :
throw new java::net::SocketException ( throw new java::net::SocketException
JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP")); (JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
break; break;
case _Jv_IP_MULTICAST_IF2_ : case _Jv_IP_MULTICAST_IF2_ :
throw new java::net::SocketException ( throw new java::net::SocketException
JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP")); (JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
break; break;
case _Jv_IP_MULTICAST_LOOP_ : case _Jv_IP_MULTICAST_LOOP_ :
throw new java::net::SocketException( throw new java::net::SocketException
JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP")); (JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
break; break;
case _Jv_IP_TOS_ : case _Jv_IP_TOS_ :
@ -1000,12 +1033,14 @@ java::net::PlainSocketImpl::getOption (jint optID)
break; break;
case _Jv_SO_REUSEADDR_ : case _Jv_SO_REUSEADDR_ :
throw new java::net::SocketException ( throw new java::net::SocketException
JvNewStringUTF ("SO_REUSEADDR: not valid for TCP")); (JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
break; break;
case _Jv_SO_TIMEOUT_ : case _Jv_SO_TIMEOUT_ :
return new java::lang::Integer (timeout); return new java::lang::Integer (timeout);
break; break;
default : default :
errno = ENOPROTOOPT; errno = ENOPROTOOPT;
} }

View file

@ -0,0 +1,52 @@
/* ReadOnlyBufferException.java --
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.nio;
/**
* @author Michael Koch
* @since 1.4
*/
public class ReadOnlyBufferException extends UnsupportedOperationException
{
/**
* Creates the exception
*/
public ReadOnlyBufferException ()
{
}
}

View file

@ -39,6 +39,10 @@ package java.nio.channels;
import java.io.IOException; import java.io.IOException;
/**
* @author Michael Koch
* @since 1.4
*/
public class ClosedChannelException extends IOException public class ClosedChannelException extends IOException
{ {
/** /**

View file

@ -0,0 +1,52 @@
/* ClosedSelectorException.java --
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.nio.channels;
/**
* @author Michael Koch
* @since 1.4
*/
public class ClosedSelectorException extends IllegalStateException
{
/**
* Creates the exception
*/
public ClosedSelectorException()
{
}
}