Java 类com.intellij.util.io.UrlConnectionUtil 实例源码

项目:tools-idea    文件:AbstractExternalFilter.java   
@Nullable
private static Reader getReaderByUrl(final String surl, final HttpConfigurable httpConfigurable, final ProgressIndicator pi)
  throws IOException
{
  if (surl.startsWith(JAR_PROTOCOL)) {
    VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(BrowserUtil.getDocURL(surl));

    if (file == null) {
      return null;
    }

    return new StringReader(VfsUtilCore.loadText(file));
  }

  URL url = BrowserUtil.getURL(surl);
  if (url == null) {
    return null;
  }
  final URLConnection urlConnection = httpConfigurable.openConnection(url.toString());
  final String contentEncoding = guessEncoding(url);
  final InputStream inputStream =
    pi != null ? UrlConnectionUtil.getConnectionInputStreamWithException(urlConnection, pi) : urlConnection.getInputStream();
  //noinspection IOResourceOpenedButNotSafelyClosed
  return contentEncoding != null ? new MyReader(inputStream, contentEncoding) : new MyReader(inputStream);
}
项目:consulo-lua    文件:UrlUtil.java   
@Nullable
public static Reader getReaderByUrl(final String surl, final HttpConfigurable httpConfigurable, final ProgressIndicator pi) throws IOException {
    if (surl.startsWith(JAR_PROTOCOL)) {
        VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(BrowserUtil.getDocURL(surl));

        if (file == null) {
            return null;
        }

        return new StringReader(VfsUtil.loadText(file));
    }

    URL url = BrowserUtil.getURL(surl);
    if (url == null) {
        return null;
    }
    httpConfigurable.prepareURL(url.toString());
    final URLConnection urlConnection = url.openConnection();
    final String contentEncoding = urlConnection.getContentEncoding();
    final InputStream inputStream =
            pi != null ? UrlConnectionUtil.getConnectionInputStreamWithException(urlConnection, pi) : urlConnection.getInputStream();
    //noinspection IOResourceOpenedButNotSafelyClosed
    return contentEncoding != null ? new InputStreamReader(inputStream, contentEncoding) : new InputStreamReader(inputStream);
}
项目:tools-idea    文件:UpdateChecker.java   
private static void doDownloadAndInstallPatch(BuildInfo newVersion, ProgressIndicator i) throws IOException {
  PatchInfo patch = newVersion.findPatchForCurrentBuild();
  if (patch == null) throw new IOException("No patch is available for current version");

  String productCode = ApplicationInfo.getInstance().getBuild().getProductCode();

  String osSuffix = "-" + patch.getOSSuffix();

  String fromBuildNumber = patch.getFromBuild().asStringWithoutProductCode();
  String toBuildNumber = newVersion.getNumber().asStringWithoutProductCode();
  String fileName = productCode + "-" + fromBuildNumber + "-" + toBuildNumber + "-patch" + osSuffix + ".jar";

  String platform = PlatformUtils.getPlatformPrefix();

  File tempFile = FileUtil.createTempFile(platform, "patch", true);

  OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
  try {
    URL requestUrl = new URL(new URL(getPatchesUrl()), fileName);
    URLConnection connection;

    int followCount = 2;
    while(true) {
      connection = requestUrl.openConnection();

      if (connection instanceof HttpURLConnection) {
        HttpURLConnection hcnx = (HttpURLConnection)connection;
        int code = hcnx.getResponseCode();
        if (code >= 301 && code <= 307 && --followCount >= 0) {
          String loc = hcnx.getHeaderField(HttpHeaders.LOCATION);
          if (loc != null) {
            requestUrl = new URL(loc);
            continue;
          }
        }
      }
      break;
    }

    try {
      InputStream in = UrlConnectionUtil.getConnectionInputStreamWithException(connection, i);
      try {
        int total = connection.getContentLength();
        i.setIndeterminate(total > 0);

        byte[] buffer = new byte[10 * 1024];
        int count;
        int read = 0;

        while ((count = in.read(buffer)) > 0) {
          i.checkCanceled();
          out.write(buffer, 0, count);
          read += count;
          if (total > 0) {
            i.setFraction(((double)read) / total);
            i.setText2((read / 1024) + "/" + (total / 1024) + " KB");
          }
          else {
            i.setText2((read / 1024) + " KB");
          }
        }
      }
      finally {
        in.close();
      }
    }
    finally {
      if (connection instanceof HttpURLConnection) {
        ((HttpURLConnection)connection).disconnect();
      }
    }
  }
  finally {
    out.close();
  }

  String patchFileName = ("jetbrains.patch.jar." + platform).toLowerCase();
  File patchFile = new File(FileUtil.getTempDirectory(), patchFileName);
  FileUtil.copy(tempFile, patchFile);
  FileUtil.delete(tempFile);
}
项目:tools-idea    文件:PluginDownloader.java   
private File downloadPlugin(final ProgressIndicator pi) throws IOException {
  final File pluginsTemp = new File(PathManager.getPluginTempPath());
  if (!pluginsTemp.exists() && !pluginsTemp.mkdirs()) {
    throw new IOException(IdeBundle.message("error.cannot.create.temp.dir", pluginsTemp));
  }
  final File file = FileUtil.createTempFile(pluginsTemp, "plugin_", "_download", true, false);

  pi.setText(IdeBundle.message("progress.connecting"));

  URLConnection connection = null;
  try {
    connection = openConnection(myPluginUrl);

    final InputStream is = UrlConnectionUtil.getConnectionInputStream(connection, pi);
    if (is == null) {
      throw new IOException("Failed to open connection");
    }

    pi.setText(IdeBundle.message("progress.downloading.plugin", getPluginName()));
    final int contentLength = connection.getContentLength();
    pi.setIndeterminate(contentLength == -1);

    try {
      final OutputStream fos = new BufferedOutputStream(new FileOutputStream(file, false));
      try {
        NetUtils.copyStreamContent(pi, is, fos, contentLength);
      }
      finally {
        fos.close();
      }
    }
    finally {
      is.close();
    }

    if (myFileName == null) {
      myFileName = guessFileName(connection, file);
    }

    final File newFile = new File(file.getParentFile(), myFileName);
    FileUtil.rename(file, newFile);
    return newFile;
  }
  finally {
    if (connection instanceof HttpURLConnection) {
      ((HttpURLConnection)connection).disconnect();
    }
  }
}