You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/28 14:05:52 UTC
svn commit: r389483 [2/2] - in /incubator/harmony/enhanced/classlib/trunk:
make/patternsets/ modules/luni/META-INF/ modules/luni/src/main/java/java/net/
modules/luni/src/main/java/org/apache/harmony/luni/net/
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainDatagramSocketImpl.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainDatagramSocketImpl.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainDatagramSocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,14 +13,27 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocketImpl;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.NetworkInterface;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketOptions;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
import java.security.AccessController;
+import org.apache.harmony.luni.platform.INetworkSystem;
+import org.apache.harmony.luni.platform.Platform;
+
import com.ibm.oti.util.Msg;
import com.ibm.oti.util.PriviAction;
@@ -32,6 +45,14 @@
class PlainDatagramSocketImpl extends DatagramSocketImpl {
+ static final int MULTICAST_IF = 1;
+
+ static final int MULTICAST_TTL = 2;
+
+ static final int TCP_NODELAY = 4;
+
+ static final int FLAG_SHUTDOWN = 8;
+
private final static int SO_BROADCAST = 32;
final static int IP_MULTICAST_ADD = 19;
@@ -46,8 +67,16 @@
private int ttl = 1;
+ private INetworkSystem netImpl = Platform.getNetworkSystem();
+
private volatile boolean isNativeConnected = false;
+ public int receiveTimeout = 0;
+
+ public boolean streaming = true;
+
+ public boolean shutdownInput = false;
+
// for datagram and multicast sockets we have to set
// REUSEADDR and REUSEPORT when REUSEADDR is set
// for other types of sockets we need to just set REUSEADDR
@@ -70,13 +99,25 @@
private int trafficClass = 0;
// Fill in the JNI id caches
- private static native void oneTimeInitialization(boolean jcl_IPv6_support);
+ // private static native void oneTimeInitialization(boolean
+ // jcl_IPv6_support);
+ //
+ // static {
+ // oneTimeInitialization(true);
+ // }
+
+
+ public PlainDatagramSocketImpl(FileDescriptor fd, int localPort) {
+ super();
+ this.fd = fd;
+ this.localPort = localPort;
+ }
+
+ public PlainDatagramSocketImpl(){
+ super();
+ }
- static {
- oneTimeInitialization(true);
- }
-
- /**
+ /**
* Bind the datagram socket to the nominated localhost/port. Sockets must be
* bound prior to attempting to send or receive data.
*
@@ -89,17 +130,17 @@
* if an error occured during bind, such as if the port was
* already bound
*/
- protected void bind(int port, InetAddress addr) throws SocketException {
+ public void bind(int port, InetAddress addr) throws SocketException {
String prop = (String) AccessController.doPrivileged(new PriviAction(
"bindToDevice"));
boolean useBindToDevice = prop != null
&& prop.toLowerCase().equals("true");
- bindToDevice = socketBindImpl2(fd, port, useBindToDevice, addr);
+ bindToDevice = netImpl.bind2(fd, port, useBindToDevice, addr);
if (0 != port) {
localPort = port;
} else {
- localPort = Socket.getSocketLocalPortImpl(fd, InetAddress
- .preferIPv6Addresses());
+ localPort = netImpl.getSocketLocalPort(fd,
+ NetUtil.preferIPv6Addresses());
}
try {
@@ -112,12 +153,15 @@
/**
* Close the socket.
*/
- protected void close() {
-
+ public void close() {
synchronized (fd) {
if (fd.valid()) {
- Socket.socketCloseImpl(fd);
- initializeSocket();
+ try {
+ netImpl.socketClose(fd);
+ } catch (IOException e) {
+ // TODO do nothing?
+ }
+ fd = new FileDescriptor();
}
}
}
@@ -125,8 +169,8 @@
/**
* Allocate the socket descriptor in the IP stack.
*/
- protected void create() throws SocketException {
- createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ public void create() throws SocketException {
+ netImpl.createDatagramSocket(fd, NetUtil.preferIPv4Stack());
}
protected void finalize() {
@@ -134,18 +178,6 @@
}
/**
- * Answer the local address from the IP stack. This method should not be
- * called directly as it does not check the security policy.
- *
- * @return InetAddress the local address to which the socket is bound.
- * @see DatagramSocket
- */
- InetAddress getLocalAddress() {
- return Socket.getSocketLocalAddressImpl(fd, InetAddress
- .preferIPv6Addresses());
- }
-
- /**
* Answer the nominated socket option. As the timeouts are not set as
* options in the IP stack, the field value is returned.
*
@@ -159,31 +191,36 @@
} else {
// Call the native first so there will be
// an exception if the socket if closed.
- Object result = Socket.getSocketOptionImpl(fd, optID);
+ Object result = netImpl.getSocketOption(fd, optID);
if (optID == SocketOptions.IP_MULTICAST_IF
- && (Socket.getSocketFlags() & Socket.MULTICAST_IF) != 0) {
-
- return new Inet4Address(ipaddress);
+ && (netImpl.getSocketFlags() & MULTICAST_IF) != 0) {
+ try {
+ return Inet4Address.getByAddress(ipaddress);
+ } catch (UnknownHostException e) {
+ return null;
+ }
}
return result;
}
}
- protected int getTimeToLive() throws IOException {
+ public int getTimeToLive() throws IOException {
// Call the native first so there will be an exception if the socket if
// closed.
int result = (((Byte) getOption(IP_MULTICAST_TTL)).byteValue()) & 0xFF;
- if ((Socket.getSocketFlags() & Socket.MULTICAST_TTL) != 0)
+ if ((netImpl.getSocketFlags() & MULTICAST_TTL) != 0) {
return ttl;
+ }
return result;
}
- protected byte getTTL() throws IOException {
+ public byte getTTL() throws IOException {
// Call the native first so there will be an exception if the socket if
// closed.
byte result = ((Byte) getOption(IP_MULTICAST_TTL)).byteValue();
- if ((Socket.getSocketFlags() & Socket.MULTICAST_TTL) != 0)
+ if ((netImpl.getSocketFlags() & MULTICAST_TTL) != 0) {
return (byte) ttl;
+ }
return result;
}
@@ -197,7 +234,7 @@
* @exception java.io.IOException
* may be thrown while joining a group
*/
- protected void join(InetAddress addr) throws IOException {
+ public void join(InetAddress addr) throws IOException {
setOption(IP_MULTICAST_ADD, new GenericIPMreq(addr));
}
@@ -213,7 +250,7 @@
* @exception java.io.IOException
* may be thrown while joining a group
*/
- protected void joinGroup(SocketAddress addr, NetworkInterface netInterface)
+ public void joinGroup(SocketAddress addr, NetworkInterface netInterface)
throws IOException {
if (addr instanceof InetSocketAddress) {
InetAddress groupAddr = ((InetSocketAddress) addr).getAddress();
@@ -230,7 +267,7 @@
* @exception java.io.IOException
* May be thrown while leaving the group
*/
- protected void leave(InetAddress addr) throws IOException {
+ public void leave(InetAddress addr) throws IOException {
setOption(IP_MULTICAST_DROP, new GenericIPMreq(addr));
}
@@ -244,7 +281,7 @@
* @exception java.io.IOException
* May be thrown while leaving the group
*/
- protected void leaveGroup(SocketAddress addr, NetworkInterface netInterface)
+ public void leaveGroup(SocketAddress addr, NetworkInterface netInterface)
throws IOException {
if (addr instanceof InetSocketAddress) {
InetAddress groupAddr = ((InetSocketAddress) addr).getAddress();
@@ -253,189 +290,190 @@
}
}
- /**
- * Connect the socket to a port and address
- *
- * @param aFD
- * the FileDescriptor to associate with the socket
- * @param port
- * the port to connect to
- * @param trafficClass
- * the traffic Class to be used then the connection is made
- * @param inetAddress
- * address to connect to.
- *
- * @exception SocketException
- * if the connect fails
- */
- protected static native void connectDatagramImpl2(FileDescriptor aFD,
- int port, int trafficClass, InetAddress inetAddress)
- throws SocketException;
-
- /**
- * Disconnect the socket to a port and address
- *
- * @param aFD
- * the FileDescriptor to associate with the socket
- *
- * @exception SocketException
- * if the disconnect fails
- */
- protected static native void disconnectDatagramImpl(FileDescriptor aFD)
- throws SocketException;
-
- /**
- * Allocate a datagram socket in the IP stack. The socket is associated with
- * the <code>aFD</code>.
- *
- * @param aFD
- * the FileDescriptor to associate with the socket
- * @param preferIPv4Stack
- * IP stack preference if underlying platform is V4/V6
- * @exception SocketException
- * upon an allocation error
- */
- protected static native void createDatagramSocketImpl(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
- /**
- * Bind the socket to the port/localhost in the IP stack.
- *
- * @param aFD
- * the socket descriptor
- * @param port
- * the option selector
- * @param bindToDevice
- * bind the socket to the specified interface
- * @param inetAddress
- * address to connect to.
- * @exception SocketException
- * thrown if bind operation fails
- */
- protected static native boolean socketBindImpl2(FileDescriptor aFD,
- int port, boolean bindToDevice, InetAddress inetAddress)
- throws SocketException;
-
- /**
- * Peek on the socket, update <code>sender</code> address and answer the
- * sender port.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param sender
- * an InetAddress, to be updated with the sender's address
- * @param receiveTimeout
- * the maximum length of time the socket should block, reading
- * @return int the sender port
- *
- * @exception IOException
- * upon an read error or timeout
- */
- protected static native int peekDatagramImpl(FileDescriptor aFD,
- InetAddress sender, int receiveTimeout) throws IOException;
-
- /**
- * Recieve data on the socket into the specified buffer. The packet fields
- * <code>data</code> & <code>length</code> are passed in addition to
- * <code>packet</code> to eliminate the JNI field access calls.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param packet
- * the DatagramPacket to receive into
- * @param data
- * the data buffer of the packet
- * @param offset
- * the offset in the data buffer
- * @param length
- * the length of the data buffer in the packet
- * @param receiveTimeout
- * the maximum length of time the socket should block, reading
- * @param peek
- * indicates to peek at the data
- * @exception IOException
- * upon an read error or timeout
- */
- protected static native int receiveDatagramImpl2(FileDescriptor aFD,
- DatagramPacket packet, byte[] data, int offset, int length,
- int receiveTimeout, boolean peek) throws IOException;
-
- /**
- * Recieve data on the connected socket into the specified buffer. The
- * packet fields <code>data</code> & <code>length</code> are passed in
- * addition to <code>packet</code> to eliminate the JNI field access
- * calls.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param packet
- * the DatagramPacket to receive into
- * @param data
- * the data buffer of the packet
- * @param offset
- * the offset in the data buffer
- * @param length
- * the length of the data buffer in the packet
- * @param receiveTimeout
- * the maximum length of time the socket should block, reading
- * @param peek
- * indicates to peek at the data
- * @exception IOException
- * upon an read error or timeout
- */
- protected static native int recvConnectedDatagramImpl(FileDescriptor aFD,
- DatagramPacket packet, byte[] data, int offset, int length,
- int receiveTimeout, boolean peek) throws IOException;
-
- /**
- * Send the <code>data</code> to the nominated target <code>address</code>
- * and <code>port</code>. These values are derived from the
- * DatagramPacket to reduce the field calls within JNI.
- *
- * @param fd
- * the socket FileDescriptor
- * @param data
- * the data buffer of the packet
- * @param offset
- * the offset in the data buffer
- * @param length
- * the length of the data buffer in the packet
- * @param port
- * the target host port
- * @param trafficClass
- * the traffic class to be used when the datagram is sent
- * @param inetAddress
- * address to connect to.
- *
- * @exception IOException
- * upon an read error or timeout
- */
- protected static native int sendDatagramImpl2(FileDescriptor fd,
- byte[] data, int offset, int length, int port,
- boolean bindToDevice, int trafficClass, InetAddress inetAddress)
- throws IOException;
-
- /**
- * Send the <code>data</code> to the address and port to which the was
- * connnected and <code>port</code>.
- *
- * @param fd
- * the socket FileDescriptor
- * @param data
- * the data buffer of the packet
- * @param offset
- * the offset in the data buffer
- * @param length
- * the length of the data buffer in the packet
- * @param bindToDevice
- * not used, current kept in case needed as was the case for
- * sendDatagramImpl
- * @exception IOException
- * upon an read error or timeout
- */
- protected static native int sendConnectedDatagramImpl(FileDescriptor fd,
- byte[] data, int offset, int length, boolean bindToDevice)
- throws IOException;
+ // /**
+ // * Connect the socket to a port and address
+ // *
+ // * @param aFD
+ // * the FileDescriptor to associate with the socket
+ // * @param port
+ // * the port to connect to
+ // * @param trafficClass
+ // * the traffic Class to be used then the connection is made
+ // * @param inetAddress
+ // * address to connect to.
+ // *
+ // * @exception SocketException
+ // * if the connect fails
+ // */
+ // protected static native void connectDatagramImpl2(FileDescriptor aFD,
+ // int port, int trafficClass, InetAddress inetAddress)
+ // throws SocketException;
+ //
+ // /**
+ // * Disconnect the socket to a port and address
+ // *
+ // * @param aFD
+ // * the FileDescriptor to associate with the socket
+ // *
+ // * @exception SocketException
+ // * if the disconnect fails
+ // */
+ // protected static native void disconnectDatagramImpl(FileDescriptor aFD)
+ // throws SocketException;
+ //
+ // /**
+ // * Allocate a datagram socket in the IP stack. The socket is associated
+ // with
+ // * the <code>aFD</code>.
+ // *
+ // * @param aFD
+ // * the FileDescriptor to associate with the socket
+ // * @param preferIPv4Stack
+ // * IP stack preference if underlying platform is V4/V6
+ // * @exception SocketException
+ // * upon an allocation error
+ // */
+ // protected static native void createDatagramSocketImpl(FileDescriptor aFD,
+ // boolean preferIPv4Stack) throws SocketException;
+ //
+ // /**
+ // * Bind the socket to the port/localhost in the IP stack.
+ // *
+ // * @param aFD
+ // * the socket descriptor
+ // * @param port
+ // * the option selector
+ // * @param bindToDevice
+ // * bind the socket to the specified interface
+ // * @param inetAddress
+ // * address to connect to.
+ // * @exception SocketException
+ // * thrown if bind operation fails
+ // */
+ // protected static native boolean socketBindImpl2(FileDescriptor aFD,
+ // int port, boolean bindToDevice, InetAddress inetAddress)
+ // throws SocketException;
+ //
+ // /**
+ // * Peek on the socket, update <code>sender</code> address and answer the
+ // * sender port.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @param sender
+ // * an InetAddress, to be updated with the sender's address
+ // * @param receiveTimeout
+ // * the maximum length of time the socket should block, reading
+ // * @return int the sender port
+ // *
+ // * @exception IOException
+ // * upon an read error or timeout
+ // */
+ // protected static native int peekDatagramImpl(FileDescriptor aFD,
+ // InetAddress sender, int receiveTimeout) throws IOException;
+ //
+ // /**
+ // * Recieve data on the socket into the specified buffer. The packet fields
+ // * <code>data</code> & <code>length</code> are passed in addition to
+ // * <code>packet</code> to eliminate the JNI field access calls.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @param packet
+ // * the DatagramPacket to receive into
+ // * @param data
+ // * the data buffer of the packet
+ // * @param offset
+ // * the offset in the data buffer
+ // * @param length
+ // * the length of the data buffer in the packet
+ // * @param receiveTimeout
+ // * the maximum length of time the socket should block, reading
+ // * @param peek
+ // * indicates to peek at the data
+ // * @exception IOException
+ // * upon an read error or timeout
+ // */
+ // protected static native int receiveDatagramImpl2(FileDescriptor aFD,
+ // DatagramPacket packet, byte[] data, int offset, int length,
+ // int receiveTimeout, boolean peek) throws IOException;
+ //
+ // /**
+ // * Recieve data on the connected socket into the specified buffer. The
+ // * packet fields <code>data</code> & <code>length</code> are passed in
+ // * addition to <code>packet</code> to eliminate the JNI field access
+ // * calls.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @param packet
+ // * the DatagramPacket to receive into
+ // * @param data
+ // * the data buffer of the packet
+ // * @param offset
+ // * the offset in the data buffer
+ // * @param length
+ // * the length of the data buffer in the packet
+ // * @param receiveTimeout
+ // * the maximum length of time the socket should block, reading
+ // * @param peek
+ // * indicates to peek at the data
+ // * @exception IOException
+ // * upon an read error or timeout
+ // */
+ // protected static native int recvConnectedDatagramImpl(FileDescriptor aFD,
+ // DatagramPacket packet, byte[] data, int offset, int length,
+ // int receiveTimeout, boolean peek) throws IOException;
+ //
+ // /**
+ // * Send the <code>data</code> to the nominated target <code>address</code>
+ // * and <code>port</code>. These values are derived from the
+ // * DatagramPacket to reduce the field calls within JNI.
+ // *
+ // * @param fd
+ // * the socket FileDescriptor
+ // * @param data
+ // * the data buffer of the packet
+ // * @param offset
+ // * the offset in the data buffer
+ // * @param length
+ // * the length of the data buffer in the packet
+ // * @param port
+ // * the target host port
+ // * @param trafficClass
+ // * the traffic class to be used when the datagram is sent
+ // * @param inetAddress
+ // * address to connect to.
+ // *
+ // * @exception IOException
+ // * upon an read error or timeout
+ // */
+ // protected static native int sendDatagramImpl2(FileDescriptor fd,
+ // byte[] data, int offset, int length, int port,
+ // boolean bindToDevice, int trafficClass, InetAddress inetAddress)
+ // throws IOException;
+ //
+ // /**
+ // * Send the <code>data</code> to the address and port to which the was
+ // * connnected and <code>port</code>.
+ // *
+ // * @param fd
+ // * the socket FileDescriptor
+ // * @param data
+ // * the data buffer of the packet
+ // * @param offset
+ // * the offset in the data buffer
+ // * @param length
+ // * the length of the data buffer in the packet
+ // * @param bindToDevice
+ // * not used, current kept in case needed as was the case for
+ // * sendDatagramImpl
+ // * @exception IOException
+ // * upon an read error or timeout
+ // */
+ // protected static native int sendConnectedDatagramImpl(FileDescriptor fd,
+ // byte[] data, int offset, int length, boolean bindToDevice)
+ // throws IOException;
protected int peek(InetAddress sender) throws IOException {
if (isNativeConnected) {
@@ -447,12 +485,14 @@
byte[] storageArray = new byte[10];
DatagramPacket pack = new DatagramPacket(storageArray,
storageArray.length);
- recvConnectedDatagramImpl(fd, pack, pack.getData(), pack
+ netImpl.recvConnectedDatagram(fd, pack, pack.getData(), pack
.getOffset(), pack.getLength(), receiveTimeout, true); // peek
- sender.ipaddress = connectedAddress.getAddress();
+ // to set the sender ,we now use a native function
+ // sender.ipaddress = connectedAddress.getAddress();
+ netImpl.setInetAddress(sender, connectedAddress.getAddress());
return connectedPort;
}
- return peekDatagramImpl(fd, sender, receiveTimeout);
+ return netImpl.peekDatagram(fd, sender, receiveTimeout);
}
/**
@@ -463,18 +503,18 @@
* @exception IOException
* if an error or timeout occurs during a read
*/
- protected void receive(DatagramPacket pack) throws java.io.IOException {
+ public void receive(DatagramPacket pack) throws java.io.IOException {
try {
if (isNativeConnected) {
// do not peek
- recvConnectedDatagramImpl(fd, pack, pack.getData(), pack
- .getOffset(), pack.getLength(), receiveTimeout, false);
+ netImpl.recvConnectedDatagram(fd, pack, pack.getData(),
+ pack.getOffset(), pack.getLength(), receiveTimeout,
+ false);
updatePacketRecvAddress(pack);
} else {
- receiveDatagramImpl2(fd, pack, pack.getData(),
- pack.getOffset(), pack.getLength(), receiveTimeout,
- false // do not peek
- );
+ // receiveDatagramImpl2
+ netImpl.receiveDatagram(fd, pack, pack.getData(), pack
+ .getOffset(), pack.getLength(), receiveTimeout, false);
}
} catch (InterruptedIOException e) {
throw new SocketTimeoutException(e.getMessage());
@@ -487,15 +527,16 @@
* @exception IOException
* if an error occurs during the write
*/
- protected void send(DatagramPacket packet) throws IOException {
+ public void send(DatagramPacket packet) throws IOException {
if (isNativeConnected) {
- sendConnectedDatagramImpl(fd, packet.getData(), packet.getOffset(),
- packet.getLength(), bindToDevice);
+ netImpl.sendConnectedDatagram(fd, packet.getData(), packet
+ .getOffset(), packet.getLength(), bindToDevice);
} else {
- sendDatagramImpl2(fd, packet.getData(), packet.getOffset(), packet
- .getLength(), packet.getPort(), bindToDevice, trafficClass,
- packet.getAddress());
+ // sendDatagramImpl2
+ netImpl.sendDatagram(fd, packet.getData(), packet.getOffset(),
+ packet.getLength(), packet.getPort(), bindToDevice,
+ trafficClass, packet.getAddress());
}
}
@@ -518,9 +559,9 @@
if (optID == SocketOptions.SO_TIMEOUT) {
receiveTimeout = ((Integer) val).intValue();
} else {
- int flags = Socket.getSocketFlags();
+ int flags = netImpl.getSocketFlags();
try {
- Socket.setSocketOptionImpl(fd, optID | (flags << 16), val);
+ netImpl.setSocketOption(fd, optID | (flags << 16), val);
} catch (SocketException e) {
// we don't throw an exception for IP_TOS even if the platform
// won't let us set the requested value
@@ -529,10 +570,10 @@
}
}
if (optID == SocketOptions.IP_MULTICAST_IF
- && (flags & Socket.MULTICAST_IF) != 0) {
+ && (flags & MULTICAST_IF) != 0) {
InetAddress inet = (InetAddress) val;
- if (InetAddress.bytesToInt(inet.ipaddress, 0) == 0
- || inet.equals(InetAddress.LOOPBACK)) {
+ if (bytesToInt(inet.getAddress(), 0) == 0
+ || inet.isLoopbackAddress()) {
ipaddress = ((InetAddress) val).getAddress();
} else {
InetAddress local = null;
@@ -563,16 +604,16 @@
}
}
- protected void setTimeToLive(int ttl) throws java.io.IOException {
+ public void setTimeToLive(int ttl) throws java.io.IOException {
setOption(IP_MULTICAST_TTL, new Byte((byte) (ttl & 0xFF)));
- if ((Socket.getSocketFlags() & Socket.MULTICAST_TTL) != 0) {
+ if ((netImpl.getSocketFlags() & MULTICAST_TTL) != 0) {
this.ttl = ttl;
}
}
- protected void setTTL(byte ttl) throws java.io.IOException {
+ public void setTTL(byte ttl) throws java.io.IOException {
setOption(IP_MULTICAST_TTL, new Byte(ttl));
- if ((Socket.getSocketFlags() & Socket.MULTICAST_TTL) != 0) {
+ if ((netImpl.getSocketFlags() & MULTICAST_TTL) != 0) {
this.ttl = ttl;
}
}
@@ -589,10 +630,10 @@
* possibly thrown, if the datagram socket cannot be
* connected to the specified remote address and port
*/
- protected void connect(InetAddress inetAddr, int port)
- throws SocketException {
+ public void connect(InetAddress inetAddr, int port) throws SocketException {
- connectDatagramImpl2(fd, port, trafficClass, inetAddr);
+ // connectDatagram impl2
+ netImpl.connectDatagram(fd, port, trafficClass, inetAddr);
// if we get here then we are connected at the native level
try {
@@ -610,9 +651,9 @@
/**
* Disconnect the socket from the remote address and port.
*/
- protected void disconnect() {
+ public void disconnect() {
try {
- disconnectDatagramImpl(fd);
+ netImpl.disconnectDatagram(fd);
} catch (Exception e) {
// there is currently no way to return an error so just eat any
// exception
@@ -637,16 +678,17 @@
* @exception IOException
* if an error occurs
*/
- protected int peekData(DatagramPacket pack) throws IOException {
+ public int peekData(DatagramPacket pack) throws IOException {
try {
if (isNativeConnected) {
- recvConnectedDatagramImpl(fd, pack, pack.getData(), pack
- .getOffset(), pack.getLength(), receiveTimeout, true); // peek
- updatePacketRecvAddress(pack);
- } else {
- receiveDatagramImpl2(fd, pack, pack.getData(),
+ netImpl.recvConnectedDatagram(fd, pack, pack.getData(),
pack.getOffset(), pack.getLength(), receiveTimeout,
true); // peek
+ updatePacketRecvAddress(pack);
+ } else {
+ // receiveDatagram 2
+ netImpl.receiveDatagram(fd, pack, pack.getData(), pack
+ .getOffset(), pack.getLength(), receiveTimeout, true); // peek
}
} catch (InterruptedIOException e) {
throw new SocketTimeoutException(e.getMessage());
@@ -665,6 +707,29 @@
*/
private void updatePacketRecvAddress(DatagramPacket packet) {
packet.setAddress(connectedAddress);
- packet.port = connectedPort;
+ packet.setPort(connectedPort);
+ }
+
+ static void intToBytes(int value, byte bytes[], int start) {
+ // Shift the int so the current byte is right-most
+ // Use a byte mask of 255 to single out the last byte.
+ bytes[start] = (byte) ((value >> 24) & 255);
+ bytes[start + 1] = (byte) ((value >> 16) & 255);
+ bytes[start + 2] = (byte) ((value >> 8) & 255);
+ bytes[start + 3] = (byte) (value & 255);
+ }
+
+ static int bytesToInt(byte bytes[], int start) {
+ // First mask the byte with 255, as when a negative
+ // signed byte converts to an integer, it has bits
+ // on in the first 3 bytes, we are only concerned
+ // about the right-most 8 bits.
+ // Then shift the rightmost byte to align with its
+ // position in the integer.
+ int value = ((bytes[start + 3] & 255))
+ | ((bytes[start + 2] & 255) << 8)
+ | ((bytes[start + 1] & 255) << 16)
+ | ((bytes[start] & 255) << 24);
+ return value;
}
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainMulticastSocketImpl.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainMulticastSocketImpl.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainMulticastSocketImpl.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainMulticastSocketImpl.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainMulticastSocketImpl.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainMulticastSocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainMulticastSocketImpl.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,10 +13,13 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.FileDescriptor;
+import java.net.SocketException;
+
+import org.apache.harmony.luni.platform.Platform;
+
/**
* This class was added so we can create sockets with options that are needed
@@ -27,24 +30,25 @@
* used, for earlier versions the original PlainSocketImpl is used.
*/
class PlainMulticastSocketImpl extends PlainDatagramSocketImpl {
+
+ // /**
+ // * Answer the result of attempting to create a multicast socket in the IP
+ // * stack. Any special options required for server sockets will be set by
+ // * this method.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @exception SocketException
+ // * if an error occurs while creating the socket
+ // */
+ // static native void createMulticastSocketImpl(FileDescriptor aFD,
+ // boolean preferIPv4Stack) throws SocketException;
- /**
- * Answer the result of attempting to create a multicast socket in the IP
- * stack. Any special options required for server sockets will be set by
- * this method.
- *
- * @param aFD
- * the socket FileDescriptor
- * @exception SocketException
- * if an error occurs while creating the socket
- */
- static native void createMulticastSocketImpl(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
- /**
+ /**
* Allocate the socket descriptor in the IP stack.
*/
- protected void create() throws SocketException {
- createMulticastSocketImpl(fd, Socket.preferIPv4Stack());
+ public void create() throws SocketException {
+ Platform.getNetworkSystem()
+ .createMulticastSocket(fd, NetUtil.preferIPv4Stack());
}
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainServerSocketImpl.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainServerSocketImpl.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainServerSocketImpl.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainServerSocketImpl.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainServerSocketImpl.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainServerSocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainServerSocketImpl.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,10 +13,14 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.FileDescriptor;
+import java.net.SocketException;
+
+import org.apache.harmony.luni.net.NetUtil;
+import org.apache.harmony.luni.net.PlainSocketImpl;
+
/**
* This class was added so we can create sockets with options that are needed
@@ -27,6 +31,15 @@
* used, for earlier versions the original PlainSocketImpl is used.
*/
class PlainServerSocketImpl extends PlainSocketImpl {
+
+ public PlainServerSocketImpl(){
+ super();
+ }
+
+ public PlainServerSocketImpl(FileDescriptor fd){
+ super();
+ this.fd = fd;
+ }
/**
* Answer the result of attempting to create a server stream socket in the
@@ -38,9 +51,8 @@
* @exception SocketException
* if an error occurs while creating the socket
*/
- static native void createServerStreamSocketImpl(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
+ // static native void createServerStreamSocketImpl(FileDescriptor aFD,
+ // boolean preferIPv4Stack) throws SocketException;
/**
* Creates a new unconnected socket. If streaming is true, create a stream
* socket, else a datagram socket. The deprecated datagram usage is not
@@ -52,11 +64,16 @@
* if an error occurs while creating the socket
*/
protected void create(boolean isStreaming) throws SocketException {
- this.streaming = isStreaming;
+ streaming = isStreaming;
+ // if (isStreaming) {
+ // createServerStreamSocketImpl(fd, Socket.preferIPv4Stack());
+ // } else {
+ // createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ // }
if (isStreaming) {
- createServerStreamSocketImpl(fd, Socket.preferIPv4Stack());
+ netImpl.createServerStreamSocket(fd, NetUtil.preferIPv4Stack());
} else {
- createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ netImpl.createDatagramSocket(fd, NetUtil.preferIPv4Stack());
}
}
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,15 +13,32 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
+import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketImpl;
+import java.net.SocketOptions;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
import java.security.AccessController;
+import org.apache.harmony.luni.net.NetUtil;
+import org.apache.harmony.luni.net.PlainSocketImpl;
+import org.apache.harmony.luni.net.SocketInputStream;
+import org.apache.harmony.luni.net.SocketOutputStream;
+import org.apache.harmony.luni.net.Socks4Message;
+import org.apache.harmony.luni.platform.INetworkSystem;
+import org.apache.harmony.luni.platform.Platform;
+
import com.ibm.oti.util.Msg;
import com.ibm.oti.util.PriviAction;
@@ -30,6 +47,16 @@
*/
class PlainSocketImpl extends SocketImpl {
+ // Const copy from socket
+
+ static final int MULTICAST_IF = 1;
+
+ static final int MULTICAST_TTL = 2;
+
+ static final int TCP_NODELAY = 4;
+
+ static final int FLAG_SHUTDOWN = 8;
+
// For SOCKS support. A SOCKS bind() uses the last
// host connected to in its request.
static private InetAddress lastConnectedAddress;
@@ -45,6 +72,14 @@
// that specify an address packets are going to be sent to
private int trafficClass = 0;
+ protected INetworkSystem netImpl = Platform.getNetworkSystem();
+
+ public int receiveTimeout = 0;
+
+ public boolean streaming = true;
+
+ public boolean shutdownInput = false;
+
/**
* Accepts a connection on the provided socket, by calling the IP stack.
*
@@ -54,18 +89,19 @@
* if an error occurs while accepting
*/
protected void accept(SocketImpl newImpl) throws IOException {
- if (usingSocks()) {
+ if (NetUtil.usingSocks()) {
((PlainSocketImpl) newImpl).socksBind();
((PlainSocketImpl) newImpl).socksAccept();
return;
}
try {
- acceptStreamSocketImpl(fd, newImpl, newImpl.fd, receiveTimeout);
+ netImpl.acceptStreamSocket(fd, newImpl, ((PlainSocketImpl) newImpl)
+ .getFD(), receiveTimeout);
} catch (InterruptedIOException e) {
throw new SocketTimeoutException(e.getMessage());
}
- newImpl.localport = getLocalPort();
+ ((PlainSocketImpl) newImpl).setLocalport(getLocalPort());
}
/**
@@ -83,7 +119,7 @@
if (shutdownInput == true) {
return 0;
}
- return availableStreamImpl(fd);
+ return netImpl.availableStream(fd);
}
/**
@@ -100,18 +136,18 @@
* if an error occurs while binding
*/
protected void bind(InetAddress anAddr, int aPort) throws IOException {
- if (usingSocks()) {
+ if (NetUtil.usingSocks()) {
socksBind();
return;
}
-
- PlainSocketImpl2.socketBindImpl2(fd, aPort, anAddr);
+ netImpl.bind(fd, aPort, anAddr);
+ // PlainSocketImpl2.socketBindImpl2(fd, aPort, anAddr);
address = anAddr;
if (0 != aPort) {
localport = aPort;
} else {
- localport = Socket.getSocketLocalPortImpl(fd, InetAddress
- .preferIPv6Addresses());
+ localport = netImpl.getSocketLocalPort(fd,
+ NetUtil.preferIPv6Addresses());
}
}
@@ -124,14 +160,14 @@
protected void close() throws IOException {
synchronized (fd) {
if (fd.valid()) {
- if ((Socket.getSocketFlags() & Socket.FLAG_SHUTDOWN) != 0) {
+ if ((netImpl.getSocketFlags() & FLAG_SHUTDOWN) != 0) {
try {
shutdownOutput();
} catch (Exception e) {
}
}
- Socket.socketCloseImpl(fd);
- initializeSocket();
+ netImpl.socketClose(fd);
+ fd = new FileDescriptor();
}
}
}
@@ -148,8 +184,10 @@
* if an error occurs while connecting
*/
protected void connect(String aHost, int aPort) throws IOException {
- InetAddress anAddr = InetAddress.getHostByNameImpl(aHost, InetAddress
- .preferIPv6Addresses());
+ // InetAddress anAddr = InetAddress.getHostByNameImpl(aHost,
+ // preferIPv6Addresses());
+ InetAddress anAddr = netImpl.getHostByName(aHost,
+ NetUtil.preferIPv6Addresses());
connect(anAddr, aPort);
}
@@ -181,20 +219,23 @@
*/
private void connect(InetAddress anAddr, int aPort, int timeout)
throws IOException {
- InetAddress address = anAddr.equals(InetAddress.ANY) ? InetAddress.LOOPBACK
- : anAddr;
+ InetAddress address = anAddr.isAnyLocalAddress() ? InetAddress
+ .getByName("localhost") : anAddr;
try {
if (streaming) {
- if (usingSocks())
+ if (NetUtil.usingSocks()) {
socksConnect(anAddr, aPort, 0);
- else {
+ } else {
if (timeout == 0) {
- PlainSocketImpl2.connectStreamSocketImpl2(fd, aPort,
- trafficClass, address);
+ // PlainSocketImpl2.connectStreamSocketImpl2(fd, aPort,
+ // trafficClass, address);
+ netImpl.connect(fd, trafficClass, anAddr, aPort);
} else {
- PlainSocketImpl2.connectStreamWithTimeoutSocketImpl2(
- fd, aPort, timeout, trafficClass, address);
+ // PlainSocketImpl2.connectStreamWithTimeoutSocketImpl2(
+ // fd, aPort, timeout, trafficClass, address);
+ netImpl.connectStreamWithTimeoutSocket(fd, aPort,
+ timeout, trafficClass, anAddr);
}
}
}
@@ -202,8 +243,8 @@
throw new ConnectException(anAddr + ":" + aPort + " - "
+ e.getMessage());
}
- this.address = anAddr;
- this.port = aPort;
+ super.address = anAddr;
+ super.port = aPort;
}
/**
@@ -216,12 +257,18 @@
* @exception SocketException
* if an error occurs while creating the socket
*/
- protected void create(boolean streaming) throws SocketException {
+ protected void create(boolean streaming) throws IOException {
this.streaming = streaming;
+ // if (streaming) {
+ // createStreamSocketImpl(fd, Socket.preferIPv4Stack());
+ // } else {
+ // createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ // }
+
if (streaming) {
- createStreamSocketImpl(fd, Socket.preferIPv4Stack());
+ netImpl.createStreamSocket(fd, NetUtil.preferIPv4Stack());
} else {
- createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ netImpl.createDatagramSocket(fd, NetUtil.preferIPv4Stack());
}
}
@@ -237,8 +284,9 @@
* thrown if an error occurs while accessing the stream
*/
protected synchronized InputStream getInputStream() throws IOException {
- if (!fd.valid())
+ if (!fd.valid()) {
throw new SocketException(Msg.getString("K003d"));
+ }
return new SocketInputStream(this);
}
@@ -261,9 +309,9 @@
} else {
// Call the native first so there will be
// an exception if the socket if closed.
- Object result = Socket.getSocketOptionImpl(fd, optID);
+ Object result = netImpl.getSocketOption(fd, optID);
if (optID == SocketOptions.TCP_NODELAY
- && (Socket.getSocketFlags() & Socket.TCP_NODELAY) != 0) {
+ && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
return new Boolean(tcpNoDelay);
}
return result;
@@ -278,9 +326,9 @@
* thrown if an error occurs while accessing the stream
*/
protected synchronized OutputStream getOutputStream() throws IOException {
- if (!fd.valid())
+ if (!fd.valid()) {
throw new SocketException(Msg.getString("K003d"));
-
+ }
return new SocketOutputStream(this);
}
@@ -295,12 +343,13 @@
* thrown if an error occurs while listening
*/
protected void listen(int backlog) throws IOException {
- if (usingSocks()) {
+ if (NetUtil.usingSocks()) {
// Do nothing for a SOCKS connection. The listen occurs on the
// server during the bind.
return;
}
- listenStreamSocketImpl(fd, backlog);
+ // listenStreamSocketImpl(fd, backlog);
+ netImpl.listenStreamSocket(fd, backlog);
}
/**
@@ -319,10 +368,11 @@
receiveTimeout = ((Integer) val).intValue();
} else {
try {
- Socket.setSocketOptionImpl(fd, optID, val);
+ netImpl.setSocketOption(fd, optID, val);
if (optID == SocketOptions.TCP_NODELAY
- && (Socket.getSocketFlags() & Socket.TCP_NODELAY) != 0)
+ && (netImpl.getSocketFlags() & TCP_NODELAY) != 0) {
tcpNoDelay = ((Boolean) val).booleanValue();
+ }
} catch (SocketException e) {
// we don't through an exception for IP_TOS even if the platform
@@ -346,20 +396,6 @@
}
/**
- * Answer whether to use a SOCKS proxy.
- *
- * @return boolean
- */
- static boolean usingSocks() {
- String proxySet = (String) AccessController
- .doPrivileged(new PriviAction("socksProxySet"));
- if (proxySet != null)
- return proxySet.toLowerCase().equals("true");
-
- return AccessController.doPrivileged(new PriviAction("socksProxyHost")) != null;
- }
-
- /**
* Get the SOCKS proxy server port.
*/
private int socksGetServerPort() {
@@ -371,8 +407,9 @@
if (proxyPort != null) {
portValue = Integer.parseInt(proxyPort);
}
- if (portValue < 0)
+ if (portValue < 0) {
portValue = 1080;
+ }
return portValue;
}
@@ -384,8 +421,8 @@
String proxyName = (String) AccessController
.doPrivileged(new PriviAction("socksProxyHost"));
- InetAddress anAddr = InetAddress.getHostByNameImpl(proxyName,
- InetAddress.preferIPv6Addresses());
+ InetAddress anAddr = netImpl.getHostByName(proxyName,
+ NetUtil.preferIPv6Addresses());
return anAddr;
}
@@ -396,11 +433,16 @@
int applicationServerPort, int timeout) throws IOException {
try {
if (timeout == 0) {
- PlainSocketImpl2.connectStreamSocketImpl2(fd,
- socksGetServerPort(), trafficClass,
- socksGetServerAddress());
+ // PlainSocketImpl2.connectStreamSocketImpl2(fd,
+ // socksGetServerPort(), trafficClass,
+ // socksGetServerAddress());
+ netImpl.connect(fd, trafficClass, socksGetServerAddress(),
+ socksGetServerPort());
} else {
- PlainSocketImpl2.connectStreamWithTimeoutSocketImpl2(fd,
+ // PlainSocketImpl2.connectStreamWithTimeoutSocketImpl2(fd,
+ // socksGetServerPort(), timeout, trafficClass,
+ // socksGetServerAddress());
+ netImpl.connectStreamWithTimeoutSocket(fd,
socksGetServerPort(), timeout, trafficClass,
socksGetServerAddress());
}
@@ -433,7 +475,7 @@
/**
* Perform an accept for a SOCKS bind.
*/
- void socksAccept() throws IOException {
+ public void socksAccept() throws IOException {
Socks4Message reply = socksReadReply();
if (reply.getCommandOrResult() != Socks4Message.RETURN_SUCCESS) {
throw new IOException(reply.getErrorString(reply
@@ -442,20 +484,41 @@
}
/**
+ * Shutdown the input portion of the socket.
+ */
+ protected void shutdownInput() throws IOException {
+ shutdownInput = true;
+ // shutdownInputImpl(fd);
+ netImpl.shutdownInput(fd);
+ }
+
+ /**
+ * Shutdown the output portion of the socket.
+ */
+ protected void shutdownOutput() throws IOException {
+ // shutdownOutputImpl(fd);
+ netImpl.shutdownOutput(fd);
+ }
+
+ /**
* Bind using a SOCKS server.
*/
private void socksBind() throws IOException {
try {
- PlainSocketImpl2.connectStreamSocketImpl2(fd, socksGetServerPort(),
- trafficClass, socksGetServerAddress());
+ // PlainSocketImpl2.connectStreamSocketImpl2(fd,
+ // socksGetServerPort(),
+ // trafficClass, socksGetServerAddress());
+ netImpl.connect(fd, trafficClass, socksGetServerAddress(),
+ socksGetServerPort());
} catch (Exception e) {
throw new IOException(Msg.getString("K003f", e));
}
// There must be a connection to an application host for the bind to
// work.
- if (lastConnectedAddress == null)
+ if (lastConnectedAddress == null) {
throw new SocketException(Msg.getString("K0040"));
+ }
// Use the last connected address and port in the bind request.
socksSendRequest(Socks4Message.COMMAND_BIND, lastConnectedAddress,
@@ -476,8 +539,8 @@
// currently the Socks4Message.getIP() only returns int,
// so only works with IPv4 4byte addresses
byte[] replyBytes = new byte[4];
- InetAddress.intToBytes(reply.getIP(), replyBytes, 0);
- address = new InetAddress(replyBytes);
+ intToBytes(reply.getIP(), replyBytes, 0);
+ address = InetAddress.getByAddress(replyBytes);
}
localport = reply.getPort();
}
@@ -533,7 +596,8 @@
* Answer if the socket supports urgent data.
*/
protected boolean supportsUrgentData() {
- return !streaming || SocketImpl.supportsUrgentDataImpl(fd);
+ // return !streaming || SocketImpl.supportsUrgentDataImpl(fd);
+ return !streaming || netImpl.supportsUrgentData(fd);
}
/**
@@ -546,6 +610,54 @@
* when an error occurs sending urgent data
*/
protected void sendUrgentData(int value) throws IOException {
- SocketImpl.sendUrgentDataImpl(fd, (byte) value);
+ // SocketImpl.sendUrgentDataImpl(fd, (byte) value);
+ netImpl.sendUrgentData(fd, (byte) value);
+ }
+
+ FileDescriptor getFD() {
+ return fd;
+ }
+
+ private void setLocalport(int localport) {
+ this.localport = localport;
+ }
+
+ int read(byte[] buffer, int offset, int count) throws IOException {
+ if (shutdownInput) {
+ return -1;
+ }
+ try {
+ // int read = receiveStreamImpl(fd, buffer, offset, count,
+ // receiveTimeout);
+ int read = netImpl.receiveStream(fd, buffer, offset, count,
+ receiveTimeout);
+ if (read == -1) {
+ shutdownInput = true;
+ }
+ return read;
+ } catch (InterruptedIOException e) {
+ throw new SocketTimeoutException(e.getMessage());
+ }
+ }
+
+ int write(byte[] buffer, int offset, int count) throws IOException {
+ if (!streaming) {
+ // PlainSocketImpl2.sendDatagramImpl2(fd, buffer, offset, count,
+ // port,
+ // address);
+ netImpl
+ .sendDatagram2(fd, buffer, offset, count, port, address);
+ }
+ // return sendStreamImpl(fd, buffer, offset, count);
+ return netImpl.sendStream(fd, buffer, offset, count);
+ }
+
+ static void intToBytes(int value, byte bytes[], int start) {
+ // Shift the int so the current byte is right-most
+ // Use a byte mask of 255 to single out the last byte.
+ bytes[start] = (byte) ((value >> 24) & 255);
+ bytes[start + 1] = (byte) ((value >> 16) & 255);
+ bytes[start + 2] = (byte) ((value >> 8) & 255);
+ bytes[start + 3] = (byte) (value & 255);
}
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl2.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl2.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl2.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,15 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.FileDescriptor;
import java.io.IOException;
+import java.net.InetAddress;
+import java.net.SocketException;
+
+import org.apache.harmony.luni.net.NetUtil;
+
/**
* This class was added so we can create sockets without options that were
@@ -29,7 +33,19 @@
*/
class PlainSocketImpl2 extends PlainSocketImpl {
- /**
+ public PlainSocketImpl2(FileDescriptor fd, int localport, InetAddress addr, int port) {
+ super();
+ super.fd = fd;
+ super.localport = localport;
+ super.address = addr;
+ super.port = port;
+ }
+
+ public PlainSocketImpl2(){
+ super();
+ }
+
+ /**
* Answer the result of attempting to create a stream socket in the IP
* stack. This version does not set certain options which were required for
* server sockets and which the initial vesrion ended up setting for both
@@ -42,48 +58,49 @@
* @exception SocketException
* if an error occurs while creating the socket
*/
- static native void createStreamSocketImpl2(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
- /**
- * Connect the underlying socket to the nominated remotehost/port.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param aport
- * the remote machine port to connect to
- * @param trafficClass
- * the traffic class to be used when connecting
- * @param inetAddress
- * the address to connect to
- * @exception SocketException
- * if an error occurs while connecting
- */
- static native void connectStreamSocketImpl2(FileDescriptor aFD, int aport,
- int trafficClass, InetAddress inetAddress) throws IOException;
-
- /**
- * Connect the underlying socket to the nominated remotehost/port.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param aport
- * the remote machine port to connect to
- * @param timeout
- * timeout after which SocketTimeoutException will be thrown
- * @param trafficClass
- * the traffic class to be used when connecting
- * @param inetAddress
- * the address to connect to
- * @exception SocketException
- * if an error occurs while connecting
- * @exception SocketTimeoutException
- * if a timeout occurs while trying to connect
- */
- static native void connectStreamWithTimeoutSocketImpl2(FileDescriptor aFD,
- int aport, int timeout, int trafficClass, InetAddress inetAddress)
- throws IOException;
-
+ // static native void createStreamSocketImpl2(FileDescriptor aFD,
+ // boolean preferIPv4Stack) throws SocketException;
+ //
+ // /**
+ // * Connect the underlying socket to the nominated remotehost/port.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @param aport
+ // * the remote machine port to connect to
+ // * @param trafficClass
+ // * the traffic class to be used when connecting
+ // * @param inetAddress
+ // * the address to connect to
+ // * @exception SocketException
+ // * if an error occurs while connecting
+ // */
+ // static native void connectStreamSocketImpl2(FileDescriptor aFD, int
+ // aport,
+ // int trafficClass, InetAddress inetAddress) throws IOException;
+ //
+ // /**
+ // * Connect the underlying socket to the nominated remotehost/port.
+ // *
+ // * @param aFD
+ // * the socket FileDescriptor
+ // * @param aport
+ // * the remote machine port to connect to
+ // * @param timeout
+ // * timeout after which SocketTimeoutException will be thrown
+ // * @param trafficClass
+ // * the traffic class to be used when connecting
+ // * @param inetAddress
+ // * the address to connect to
+ // * @exception SocketException
+ // * if an error occurs while connecting
+ // * @exception SocketTimeoutException
+ // * if a timeout occurs while trying to connect
+ // */
+ // static native void connectStreamWithTimeoutSocketImpl2(FileDescriptor
+ // aFD,
+ // int aport, int timeout, int trafficClass, InetAddress inetAddress)
+ // throws IOException;
/**
* Creates a new unconnected socket. If streaming is true, create a stream
* socket, else a datagram socket. The deprecated datagram usage is not
@@ -94,12 +111,12 @@
* @exception SocketException
* if an error occurs while creating the socket
*/
- protected void create(boolean isStreaming) throws SocketException {
- this.streaming = isStreaming;
+ protected void create(boolean isStreaming) throws IOException {
+ streaming = isStreaming;
if (isStreaming) {
- createStreamSocketImpl2(fd, Socket.preferIPv4Stack());
+ netImpl.createSocket(fd, NetUtil.preferIPv4Stack());
} else {
- createDatagramSocketImpl(fd, Socket.preferIPv4Stack());
+ netImpl.createDatagramSocket(fd, NetUtil.preferIPv4Stack());
}
}
@@ -122,23 +139,23 @@
* @exception IOException
* upon an read error or timeout
*/
- static native int sendDatagramImpl2(FileDescriptor fd, byte[] data,
- int offset, int length, int port, InetAddress inetAddress)
- throws IOException;
-
- /**
- * Bind the socket to the port/localhost in the IP stack.
- *
- * @param aFD
- * the socket descriptor
- * @param port
- * the option selector
- * @param inetAddress
- * the address to be used
- *
- * @throws SocketException
- * if bind operation fails
- */
- static native void socketBindImpl2(FileDescriptor aFD, int port,
- InetAddress inetAddress) throws SocketException;
+ // static native int sendDatagramImpl2(FileDescriptor fd, byte[] data,
+ // int offset, int length, int port, InetAddress inetAddress)
+ // throws IOException;
+ //
+ // /**
+ // * Bind the socket to the port/localhost in the IP stack.
+ // *
+ // * @param aFD
+ // * the socket descriptor
+ // * @param port
+ // * the option selector
+ // * @param inetAddress
+ // * the address to be used
+ // *
+ // * @throws SocketException
+ // * if bind operation fails
+ // */
+ // static native void socketBindImpl2(FileDescriptor aFD, int port,
+ // InetAddress inetAddress) throws SocketException;
}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java?rev=389483&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java Tue Mar 28 04:05:47 2006
@@ -0,0 +1,53 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.luni.net;
+
+import java.io.FileDescriptor;
+import java.net.DatagramSocketImpl;
+import java.net.InetAddress;
+import java.net.SocketImpl;
+
+
+public class SocketImplProvider {
+
+ public static SocketImpl getSocketImpl(){
+ return new PlainSocketImpl2();
+ }
+
+ public static SocketImpl getSocketImpl(FileDescriptor fd, int localport, InetAddress addr, int port){
+ return new PlainSocketImpl2(fd, localport, addr, port);
+ }
+
+ public static SocketImpl getServerSocketImpl(){
+ return new PlainServerSocketImpl();
+ }
+
+ public static SocketImpl getServerSocketImpl(FileDescriptor fd){
+ return new PlainServerSocketImpl(fd);
+ }
+
+ public static DatagramSocketImpl getDatagramSocketImpl(){
+ return new PlainDatagramSocketImpl();
+ }
+
+ public static DatagramSocketImpl getMulticastSocketImpl(){
+ return new PlainMulticastSocketImpl();
+ }
+
+ public static DatagramSocketImpl getDatagramSocketImpl(FileDescriptor fd, int localPort) {
+ return new PlainDatagramSocketImpl(fd, localPort);
+ }
+
+}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketInputStream.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketInputStream.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketInputStream.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketInputStream.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketInputStream.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketInputStream.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,13 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.IOException;
import java.io.InputStream;
+import java.net.Socket;
+import java.net.SocketImpl;
+
import com.ibm.oti.util.Msg;
@@ -28,7 +30,13 @@
*/
class SocketInputStream extends InputStream {
- SocketImpl socket;
+ private static final String ERRCODE_OFFSETCOUNT_OUTOFBOUND = "K002f"; //$NON-NLS-1$
+
+ private static final String ERRCODE_OFFSET_OUTOFBOUND = "K002e"; //$NON-NLS-1$
+
+ private static final String ERRCODE_BUFFER_NULL = "K0047"; //$NON-NLS-1$
+
+ PlainSocketImpl socket;
/**
* Constructs a SocketInputStream for the <code>socket</code>. Read
@@ -40,7 +48,7 @@
*/
public SocketInputStream(SocketImpl socket) {
super();
- this.socket = socket;
+ this.socket = (PlainSocketImpl) socket;
}
/**
@@ -119,16 +127,22 @@
* bounds are incorrect or an error occurs during the read
*/
public int read(byte[] buffer, int offset, int count) throws IOException {
- if (null == buffer)
- throw new IOException(Msg.getString("K0047"));
-
- if (0 == count)
+ if (null == buffer) {
+ throw new IOException(Msg.getString(ERRCODE_BUFFER_NULL));
+ }
+
+ if (0 == count) {
return 0;
+ }
- if (0 > offset || offset >= buffer.length)
- throw new ArrayIndexOutOfBoundsException(Msg.getString("K002e"));
- if (0 > count || offset + count > buffer.length)
- throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f"));
+ if (0 > offset || offset >= buffer.length) {
+ throw new ArrayIndexOutOfBoundsException(Msg
+ .getString(ERRCODE_OFFSET_OUTOFBOUND));
+ }
+ if (0 > count || offset + count > buffer.length) {
+ throw new ArrayIndexOutOfBoundsException(Msg
+ .getString(ERRCODE_OFFSETCOUNT_OUTOFBOUND));
+ }
return socket.read(buffer, offset, count);
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketOutputStream.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketOutputStream.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketOutputStream.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketOutputStream.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketOutputStream.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketOutputStream.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,20 +13,21 @@
* limitations under the License.
*/
-package java.net;
+package org.apache.harmony.luni.net;
-
-/**
- * The SocketOutputStream supports the streamed writing of bytes to the socket.
- * Multiple streams may be opened on a socket, so care should be taken to manage
- * opened streams and coordinate write operations between threads.
- */
import java.io.IOException;
import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketImpl;
+
class SocketOutputStream extends OutputStream {
- SocketImpl socket;
+ private static final String ERRCODE_BUFFER_NULL = "K0047"; //$NON-NLS-1$
+
+ private static final String ERRCODE_OFFSET_OUTOFBOUND = "K002f"; //$NON-NLS-1$
+
+ PlainSocketImpl socket;
/**
* Constructs a SocketOutputStream for the <code>socket</code>. Write
@@ -39,7 +40,7 @@
public SocketOutputStream(SocketImpl socket) {
super();
- this.socket = socket;
+ this.socket = (PlainSocketImpl) socket;
}
/**
@@ -86,12 +87,14 @@
if (0 <= offset && offset <= buffer.length && 0 <= count
&& count <= buffer.length - offset) {
socket.write(buffer, offset, count);
- } else
+ } else {
throw new ArrayIndexOutOfBoundsException(com.ibm.oti.util.Msg
- .getString("K002f"));
- } else
+ .getString(ERRCODE_OFFSET_OUTOFBOUND));
+ }
+ } else {
throw new NullPointerException(com.ibm.oti.util.Msg
- .getString("K0047"));
+ .getString(ERRCODE_BUFFER_NULL));
+ }
}
/**
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/Socks4Message.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socks4Message.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/Socks4Message.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/Socks4Message.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socks4Message.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socks4Message.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/Socks4Message.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 2000, 2003 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,8 +13,7 @@
* limitations under the License.
*/
-package java.net;
-
+package org.apache.harmony.luni.net;
import java.io.UnsupportedEncodingException;
@@ -202,8 +201,9 @@
int lastIndex = index + maxLength;
String result;
- while (index < lastIndex && (buffer[index] != 0))
+ while (index < lastIndex && (buffer[index] != 0)) {
index++;
+ }
try {
result = new String(buffer, offset, index - offset, "ISO8859_1");
} catch (UnsupportedEncodingException e) {