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>
* java/net/HttpURLConnection.java ((getPermission): Take port

View file

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

View file

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

View file

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

View file

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