<commit_before>package com.novoda.downloadmanager;

import com.novoda.notils.logger.simple.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

class NetworkFileDownloader implements FileDownloader {

    private static final int BUFFER_SIZE = 8 * 512;

    private final HttpClient httpClient;
    private final NetworkRequestCreator requestCreator;

    private boolean canDownload;

    NetworkFileDownloader(HttpClient httpClient, NetworkRequestCreator requestCreator) {
        this.httpClient = httpClient;
        this.requestCreator = requestCreator;
    }

    @Override
    public void startDownloading(String url, FileSize fileSize, Callback callback) {
        canDownload = true;

        NetworkRequest request = createRequestFrom(url, fileSize);
        HttpClient.NetworkResponse response = null;
        try {
            response = httpClient.execute(request);
            int responseCode = response.code();
            processResponse(callback, response, responseCode);
        } catch (IOException e) {
            Log.e(e, "Exception with http request");
            callback.onError();
        } finally {
            try {
                if (response != null) {
                    response.closeByteStream();
                }
            } catch (IOException e) {
                Log.e(e, "Exception while closing the body response");
            }
        }

        callback.onDownloadFinished();
    }

    private void processResponse(Callback callback, HttpClient.NetworkResponse response, int responseCode) throws IOException {
        if (isValid(responseCode)) {
            InputStream in = response.openByteStream();
            byte[] buffer = new byte[BUFFER_SIZE];
            int readLast = 0;
            while (canDownload && readLast != -1) {
                readLast = in.read(buffer);

                if (readLast != 0 && readLast != -1) {
                    callback.onBytesRead(buffer, readLast);
                }
            }
        } else {
            Log.e("Network response code is not ok, responseCode: " + responseCode);
            callback.onError();
        }
    }

    private boolean isValid(int responseCode) {
        return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL;
    }

    private NetworkRequest createRequestFrom(String url, FileSize fileSize) {
        if (fileSize.areBytesDownloadedKnown()) {
            return requestCreator.createDownloadRequestWithDownloadedBytesHeader(url, fileSize.currentSize(), fileSize.totalSize());
        } else {
            return requestCreator.createDownloadRequest(url);
        }
    }

    @Override
    public void stopDownloading() {
        canDownload = false;
    }
}
<commit_msg>Ensure InputStream is closed when processing the network response<commit_after>package com.novoda.downloadmanager;

import com.novoda.notils.logger.simple.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

class NetworkFileDownloader implements FileDownloader {

    private static final int BUFFER_SIZE = 8 * 512;

    private final HttpClient httpClient;
    private final NetworkRequestCreator requestCreator;

    private boolean canDownload;

    NetworkFileDownloader(HttpClient httpClient, NetworkRequestCreator requestCreator) {
        this.httpClient = httpClient;
        this.requestCreator = requestCreator;
    }

    @Override
    public void startDownloading(String url, FileSize fileSize, Callback callback) {
        canDownload = true;

        NetworkRequest request = createRequestFrom(url, fileSize);
        HttpClient.NetworkResponse response = null;
        try {
            response = httpClient.execute(request);
            int responseCode = response.code();
            processResponse(callback, response, responseCode);
        } catch (IOException e) {
            Log.e(e, "Exception with http request");
            callback.onError();
        } finally {
            try {
                if (response != null) {
                    response.closeByteStream();
                }
            } catch (IOException e) {
                Log.e(e, "Exception while closing the body response");
            }
        }

        callback.onDownloadFinished();
    }

    private void processResponse(Callback callback, HttpClient.NetworkResponse response, int responseCode) throws IOException {
        if (isValid(responseCode)) {
            byte[] buffer = new byte[BUFFER_SIZE];
            int readLast = 0;
            try (InputStream in = response.openByteStream()) {
                while (canDownload && readLast != -1) {
                    readLast = in.read(buffer);

                    if (readLast != 0 && readLast != -1) {
                        callback.onBytesRead(buffer, readLast);
                    }
                }
            }
        } else {
            Log.e("Network response code is not ok, responseCode: " + responseCode);
            callback.onError();
        }
    }

    private boolean isValid(int responseCode) {
        return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL;
    }

    private NetworkRequest createRequestFrom(String url, FileSize fileSize) {
        if (fileSize.areBytesDownloadedKnown()) {
            return requestCreator.createDownloadRequestWithDownloadedBytesHeader(url, fileSize.currentSize(), fileSize.totalSize());
        } else {
            return requestCreator.createDownloadRequest(url);
        }
    }

    @Override
    public void stopDownloading() {
        canDownload = false;
    }
}

{'fraction_non_alphanumeric': 0.08460861917326297, 'fraction_numerical': 0.002814423922603342, 'mean_word_length': 2.290509259259259, 'pattern_counts': {'":': 0, '<': 3, '<?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': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567802', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1373, 'n_words': 345}