home *** CD-ROM | disk | FTP | other *** search
Wrap
package java.net; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Proxy.Type; import java.nio.channels.SocketChannel; import java.security.AccessController; import java.security.PrivilegedActionException; public class Socket { private boolean created; private boolean bound; private boolean connected; private boolean closed; private Object closeLock; private boolean shutIn; private boolean shutOut; SocketImpl impl; private boolean oldImpl; private static SocketImplFactory factory = null; public Socket() { this.created = false; this.bound = false; this.connected = false; this.closed = false; this.closeLock = new Object(); this.shutIn = false; this.shutOut = false; this.oldImpl = false; this.setImpl(); } public Socket(Proxy var1) { this.created = false; this.bound = false; this.connected = false; this.closed = false; this.closeLock = new Object(); this.shutIn = false; this.shutOut = false; this.oldImpl = false; if (var1 != null && var1.type() == Type.SOCKS) { SecurityManager var2 = System.getSecurityManager(); InetSocketAddress var3 = (InetSocketAddress)var1.address(); if (var2 != null) { if (var3.isUnresolved()) { var2.checkConnect(var3.getHostName(), var3.getPort()); } else { var2.checkConnect(var3.getAddress().getHostAddress(), var3.getPort()); } } this.impl = new SocksSocketImpl(var1); this.impl.setSocket(this); } else { if (var1 != Proxy.NO_PROXY) { throw new IllegalArgumentException("Invalid Proxy"); } if (factory == null) { this.impl = new PlainSocketImpl(); this.impl.setSocket(this); } else { this.setImpl(); } } } protected Socket(SocketImpl var1) throws SocketException { this.created = false; this.bound = false; this.connected = false; this.closed = false; this.closeLock = new Object(); this.shutIn = false; this.shutOut = false; this.oldImpl = false; this.impl = var1; if (var1 != null) { this.checkOldImpl(); this.impl.setSocket(this); } } public Socket(String var1, int var2) throws UnknownHostException, IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(0), true); } public Socket(InetAddress var1, int var2) throws IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(0), true); } public Socket(String var1, int var2, InetAddress var3, int var4) throws IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(var3, var4), true); } public Socket(InetAddress var1, int var2, InetAddress var3, int var4) throws IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(var3, var4), true); } /** @deprecated */ @Deprecated public Socket(String var1, int var2, boolean var3) throws IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(0), var3); } /** @deprecated */ @Deprecated public Socket(InetAddress var1, int var2, boolean var3) throws IOException { this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(0), var3); } private Socket(SocketAddress var1, SocketAddress var2, boolean var3) throws IOException { this.created = false; this.bound = false; this.connected = false; this.closed = false; this.closeLock = new Object(); this.shutIn = false; this.shutOut = false; this.oldImpl = false; this.setImpl(); if (var1 == null) { throw new NullPointerException(); } else { try { this.createImpl(var3); if (var2 == null) { var2 = new InetSocketAddress(0); } this.bind((SocketAddress)var2); if (var1 != null) { this.connect(var1); } } catch (IOException var5) { this.close(); throw var5; } } } void createImpl(boolean var1) throws SocketException { if (this.impl == null) { this.setImpl(); } try { this.impl.create(var1); this.created = true; } catch (IOException var3) { throw new SocketException(var3.getMessage()); } } private void checkOldImpl() { if (this.impl != null) { this.oldImpl = (Boolean)AccessController.doPrivileged(new 1(this)); } } void setImpl() { if (factory != null) { this.impl = factory.createSocketImpl(); this.checkOldImpl(); } else { this.impl = new SocksSocketImpl(); } if (this.impl != null) { this.impl.setSocket(this); } } SocketImpl getImpl() throws SocketException { if (!this.created) { this.createImpl(true); } return this.impl; } public void connect(SocketAddress var1) throws IOException { this.connect(var1, 0); } public void connect(SocketAddress var1, int var2) throws IOException { if (var1 == null) { throw new IllegalArgumentException("connect: The address can't be null"); } else if (var2 < 0) { throw new IllegalArgumentException("connect: timeout can't be negative"); } else if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.oldImpl && this.isConnected()) { throw new SocketException("already connected"); } else if (!(var1 instanceof InetSocketAddress)) { throw new IllegalArgumentException("Unsupported address type"); } else { InetSocketAddress var3 = (InetSocketAddress)var1; SecurityManager var4 = System.getSecurityManager(); if (var4 != null) { if (var3.isUnresolved()) { var4.checkConnect(var3.getHostName(), var3.getPort()); } else { var4.checkConnect(var3.getAddress().getHostAddress(), var3.getPort()); } } if (!this.created) { this.createImpl(true); } if (!this.oldImpl) { this.impl.connect(var3, var2); } else { if (var2 != 0) { throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)"); } if (var3.isUnresolved()) { this.impl.connect(var3.getAddress().getHostName(), var3.getPort()); } else { this.impl.connect(var3.getAddress(), var3.getPort()); } } this.connected = true; this.bound = true; } } public void bind(SocketAddress var1) throws IOException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.oldImpl && this.isBound()) { throw new SocketException("Already bound"); } else if (var1 != null && !(var1 instanceof InetSocketAddress)) { throw new IllegalArgumentException("Unsupported address type"); } else { InetSocketAddress var2 = (InetSocketAddress)var1; if (var2 != null && var2.isUnresolved()) { throw new SocketException("Unresolved address"); } else { if (var1 == null) { this.getImpl().bind(InetAddress.anyLocalAddress(), 0); } else { this.getImpl().bind(var2.getAddress(), var2.getPort()); } this.bound = true; } } } final void postAccept() { this.connected = true; this.created = true; this.bound = true; } void setCreated() { this.created = true; } void setBound() { this.bound = true; } void setConnected() { this.connected = true; } public InetAddress getInetAddress() { if (!this.isConnected()) { return null; } else { try { return this.getImpl().getInetAddress(); } catch (SocketException var2) { return null; } } } public InetAddress getLocalAddress() { if (!this.isBound()) { return InetAddress.anyLocalAddress(); } else { Object var1 = null; try { var4 = (InetAddress)this.getImpl().getOption(15); if (var4.isAnyLocalAddress()) { var4 = InetAddress.anyLocalAddress(); } } catch (Exception var3) { var4 = InetAddress.anyLocalAddress(); } return var4; } } public int getPort() { if (!this.isConnected()) { return 0; } else { try { return this.getImpl().getPort(); } catch (SocketException var2) { return -1; } } } public int getLocalPort() { if (!this.isBound()) { return -1; } else { try { return this.getImpl().getLocalPort(); } catch (SocketException var2) { return -1; } } } public SocketAddress getRemoteSocketAddress() { return !this.isConnected() ? null : new InetSocketAddress(this.getInetAddress(), this.getPort()); } public SocketAddress getLocalSocketAddress() { return !this.isBound() ? null : new InetSocketAddress(this.getLocalAddress(), this.getLocalPort()); } public SocketChannel getChannel() { return null; } public InputStream getInputStream() throws IOException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.isConnected()) { throw new SocketException("Socket is not connected"); } else if (this.isInputShutdown()) { throw new SocketException("Socket input is shutdown"); } else { Object var2 = null; try { InputStream var5 = (InputStream)AccessController.doPrivileged(new 2(this)); return var5; } catch (PrivilegedActionException var4) { throw (IOException)var4.getException(); } } } public OutputStream getOutputStream() throws IOException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.isConnected()) { throw new SocketException("Socket is not connected"); } else if (this.isOutputShutdown()) { throw new SocketException("Socket output is shutdown"); } else { Object var2 = null; try { OutputStream var5 = (OutputStream)AccessController.doPrivileged(new 3(this)); return var5; } catch (PrivilegedActionException var4) { throw (IOException)var4.getException(); } } } public void setTcpNoDelay(boolean var1) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(1, var1); } } public boolean getTcpNoDelay() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { return (Boolean)this.getImpl().getOption(1); } } public void setSoLinger(boolean var1, int var2) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { if (!var1) { this.getImpl().setOption(128, new Boolean(var1)); } else { if (var2 < 0) { throw new IllegalArgumentException("invalid value for SO_LINGER"); } if (var2 > 65535) { var2 = 65535; } this.getImpl().setOption(128, new Integer(var2)); } } } public int getSoLinger() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { Object var1 = this.getImpl().getOption(128); return var1 instanceof Integer ? (Integer)var1 : -1; } } public void sendUrgentData(int var1) throws IOException { if (!this.getImpl().supportsUrgentData()) { throw new SocketException("Urgent data not supported"); } else { this.getImpl().sendUrgentData(var1); } } public void setOOBInline(boolean var1) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(4099, var1); } } public boolean getOOBInline() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { return (Boolean)this.getImpl().getOption(4099); } } public synchronized void setSoTimeout(int var1) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (var1 < 0) { throw new IllegalArgumentException("timeout can't be negative"); } else { this.getImpl().setOption(4102, new Integer(var1)); } } public synchronized int getSoTimeout() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { Object var1 = this.getImpl().getOption(4102); return var1 instanceof Integer ? (Integer)var1 : 0; } } public synchronized void setSendBufferSize(int var1) throws SocketException { if (var1 <= 0) { throw new IllegalArgumentException("negative send size"); } else if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(4097, new Integer(var1)); } } public synchronized int getSendBufferSize() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { int var1 = 0; Object var2 = this.getImpl().getOption(4097); if (var2 instanceof Integer) { var1 = (Integer)var2; } return var1; } } public synchronized void setReceiveBufferSize(int var1) throws SocketException { if (var1 <= 0) { throw new IllegalArgumentException("invalid receive size"); } else if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(4098, new Integer(var1)); } } public synchronized int getReceiveBufferSize() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { int var1 = 0; Object var2 = this.getImpl().getOption(4098); if (var2 instanceof Integer) { var1 = (Integer)var2; } return var1; } } public void setKeepAlive(boolean var1) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(8, var1); } } public boolean getKeepAlive() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { return (Boolean)this.getImpl().getOption(8); } } public void setTrafficClass(int var1) throws SocketException { if (var1 >= 0 && var1 <= 255) { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(3, new Integer(var1)); } } else { throw new IllegalArgumentException("tc is not in range 0 -- 255"); } } public int getTrafficClass() throws SocketException { return (Integer)((Integer)this.getImpl().getOption(3)); } public void setReuseAddress(boolean var1) throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { this.getImpl().setOption(4, var1); } } public boolean getReuseAddress() throws SocketException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else { return (Boolean)((Boolean)this.getImpl().getOption(4)); } } public synchronized void close() throws IOException { synchronized(this.closeLock) { if (!this.isClosed()) { if (this.created) { this.impl.close(); } this.closed = true; } } } public void shutdownInput() throws IOException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.isConnected()) { throw new SocketException("Socket is not connected"); } else if (this.isInputShutdown()) { throw new SocketException("Socket input is already shutdown"); } else { this.getImpl().shutdownInput(); this.shutIn = true; } } public void shutdownOutput() throws IOException { if (this.isClosed()) { throw new SocketException("Socket is closed"); } else if (!this.isConnected()) { throw new SocketException("Socket is not connected"); } else if (this.isOutputShutdown()) { throw new SocketException("Socket output is already shutdown"); } else { this.getImpl().shutdownOutput(); this.shutOut = true; } } public String toString() { try { if (this.isConnected()) { return "Socket[addr=" + this.getImpl().getInetAddress() + ",port=" + this.getImpl().getPort() + ",localport=" + this.getImpl().getLocalPort() + "]"; } } catch (SocketException var2) { } return "Socket[unconnected]"; } public boolean isConnected() { return this.connected || this.oldImpl; } public boolean isBound() { return this.bound || this.oldImpl; } public boolean isClosed() { synchronized(this.closeLock) { return this.closed; } } public boolean isInputShutdown() { return this.shutIn; } public boolean isOutputShutdown() { return this.shutOut; } public static synchronized void setSocketImplFactory(SocketImplFactory var0) throws IOException { if (factory != null) { throw new SocketException("factory already defined"); } else { SecurityManager var1 = System.getSecurityManager(); if (var1 != null) { var1.checkSetFactory(); } factory = var0; } } public void setPerformancePreferences(int var1, int var2, int var3) { } }