gt:         if (!success) {
context: /*
   * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   */
  package com.sun.webkit.network;
  
  import com.sun.webkit.Invoker;
  import com.sun.webkit.WebPage;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import static java.lang.String.format;
  import java.net.ConnectException;
  import java.net.InetSocketAddress;
  import java.net.NoRouteToHostException;
  import java.net.PortUnreachableException;
  import java.net.Proxy;
  import java.net.ProxySelector;
  import java.net.Socket;
  import java.net.SocketException;
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.net.UnknownHostException;
  import java.security.AccessController;
  import java.security.PrivilegedAction;
  import java.security.PrivilegedActionException;
  import java.security.PrivilegedExceptionAction;
  import java.util.List;
  import java.util.concurrent.SynchronousQueue;
  import java.util.concurrent.ThreadFactory;
  import java.util.concurrent.ThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.atomic.AtomicInteger;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import javax.net.ssl.HttpsURLConnection;
  import javax.net.ssl.SSLException;
  import javax.net.ssl.SSLSocket;
  
  final class SocketStreamHandle {
      private static final Pattern FIRST_LINE_PATTERN = Pattern.compile(
              "^HTTP/1.[01]\\s+(\\d{3})(?:\\s.*)?$");
      private static final Logger logger = Logger.getLogger(
              SocketStreamHandle.class.getName());
      private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
              0, Integer.MAX_VALUE,
              10, TimeUnit.SECONDS,
              new SynchronousQueue<Runnable>(),
              new CustomThreadFactory());
  
      private enum State {ACTIVE, CLOSE_REQUESTED, DISPOSED}
  
      private final String host;
      private final int port;
      private final boolean ssl;
      private final WebPage webPage;
      private final long data;
      private volatile Socket socket;
      private volatile State state = State.ACTIVE;
      private volatile boolean connected;
  
      private SocketStreamHandle(String host, int port, boolean ssl,
                                 WebPage webPage, long data)
      {
          this.host = host;
          this.port = port;
          this.ssl = ssl;
          this.webPage = webPage;
          this.data = data;
      }
  
      private static SocketStreamHandle fwkCreate(String host, int port,
                                                  boolean ssl, WebPage webPage,
                                                  long data)
      {
          final SocketStreamHandle ssh =
                  new SocketStreamHandle(host, port, ssl, webPage, data);
          logger.log(Level.FINEST, "Starting {0}", ssh);
          threadPool.submit(new Runnable() {
              @Override public void run() {
                  ssh.run();
              }
          });
          return ssh;
      }
  
      private void run() {
          if (webPage == null) {
              logger.log(Level.FINEST, "{0} is not associated with any web "
                      + "page, aborted", this);
              // In theory we could pump this error through the doRun()'s
              // error handling code but in that case that error handling
              // code would have to run outside the doPrivileged block,
              // which is something we want to avoid.
              didFail(0, "Web socket is not associated with any web page");
              didClose();
              return;
          }
          AccessController.doPrivileged(new PrivilegedAction<Void>() {
              @Override public Void run() {
                  doRun();
                  return null;
              }
          }, webPage.getAccessControlContext());
      }
  
      private void doRun() {
          Throwable error = null;
          String errorDescription = null;
          try {
              logger.log(Level.FINEST, "{0} started", this);
              connect();
              connected = true;
              logger.log(Level.FINEST, "{0} connected", this);
              didOpen();
              InputStream is = socket.getInputStream();
              while (true) {
                  byte[] buffer = new byte[8192];
                  int n = is.read(buffer);
                  if(n > 0) {
                      if (logger.isLoggable(Level.FINEST)) {
                          logger.log(Level.FINEST, format("%s received len: [%d],"
                                  + " data:%s", this, n, dump(buffer, n)));
                      }
                      didReceiveData(buffer, n);
                  } else {
                      logger.log(Level.FINEST, "{0} connection closed by "
                              + "remote host", this);
                      break;
                  }
              }
          } catch (UnknownHostException ex) {
              error = ex;
              errorDescription = "Unknown host";
          } catch (ConnectException ex) {
              error = ex;
              errorDescription = "Unable to connect";
          } catch (NoRouteToHostException ex) {
              error = ex;
              errorDescription = "No route to host";
          } catch (PortUnreachableException ex) {
              error = ex;
              errorDescription = "Port unreachable";
          } catch (SocketException ex) {
              if (state != State.ACTIVE) {
                  if (logger.isLoggable(Level.FINEST)) {
                      logger.log(Level.FINEST, format("%s exception (most "
                              + "likely caused by local close)", this), ex);
                  }
              } else {
                  error = ex;
                  errorDescription = "Socket error";
              }
          } catch (SSLException ex) {
              error = ex;
              errorDescription = "SSL error";
          } catch (IOException ex) {
              error = ex;
              errorDescription = "I/O error";
          } catch (SecurityException ex) {
              error = ex;
              errorDescription = "Security error";
          } catch (Throwable th) {
              error = th;
          }
  
          if (error != null) {
              if (errorDescription == null) {
                  errorDescription = "Unknown error";
                  logger.log(Level.WARNING, format("%s unexpected error", this),
                             error);
              } else {
                  logger.log(Level.FINEST, format("%s exception", this), error);
              }
              didFail(0, errorDescription);
          }
  
          try {
              socket.close();
          } catch (IOException ignore) {}
          didClose();
  
          logger.log(Level.FINEST, "{0} finished", this);
      }
  
      private void connect() throws IOException {
          SecurityManager securityManager = System.getSecurityManager();
          if (securityManager != null) {
              securityManager.checkConnect(host, port);
          }
  
          // The proxy trial logic here is meant to mimic
          // sun.net.www.protocol.http.HttpURLConnection.plainConnect
          boolean success = false;
          IOException lastException = null;
          boolean triedDirectConnection = false;
          ProxySelector proxySelector = AccessController.doPrivileged(
                  new PrivilegedAction<ProxySelector>() {
                      public ProxySelector run() {
                          return ProxySelector.getDefault();
                      }
                  });
          if (proxySelector != null) {
              URI uri;
              try {
                  uri = new URI((ssl ? "https" : "http") + "://" + host);
              } catch (URISyntaxException ex) {
                  throw new IOException(ex);
              }
              if (logger.isLoggable(Level.FINEST)) {
                  logger.log(Level.FINEST, format("%s selecting proxies "
                          + "for: [%s]", this, uri));
              }
              List<Proxy> proxies = proxySelector.select(uri);
              if (logger.isLoggable(Level.FINEST)) {
                  logger.log(Level.FINEST, format("%s selected proxies: %s",
                          this, proxies));
              }
              for (Proxy proxy : proxies) {
                  if (logger.isLoggable(Level.FINEST)) {
                      logger.log(Level.FINEST, format("%s trying proxy: [%s]",
                              this, proxy));
                  }
                  if (proxy.type() == Proxy.Type.DIRECT) {
                      triedDirectConnection = true;
                  }
                  try {
                      connect(proxy);
                      success = true;
                      break;
                  } catch (IOException ex) {
                      logger.log(Level.FINEST, format("%s exception", this), ex);
                      lastException = ex;
                      if (proxy.address() != null) {
                          proxySelector.connectFailed(uri, proxy.address(), ex);
                      }
                      continue;
                  }
              }
          }
          if (!success && !triedDirectConnection) {
              logger.log(Level.FINEST, "{0} trying direct connection", this);
              connect(Proxy.NO_PROXY);
              success = true;
          }
  