<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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 com.intellij.util.io;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author nik
 */
public class UrlConnectionUtil {
  private UrlConnectionUtil() {
  }

  public static
  @Nullable
  InputStream getConnectionInputStream(URLConnection connection, ProgressIndicator pi) {
    try {
      return getConnectionInputStreamWithException(connection, pi);
    }
    catch (ProcessCanceledException e) {
      return null;
    }
    catch (IOException e) {
      return null;
    }
  }


  public static InputStream getConnectionInputStreamWithException(@NotNull URLConnection connection, @NotNull ProgressIndicator pi)
    throws IOException {
    InputStreamGetter getter = new InputStreamGetter(connection);
    final Future<?> getterFuture = ApplicationManager.getApplication().executeOnPooledThread(getter);
    while (true) {
      pi.checkCanceled();
      try {
        try {
          getterFuture.get(50, TimeUnit.MILLISECONDS);
        }
        catch (TimeoutException ignored) {
        }

        pi.setIndeterminate(true);
        pi.setText(pi.getText());
        if (getterFuture.isDone()) {
          break;
        }
      }
      catch (Exception e) {
        throw new ProcessCanceledException(e);
      }
    }
    //noinspection ThrowableResultOfMethodCallIgnored
    if (getter.getException() != null) {
      throw getter.getException();<commit_msg>nullify: to run on pooled thread without progress<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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 com.intellij.util.io;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author nik
 */
public class UrlConnectionUtil {
  private UrlConnectionUtil() {
  }

  public static
  @Nullable
  InputStream getConnectionInputStream(URLConnection connection, ProgressIndicator pi) {
    try {
      return getConnectionInputStreamWithException(connection, pi);
    }
    catch (ProcessCanceledException e) {
      return null;
    }
    catch (IOException e) {
      return null;
    }
  }


  public static InputStream getConnectionInputStreamWithException(@NotNull URLConnection connection, @Nullable ProgressIndicator pi)
    throws IOException {
    InputStreamGetter getter = new InputStreamGetter(connection);
    final Future<?> getterFuture = ApplicationManager.getApplication().executeOnPooledThread(getter);
    while (true) {
      if (pi != null) {
        pi.checkCanceled();
      }

      try {
        try {
          getterFuture.get(50, TimeUnit.MILLISECONDS);
        }
        catch (TimeoutException ignored) {
        }

        if (pi != null) {
          pi.setIndeterminate(true);
          pi.setText(pi.getText());
        }

        if (getterFuture.isDone()) {
          break;
        }
      }
      catch (Exception e) {
        throw new ProcessCanceledException(e);
      }
    }
    //noinspection ThrowableResultOfMethodCallIgnored
    if (getter.getException() != null) {
      throw getter.getException();
{'fraction_non_alphanumeric': 0.08841893252769385, 'fraction_numerical': 0.005639476334340383, 'mean_word_length': 3.4339285714285714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937530', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1337, 'n_words': 378}