<commit_before>
package com.github.snowdream.android.app;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.webkit.URLUtil;
import com.github.snowdream.android.app.dao.ISql;
import com.github.snowdream.android.app.dao.ISqlImpl;
import com.github.snowdream.android.util.Log;
import com.github.snowdream.android.util.concurrent.AsyncTask;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.sql.SQLException;

public class AsycDownloadTask extends AsyncTask<DownloadTask, Integer, DownloadTask> {
    private static final InternalHandler sHandler = new InternalHandler();
    private static final int MESSAGE_POST_ERROR = 0x1;

    /**
     * http default
     */
    private static final int MODE_DEFAULT = 0x1;
    /**
     * http trunked
     */
    private static final int MODE_TRUNKED = 0x2;

    private int mode = MODE_DEFAULT;

    public AsycDownloadTask(DownloadListener<Integer, DownloadTask> listener) {
        super(listener);
    }

    /**
     * TODO if error occurs,carry it out. if (listener != null) {
     * listener.onError(new Throwable()); }
     */
    protected DownloadTask doInBackground(DownloadTask... tasks) {
        if (tasks.length <= 0) {
            Log.e("There is no DownloadTask.");
            return null;
        }

        DownloadTask task = tasks[0];

        if (task == null || task.getUrl() == null || !URLUtil.isValidUrl(task.getUrl())) {
            SendError(task, DownloadException.DOWNLOAD_TASK_NOT_VALID);

            Log.e("The task is not valid,or the url of the task is not valid.");
            return null;
        }

        String path = task.getPath();
        File file = new File(path);

        InputStream in = null;
        RandomAccessFile out = null;
        HttpURLConnection connection = null;

        try {
            long range = file.length();
            long size = task.getSize();
            long curSize = range;
            String contentType = task.getMimeType();

            File dir = file.getParentFile();

            if (!dir.exists() && !dir.mkdirs()) {
                SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);
                Log.e("The directory of the file can not be created!");
                return null;
            }

            if (!file.exists() && !file.createNewFile() && !file.canWrite()) {
                SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);
                Log.e("The file can not be created!");
                return null;
            }

            if (task.getStatus() == DownloadStatus.STATUS_FINISHED && size == range) {
                Log.i("The DownloadTask has already been downloaded.");
                return task;
            }

            task.setStatus(DownloadStatus.STATUS_RUNNING);


