<commit_before>package de.danoeh.antennapod.service.download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.net.http.AndroidHttpClient;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.StorageUtils;

public class HttpDownloader extends Downloader {
	private static final String TAG = "HttpDownloader";

	private static final int MAX_REDIRECTS = 5;

	private static final int BUFFER_SIZE = 8 * 1024;
	private static final int CONNECTION_TIMEOUT = 5000;

	public HttpDownloader(DownloaderCallback downloaderCallback,
			DownloadStatus status) {
		super(downloaderCallback, status);
	}

	private AndroidHttpClient createHttpClient() {
		AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
		HttpParams params = httpClient.getParams();
		params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS);
		params.setBooleanParameter("http.protocol.reject-relative-redirect",
				false);
		params.setIntParameter("http.socket.timeout", CONNECTION_TIMEOUT);
		HttpClientParams.setRedirecting(params, true);
		return httpClient;
	}

	/**
	 * This method is called by establishConnection(String). Don't call it
	 * directly.
	 * */
	private HttpURLConnection establishConnection(String location,
			int redirectCount) throws MalformedURLException, IOException {
		URL url = new URL(location);
		HttpURLConnection connection = null;
		int responseCode = -1;
		connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(CONNECTION_TIMEOUT);
		// try with 'follow redirect'
		connection.setInstanceFollowRedirects(true);
		try {
			responseCode = connection.getResponseCode();
		} catch (IOException e) {
			e.printStackTrace();
			if (AppConfig.DEBUG)
				Log.d(TAG,
						"Failed to establish connection with 'follow redirects. Disabling 'follow redirects'");
			connection.disconnect();
			connection.setInstanceFollowRedirects(false);
			responseCode = connection.getResponseCode();
		}
		if (AppConfig.DEBUG)
			Log.d(TAG, "Response Code: " + responseCode);
		switch (responseCode) {
		case HttpStatus.SC_TEMPORARY_REDIRECT:
			if (redirectCount < MAX_REDIRECTS) {
				final String redirect = connection.getHeaderField("Location");
				if (redirect != null) {
					return establishConnection(redirect, redirectCount + 1);
				}
			}
		case HttpStatus.SC_OK:
			return connection;
		default:
			onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
					String.valueOf(responseCode));
			return null;
		}
	}

	/**
	 * Establish connection to resource. This method will also try to handle
	 * different response codes / redirect issues.
	 * 
	 * @return the HttpURLConnection object if the connection could be opened,
	 *         null otherwise.
	 * @throws MalformedURLException
	 *             , IOException
	 * */
	private HttpURLConnection establishConnection(String location)
			throws MalformedURLException, IOException {
		return establishConnection(location, 0);
	}<commit_msg>removed unused code<commit_after>package de.danoeh.antennapod.service.download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.params.HttpParams;

import android.net.http.AndroidHttpClient;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.StorageUtils;

public class HttpDownloader extends Downloader {
	private static final String TAG = "HttpDownloader";

	private static final int MAX_REDIRECTS = 5;

	private static final int BUFFER_SIZE = 8 * 1024;
	private static final int CONNECTION_TIMEOUT = 5000;

	public HttpDownloader(DownloaderCallback downloaderCallback,
			DownloadStatus status) {
		super(downloaderCallback, status);
	}

	private AndroidHttpClient createHttpClient() {
		AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
		HttpParams params = httpClient.getParams();
		params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS);
		params.setBooleanParameter("http.protocol.reject-relative-redirect",
				false);
		params.setIntParameter("http.socket.timeout", CONNECTION_TIMEOUT);
		HttpClientParams.setRedirecting(params, true);
		return httpClient;
	}
{'fraction_non_alphanumeric': 0.09537671232876713, 'fraction_numerical': 0.003938356164383562, 'mean_word_length': 5.4541436464088395, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5215933', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1699, 'n_words': 366}