home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / Socket.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  9.7 KB  |  656 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.Proxy.Type;
  7. import java.nio.channels.SocketChannel;
  8. import java.security.AccessController;
  9. import java.security.PrivilegedActionException;
  10.  
  11. public class Socket {
  12.    private boolean created;
  13.    private boolean bound;
  14.    private boolean connected;
  15.    private boolean closed;
  16.    private Object closeLock;
  17.    private boolean shutIn;
  18.    private boolean shutOut;
  19.    SocketImpl impl;
  20.    private boolean oldImpl;
  21.    private static SocketImplFactory factory = null;
  22.  
  23.    public Socket() {
  24.       this.created = false;
  25.       this.bound = false;
  26.       this.connected = false;
  27.       this.closed = false;
  28.       this.closeLock = new Object();
  29.       this.shutIn = false;
  30.       this.shutOut = false;
  31.       this.oldImpl = false;
  32.       this.setImpl();
  33.    }
  34.  
  35.    public Socket(Proxy var1) {
  36.       this.created = false;
  37.       this.bound = false;
  38.       this.connected = false;
  39.       this.closed = false;
  40.       this.closeLock = new Object();
  41.       this.shutIn = false;
  42.       this.shutOut = false;
  43.       this.oldImpl = false;
  44.       if (var1 != null && var1.type() == Type.SOCKS) {
  45.          SecurityManager var2 = System.getSecurityManager();
  46.          InetSocketAddress var3 = (InetSocketAddress)var1.address();
  47.          if (var2 != null) {
  48.             if (var3.isUnresolved()) {
  49.                var2.checkConnect(var3.getHostName(), var3.getPort());
  50.             } else {
  51.                var2.checkConnect(var3.getAddress().getHostAddress(), var3.getPort());
  52.             }
  53.          }
  54.  
  55.          this.impl = new SocksSocketImpl(var1);
  56.          this.impl.setSocket(this);
  57.       } else {
  58.          if (var1 != Proxy.NO_PROXY) {
  59.             throw new IllegalArgumentException("Invalid Proxy");
  60.          }
  61.  
  62.          if (factory == null) {
  63.             this.impl = new PlainSocketImpl();
  64.             this.impl.setSocket(this);
  65.          } else {
  66.             this.setImpl();
  67.          }
  68.       }
  69.  
  70.    }
  71.  
  72.    protected Socket(SocketImpl var1) throws SocketException {
  73.       this.created = false;
  74.       this.bound = false;
  75.       this.connected = false;
  76.       this.closed = false;
  77.       this.closeLock = new Object();
  78.       this.shutIn = false;
  79.       this.shutOut = false;
  80.       this.oldImpl = false;
  81.       this.impl = var1;
  82.       if (var1 != null) {
  83.          this.checkOldImpl();
  84.          this.impl.setSocket(this);
  85.       }
  86.  
  87.    }
  88.  
  89.    public Socket(String var1, int var2) throws UnknownHostException, IOException {
  90.       this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(0), true);
  91.    }
  92.  
  93.    public Socket(InetAddress var1, int var2) throws IOException {
  94.       this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(0), true);
  95.    }
  96.  
  97.    public Socket(String var1, int var2, InetAddress var3, int var4) throws IOException {
  98.       this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(var3, var4), true);
  99.    }
  100.  
  101.    public Socket(InetAddress var1, int var2, InetAddress var3, int var4) throws IOException {
  102.       this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(var3, var4), true);
  103.    }
  104.  
  105.    /** @deprecated */
  106.    @Deprecated
  107.    public Socket(String var1, int var2, boolean var3) throws IOException {
  108.       this(var1 != null ? new InetSocketAddress(var1, var2) : new InetSocketAddress(InetAddress.getByName((String)null), var2), new InetSocketAddress(0), var3);
  109.    }
  110.  
  111.    /** @deprecated */
  112.    @Deprecated
  113.    public Socket(InetAddress var1, int var2, boolean var3) throws IOException {
  114.       this(var1 != null ? new InetSocketAddress(var1, var2) : null, new InetSocketAddress(0), var3);
  115.    }
  116.  
  117.    private Socket(SocketAddress var1, SocketAddress var2, boolean var3) throws IOException {
  118.       this.created = false;
  119.       this.bound = false;
  120.       this.connected = false;
  121.       this.closed = false;
  122.       this.closeLock = new Object();
  123.       this.shutIn = false;
  124.       this.shutOut = false;
  125.       this.oldImpl = false;
  126.       this.setImpl();
  127.       if (var1 == null) {
  128.          throw new NullPointerException();
  129.       } else {
  130.          try {
  131.             this.createImpl(var3);
  132.             if (var2 == null) {
  133.                var2 = new InetSocketAddress(0);
  134.             }
  135.  
  136.             this.bind((SocketAddress)var2);
  137.             if (var1 != null) {
  138.                this.connect(var1);
  139.             }
  140.  
  141.          } catch (IOException var5) {
  142.             this.close();
  143.             throw var5;
  144.          }
  145.       }
  146.    }
  147.  
  148.    void createImpl(boolean var1) throws SocketException {
  149.       if (this.impl == null) {
  150.          this.setImpl();
  151.       }
  152.  
  153.       try {
  154.          this.impl.create(var1);
  155.          this.created = true;
  156.       } catch (IOException var3) {
  157.          throw new SocketException(var3.getMessage());
  158.       }
  159.    }
  160.  
  161.    private void checkOldImpl() {
  162.       if (this.impl != null) {
  163.          this.oldImpl = (Boolean)AccessController.doPrivileged(new 1(this));
  164.       }
  165.    }
  166.  
  167.    void setImpl() {
  168.       if (factory != null) {
  169.          this.impl = factory.createSocketImpl();
  170.          this.checkOldImpl();
  171.       } else {
  172.          this.impl = new SocksSocketImpl();
  173.       }
  174.  
  175.       if (this.impl != null) {
  176.          this.impl.setSocket(this);
  177.       }
  178.  
  179.    }
  180.  
  181.    SocketImpl getImpl() throws SocketException {
  182.       if (!this.created) {
  183.          this.createImpl(true);
  184.       }
  185.  
  186.       return this.impl;
  187.    }
  188.  
  189.    public void connect(SocketAddress var1) throws IOException {
  190.       this.connect(var1, 0);
  191.    }
  192.  
  193.    public void connect(SocketAddress var1, int var2) throws IOException {
  194.       if (var1 == null) {
  195.          throw new IllegalArgumentException("connect: The address can't be null");
  196.       } else if (var2 < 0) {
  197.          throw new IllegalArgumentException("connect: timeout can't be negative");
  198.       } else if (this.isClosed()) {
  199.          throw new SocketException("Socket is closed");
  200.       } else if (!this.oldImpl && this.isConnected()) {
  201.          throw new SocketException("already connected");
  202.       } else if (!(var1 instanceof InetSocketAddress)) {
  203.          throw new IllegalArgumentException("Unsupported address type");
  204.       } else {
  205.          InetSocketAddress var3 = (InetSocketAddress)var1;
  206.          SecurityManager var4 = System.getSecurityManager();
  207.          if (var4 != null) {
  208.             if (var3.isUnresolved()) {
  209.                var4.checkConnect(var3.getHostName(), var3.getPort());
  210.             } else {
  211.                var4.checkConnect(var3.getAddress().getHostAddress(), var3.getPort());
  212.             }
  213.          }
  214.  
  215.          if (!this.created) {
  216.             this.createImpl(true);
  217.          }
  218.  
  219.          if (!this.oldImpl) {
  220.             this.impl.connect(var3, var2);
  221.          } else {
  222.             if (var2 != 0) {
  223.                throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)");
  224.             }
  225.  
  226.             if (var3.isUnresolved()) {
  227.                this.impl.connect(var3.getAddress().getHostName(), var3.getPort());
  228.             } else {
  229.                this.impl.connect(var3.getAddress(), var3.getPort());
  230.             }
  231.          }
  232.  
  233.          this.connected = true;
  234.          this.bound = true;
  235.       }
  236.    }
  237.  
  238.    public void bind(SocketAddress var1) throws IOException {
  239.       if (this.isClosed()) {
  240.          throw new SocketException("Socket is closed");
  241.       } else if (!this.oldImpl && this.isBound()) {
  242.          throw new SocketException("Already bound");
  243.       } else if (var1 != null && !(var1 instanceof InetSocketAddress)) {
  244.          throw new IllegalArgumentException("Unsupported address type");
  245.       } else {
  246.          InetSocketAddress var2 = (InetSocketAddress)var1;
  247.          if (var2 != null && var2.isUnresolved()) {
  248.             throw new SocketException("Unresolved address");
  249.          } else {
  250.             if (var1 == null) {
  251.                this.getImpl().bind(InetAddress.anyLocalAddress(), 0);
  252.             } else {
  253.                this.getImpl().bind(var2.getAddress(), var2.getPort());
  254.             }
  255.  
  256.             this.bound = true;
  257.          }
  258.       }
  259.    }
  260.  
  261.    final void postAccept() {
  262.       this.connected = true;
  263.       this.created = true;
  264.       this.bound = true;
  265.    }
  266.  
  267.    void setCreated() {
  268.       this.created = true;
  269.    }
  270.  
  271.    void setBound() {
  272.       this.bound = true;
  273.    }
  274.  
  275.    void setConnected() {
  276.       this.connected = true;
  277.    }
  278.  
  279.    public InetAddress getInetAddress() {
  280.       if (!this.isConnected()) {
  281.          return null;
  282.       } else {
  283.          try {
  284.             return this.getImpl().getInetAddress();
  285.          } catch (SocketException var2) {
  286.             return null;
  287.          }
  288.       }
  289.    }
  290.  
  291.    public InetAddress getLocalAddress() {
  292.       if (!this.isBound()) {
  293.          return InetAddress.anyLocalAddress();
  294.       } else {
  295.          Object var1 = null;
  296.  
  297.          try {
  298.             var4 = (InetAddress)this.getImpl().getOption(15);
  299.             if (var4.isAnyLocalAddress()) {
  300.                var4 = InetAddress.anyLocalAddress();
  301.             }
  302.          } catch (Exception var3) {
  303.             var4 = InetAddress.anyLocalAddress();
  304.          }
  305.  
  306.          return var4;
  307.       }
  308.    }
  309.  
  310.    public int getPort() {
  311.       if (!this.isConnected()) {
  312.          return 0;
  313.       } else {
  314.          try {
  315.             return this.getImpl().getPort();
  316.          } catch (SocketException var2) {
  317.             return -1;
  318.          }
  319.       }
  320.    }
  321.  
  322.    public int getLocalPort() {
  323.       if (!this.isBound()) {
  324.          return -1;
  325.       } else {
  326.          try {
  327.             return this.getImpl().getLocalPort();
  328.          } catch (SocketException var2) {
  329.             return -1;
  330.          }
  331.       }
  332.    }
  333.  
  334.    public SocketAddress getRemoteSocketAddress() {
  335.       return !this.isConnected() ? null : new InetSocketAddress(this.getInetAddress(), this.getPort());
  336.    }
  337.  
  338.    public SocketAddress getLocalSocketAddress() {
  339.       return !this.isBound() ? null : new InetSocketAddress(this.getLocalAddress(), this.getLocalPort());
  340.    }
  341.  
  342.    public SocketChannel getChannel() {
  343.       return null;
  344.    }
  345.  
  346.    public InputStream getInputStream() throws IOException {
  347.       if (this.isClosed()) {
  348.          throw new SocketException("Socket is closed");
  349.       } else if (!this.isConnected()) {
  350.          throw new SocketException("Socket is not connected");
  351.       } else if (this.isInputShutdown()) {
  352.          throw new SocketException("Socket input is shutdown");
  353.       } else {
  354.          Object var2 = null;
  355.  
  356.          try {
  357.             InputStream var5 = (InputStream)AccessController.doPrivileged(new 2(this));
  358.             return var5;
  359.          } catch (PrivilegedActionException var4) {
  360.             throw (IOException)var4.getException();
  361.          }
  362.       }
  363.    }
  364.  
  365.    public OutputStream getOutputStream() throws IOException {
  366.       if (this.isClosed()) {
  367.          throw new SocketException("Socket is closed");
  368.       } else if (!this.isConnected()) {
  369.          throw new SocketException("Socket is not connected");
  370.       } else if (this.isOutputShutdown()) {
  371.          throw new SocketException("Socket output is shutdown");
  372.       } else {
  373.          Object var2 = null;
  374.  
  375.          try {
  376.             OutputStream var5 = (OutputStream)AccessController.doPrivileged(new 3(this));
  377.             return var5;
  378.          } catch (PrivilegedActionException var4) {
  379.             throw (IOException)var4.getException();
  380.          }
  381.       }
  382.    }
  383.  
  384.    public void setTcpNoDelay(boolean var1) throws SocketException {
  385.       if (this.isClosed()) {
  386.          throw new SocketException("Socket is closed");
  387.       } else {
  388.          this.getImpl().setOption(1, var1);
  389.       }
  390.    }
  391.  
  392.    public boolean getTcpNoDelay() throws SocketException {
  393.       if (this.isClosed()) {
  394.          throw new SocketException("Socket is closed");
  395.       } else {
  396.          return (Boolean)this.getImpl().getOption(1);
  397.       }
  398.    }
  399.  
  400.    public void setSoLinger(boolean var1, int var2) throws SocketException {
  401.       if (this.isClosed()) {
  402.          throw new SocketException("Socket is closed");
  403.       } else {
  404.          if (!var1) {
  405.             this.getImpl().setOption(128, new Boolean(var1));
  406.          } else {
  407.             if (var2 < 0) {
  408.                throw new IllegalArgumentException("invalid value for SO_LINGER");
  409.             }
  410.  
  411.             if (var2 > 65535) {
  412.                var2 = 65535;
  413.             }
  414.  
  415.             this.getImpl().setOption(128, new Integer(var2));
  416.          }
  417.  
  418.       }
  419.    }
  420.  
  421.    public int getSoLinger() throws SocketException {
  422.       if (this.isClosed()) {
  423.          throw new SocketException("Socket is closed");
  424.       } else {
  425.          Object var1 = this.getImpl().getOption(128);
  426.          return var1 instanceof Integer ? (Integer)var1 : -1;
  427.       }
  428.    }
  429.  
  430.    public void sendUrgentData(int var1) throws IOException {
  431.       if (!this.getImpl().supportsUrgentData()) {
  432.          throw new SocketException("Urgent data not supported");
  433.       } else {
  434.          this.getImpl().sendUrgentData(var1);
  435.       }
  436.    }
  437.  
  438.    public void setOOBInline(boolean var1) throws SocketException {
  439.       if (this.isClosed()) {
  440.          throw new SocketException("Socket is closed");
  441.       } else {
  442.          this.getImpl().setOption(4099, var1);
  443.       }
  444.    }
  445.  
  446.    public boolean getOOBInline() throws SocketException {
  447.       if (this.isClosed()) {
  448.          throw new SocketException("Socket is closed");
  449.       } else {
  450.          return (Boolean)this.getImpl().getOption(4099);
  451.       }
  452.    }
  453.  
  454.    public synchronized void setSoTimeout(int var1) throws SocketException {
  455.       if (this.isClosed()) {
  456.          throw new SocketException("Socket is closed");
  457.       } else if (var1 < 0) {
  458.          throw new IllegalArgumentException("timeout can't be negative");
  459.       } else {
  460.          this.getImpl().setOption(4102, new Integer(var1));
  461.       }
  462.    }
  463.  
  464.    public synchronized int getSoTimeout() throws SocketException {
  465.       if (this.isClosed()) {
  466.          throw new SocketException("Socket is closed");
  467.       } else {
  468.          Object var1 = this.getImpl().getOption(4102);
  469.          return var1 instanceof Integer ? (Integer)var1 : 0;
  470.       }
  471.    }
  472.  
  473.    public synchronized void setSendBufferSize(int var1) throws SocketException {
  474.       if (var1 <= 0) {
  475.          throw new IllegalArgumentException("negative send size");
  476.       } else if (this.isClosed()) {
  477.          throw new SocketException("Socket is closed");
  478.       } else {
  479.          this.getImpl().setOption(4097, new Integer(var1));
  480.       }
  481.    }
  482.  
  483.    public synchronized int getSendBufferSize() throws SocketException {
  484.       if (this.isClosed()) {
  485.          throw new SocketException("Socket is closed");
  486.       } else {
  487.          int var1 = 0;
  488.          Object var2 = this.getImpl().getOption(4097);
  489.          if (var2 instanceof Integer) {
  490.             var1 = (Integer)var2;
  491.          }
  492.  
  493.          return var1;
  494.       }
  495.    }
  496.  
  497.    public synchronized void setReceiveBufferSize(int var1) throws SocketException {
  498.       if (var1 <= 0) {
  499.          throw new IllegalArgumentException("invalid receive size");
  500.       } else if (this.isClosed()) {
  501.          throw new SocketException("Socket is closed");
  502.       } else {
  503.          this.getImpl().setOption(4098, new Integer(var1));
  504.       }
  505.    }
  506.  
  507.    public synchronized int getReceiveBufferSize() throws SocketException {
  508.       if (this.isClosed()) {
  509.          throw new SocketException("Socket is closed");
  510.       } else {
  511.          int var1 = 0;
  512.          Object var2 = this.getImpl().getOption(4098);
  513.          if (var2 instanceof Integer) {
  514.             var1 = (Integer)var2;
  515.          }
  516.  
  517.          return var1;
  518.       }
  519.    }
  520.  
  521.    public void setKeepAlive(boolean var1) throws SocketException {
  522.       if (this.isClosed()) {
  523.          throw new SocketException("Socket is closed");
  524.       } else {
  525.          this.getImpl().setOption(8, var1);
  526.       }
  527.    }
  528.  
  529.    public boolean getKeepAlive() throws SocketException {
  530.       if (this.isClosed()) {
  531.          throw new SocketException("Socket is closed");
  532.       } else {
  533.          return (Boolean)this.getImpl().getOption(8);
  534.       }
  535.    }
  536.  
  537.    public void setTrafficClass(int var1) throws SocketException {
  538.       if (var1 >= 0 && var1 <= 255) {
  539.          if (this.isClosed()) {
  540.             throw new SocketException("Socket is closed");
  541.          } else {
  542.             this.getImpl().setOption(3, new Integer(var1));
  543.          }
  544.       } else {
  545.          throw new IllegalArgumentException("tc is not in range 0 -- 255");
  546.       }
  547.    }
  548.  
  549.    public int getTrafficClass() throws SocketException {
  550.       return (Integer)((Integer)this.getImpl().getOption(3));
  551.    }
  552.  
  553.    public void setReuseAddress(boolean var1) throws SocketException {
  554.       if (this.isClosed()) {
  555.          throw new SocketException("Socket is closed");
  556.       } else {
  557.          this.getImpl().setOption(4, var1);
  558.       }
  559.    }
  560.  
  561.    public boolean getReuseAddress() throws SocketException {
  562.       if (this.isClosed()) {
  563.          throw new SocketException("Socket is closed");
  564.       } else {
  565.          return (Boolean)((Boolean)this.getImpl().getOption(4));
  566.       }
  567.    }
  568.  
  569.    public synchronized void close() throws IOException {
  570.       synchronized(this.closeLock) {
  571.          if (!this.isClosed()) {
  572.             if (this.created) {
  573.                this.impl.close();
  574.             }
  575.  
  576.             this.closed = true;
  577.          }
  578.       }
  579.    }
  580.  
  581.    public void shutdownInput() throws IOException {
  582.       if (this.isClosed()) {
  583.          throw new SocketException("Socket is closed");
  584.       } else if (!this.isConnected()) {
  585.          throw new SocketException("Socket is not connected");
  586.       } else if (this.isInputShutdown()) {
  587.          throw new SocketException("Socket input is already shutdown");
  588.       } else {
  589.          this.getImpl().shutdownInput();
  590.          this.shutIn = true;
  591.       }
  592.    }
  593.  
  594.    public void shutdownOutput() throws IOException {
  595.       if (this.isClosed()) {
  596.          throw new SocketException("Socket is closed");
  597.       } else if (!this.isConnected()) {
  598.          throw new SocketException("Socket is not connected");
  599.       } else if (this.isOutputShutdown()) {
  600.          throw new SocketException("Socket output is already shutdown");
  601.       } else {
  602.          this.getImpl().shutdownOutput();
  603.          this.shutOut = true;
  604.       }
  605.    }
  606.  
  607.    public String toString() {
  608.       try {
  609.          if (this.isConnected()) {
  610.             return "Socket[addr=" + this.getImpl().getInetAddress() + ",port=" + this.getImpl().getPort() + ",localport=" + this.getImpl().getLocalPort() + "]";
  611.          }
  612.       } catch (SocketException var2) {
  613.       }
  614.  
  615.       return "Socket[unconnected]";
  616.    }
  617.  
  618.    public boolean isConnected() {
  619.       return this.connected || this.oldImpl;
  620.    }
  621.  
  622.    public boolean isBound() {
  623.       return this.bound || this.oldImpl;
  624.    }
  625.  
  626.    public boolean isClosed() {
  627.       synchronized(this.closeLock) {
  628.          return this.closed;
  629.       }
  630.    }
  631.  
  632.    public boolean isInputShutdown() {
  633.       return this.shutIn;
  634.    }
  635.  
  636.    public boolean isOutputShutdown() {
  637.       return this.shutOut;
  638.    }
  639.  
  640.    public static synchronized void setSocketImplFactory(SocketImplFactory var0) throws IOException {
  641.       if (factory != null) {
  642.          throw new SocketException("factory already defined");
  643.       } else {
  644.          SecurityManager var1 = System.getSecurityManager();
  645.          if (var1 != null) {
  646.             var1.checkSetFactory();
  647.          }
  648.  
  649.          factory = var0;
  650.       }
  651.    }
  652.  
  653.    public void setPerformancePreferences(int var1, int var2, int var3) {
  654.    }
  655. }
  656.