            String urlString = task.getUrl();
            String cookies = null;
            while (true) {
                URL url = new URL(urlString);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestProperty("User-Agent", "Snowdream Mobile");
                connection.setRequestProperty("Connection", "Keep-Alive");
                if (cookies != null && cookies != "") {
                    connection.setRequestProperty("Cookie", cookies);
                }
                connection.setRequestMethod("GET");

                if (range > 0) {
                    connection.setRequestProperty("Range", "bytes=" + range +
                            "-");
                }

                //http auto redirection
                //see: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
                boolean redirect = false;
                boolean success = false;

                // normally, 3xx is redirect
                int status = connection.getResponseCode();
                Log.i("HTTP STATUS CODE: " + status);

                switch (status) {
                    case HttpURLConnection.HTTP_OK:
                    case HttpURLConnection.HTTP_PARTIAL:
                        success = true;

                        String transfer_encoding = connection.getHeaderField("Transfer-Encoding");
                        if (!TextUtils.isEmpty(transfer_encoding)
                                && transfer_encoding.equalsIgnoreCase("chunked")) {
                            mode = MODE_TRUNKED;
                            Log.i("HTTP MODE: TRUNKED");
                        } else {
                            mode = MODE_DEFAULT;
                            Log.i("HTTP MODE: DEFAULT");
                        }

                        String accept_ranges = connection.getHeaderField("Accept-Ranges");
                        if (!TextUtils.isEmpty(accept_ranges)
                                && accept_ranges.equalsIgnoreCase("bytes")) {
                            range = 0;
                            Log.i("Accept-Ranges: bytes");
                        }else{
                            Log.i("Accept-Ranges: none");
                        }
                        break;
                    case HttpURLConnection.HTTP_MOVED_TEMP:
                    case HttpURLConnection.HTTP_MOVED_PERM:
                    case HttpURLConnection.HTTP_SEE_OTHER:
                        redirect = true;
                        // get redirect url from "location" header field
                        urlString = connection.getHeaderField("Location");

                        // get the cookie if need, for login
                        cookies = connection.getHeaderField("Set-Cookie");

                        Log.i("Redirect Url : " + urlString);
                        break;
                    default:
                        success = false;
                        break;
                }

                if (!redirect) {
                    if (!success) {
                        SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);<commit_msg>[Fixed] set range to 0 when the Accept-Ranges is none,but not bytes<commit_after>
package com.github.snowdream.android.app;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.webkit.URLUtil;
import com.github.snowdream.android.app.dao.ISql;
import com.github.snowdream.android.app.dao.ISqlImpl;
import com.github.snowdream.android.util.Log;
import com.github.snowdream.android.util.concurrent.AsyncTask;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.sql.SQLException;

public class AsycDownloadTask extends AsyncTask<DownloadTask, Integer, DownloadTask> {
    private static final InternalHandler sHandler = new InternalHandler();
    private static final int MESSAGE_POST_ERROR = 0x1;

    /**
     * http default
     */
    private static final int MODE_DEFAULT = 0x1;
    /**
     * http trunked
     */
    private static final int MODE_TRUNKED = 0x2;

    private int mode = MODE_DEFAULT;

    public AsycDownloadTask(DownloadListener<Integer, DownloadTask> listener) {
        super(listener);
    }

    /**
     * TODO if error occurs,carry it out. if (listener != null) {
     * listener.onError(new Throwable()); }
     */
    protected DownloadTask doInBackground(DownloadTask... tasks) {
        if (tasks.length <= 0) {
            Log.e("There is no DownloadTask.");
            return null;
        }

        DownloadTask task = tasks[0];

        if (task == null || task.getUrl() == null || !URLUtil.isValidUrl(task.getUrl())) {
            SendError(task, DownloadException.DOWNLOAD_TASK_NOT_VALID);

            Log.e("The task is not valid,or the url of the task is not valid.");
            return null;
        }

        String path = task.getPath();
        File file = new File(path);

        InputStream in = null;
        RandomAccessFile out = null;
        HttpURLConnection connection = null;

        try {
            long range = file.length();
            long size = task.getSize();
            long curSize = range;
            String contentType = task.getMimeType();

            File dir = file.getParentFile();

            if (!dir.exists() && !dir.mkdirs()) {
                SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);
                Log.e("The directory of the file can not be created!");
                return null;
            }

            if (!file.exists() && !file.createNewFile() && !file.canWrite()) {
                SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);
                Log.e("The file can not be created!");
                return null;
            }

            if (task.getStatus() == DownloadStatus.STATUS_FINISHED && size == range) {
                Log.i("The DownloadTask has already been downloaded.");
                return task;
            }

            task.setStatus(DownloadStatus.STATUS_RUNNING);


            String urlString = task.getUrl();
            String cookies = null;
            while (true) {
                URL url = new URL(urlString);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestProperty("User-Agent", "Snowdream Mobile");
                connection.setRequestProperty("Connection", "Keep-Alive");
                if (cookies != null && cookies != "") {
                    connection.setRequestProperty("Cookie", cookies);
                }
                connection.setRequestMethod("GET");

                if (range > 0) {
                    connection.setRequestProperty("Range", "bytes=" + range +
                            "-");
                }

                //http auto redirection
                //see: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
                boolean redirect = false;
                boolean success = false;

                // normally, 3xx is redirect
                int status = connection.getResponseCode();
                Log.i("HTTP STATUS CODE: " + status);

                switch (status) {
                    case HttpURLConnection.HTTP_OK:
                    case HttpURLConnection.HTTP_PARTIAL:
                        success = true;

                        String transfer_encoding = connection.getHeaderField("Transfer-Encoding");
                        if (!TextUtils.isEmpty(transfer_encoding)
                                && transfer_encoding.equalsIgnoreCase("chunked")) {
                            mode = MODE_TRUNKED;
                            Log.i("HTTP MODE: TRUNKED");
                        } else {
                            mode = MODE_DEFAULT;
                            Log.i("HTTP MODE: DEFAULT");
                        }

                        String accept_ranges = connection.getHeaderField("Accept-Ranges");
                        if (!TextUtils.isEmpty(accept_ranges)
                                && accept_ranges.equalsIgnoreCase("bytes")) {
                            Log.i("Accept-Ranges: bytes");
                        }else{
                            range = 0;
                            Log.i("Accept-Ranges: none");
                        }
                        break;
                    case HttpURLConnection.HTTP_MOVED_TEMP:
                    case HttpURLConnection.HTTP_MOVED_PERM:
                    case HttpURLConnection.HTTP_SEE_OTHER:
                        redirect = true;
                        // get redirect url from "location" header field
                        urlString = connection.getHeaderField("Location");

                        // get the cookie if need, for login
                        cookies = connection.getHeaderField("Set-Cookie");

                        Log.i("Redirect Url : " + urlString);
                        break;
                    default:
                        success = false;
                        break;
                }

                if (!redirect) {
                    if (!success) {
                        SendError(task, DownloadException.DOWNLOAD_TASK_FAILED);
{'fraction_non_alphanumeric': 0.09642276422764227, 'fraction_numerical': 0.0018699186991869919, 'mean_word_length': 1.5431052305147819, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7061165', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2913, 'n_words': 686}