<commit_before>package com.amazonaws.http;

import com.amazonaws.AmazonClientException;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class AmazonHttpRequestToGoogleHttpRequestAdaptor {

    /**
     * Creates an HttpClient method object based on the specified request and
     * populates any parameters, headers, etc. from the original request.
     *
     * @param amazonRequest
     *            The request to convert to an HttpClient method object.
     *
     * @return The converted HttpClient method object with any parameters,
     *         headers, etc. from the original request set.
     */
    public HTTPRequest convert(HttpRequest amazonRequest) throws MalformedURLException {
        URI endpoint = amazonRequest.getEndpoint();
        String uri = endpoint.toString();
        String path = amazonRequest.getResourcePath();
        if (path != null && path.length() > 0) {
            if (path.startsWith("/")) {
                if (uri.endsWith("/")) {
                    uri += path.substring(1);
                } else {
                    uri += path;
                }
            } else {
                if (uri.endsWith("/")) {
                    uri += path;
                } else {
                    uri += "/" + path;
                }
            }
        } else if (!uri.endsWith("/")) {
            uri += "/";
        };

        NameValuePair[] nameValuePairs = null;
        if (amazonRequest.getParameters().size() > 0) {
            nameValuePairs = new NameValuePair[amazonRequest.getParameters().size()];
            int i = 0;
            for (Map.Entry<String, String> entry : amazonRequest.getParameters().entrySet()) {
                nameValuePairs[i++] = new NameValuePair(entry.getKey(), entry.getValue());
            }
        }

        HTTPRequest method;
        if (amazonRequest.getMethodName() == HttpMethodName.POST) {
            /*
             * If there isn't any payload content to include in this request,
             * then try to include the POST parameters in the query body,
             * otherwise, just use the query string. For all AWS Query services,
             * the best behavior is putting the params in the request body for
             * POST requests, but we can't do that for S3.
             */
            if (nameValuePairs != null) uri += toQueryString(nameValuePairs);

            method = new HTTPRequest(new URL(uri), HTTPMethod.POST);
            if (amazonRequest.getContent() != null) {
                method.setPayload(toByteArray(amazonRequest.getContent()));
            }
        } else if (amazonRequest.getMethodName() == HttpMethodName.GET) {
            if (nameValuePairs != null) uri += toQueryString(nameValuePairs);
            method = new HTTPRequest(new URL(uri), HTTPMethod.GET);
        } else if (amazonRequest.getMethodName() == HttpMethodName.PUT) {
            if (nameValuePairs != null) uri += toQueryString(nameValuePairs);
            method = new HTTPRequest(new URL(uri), HTTPMethod.PUT);

            /*
             * URLFetchService doesn't explicitly support 100-continue behaviour, so remove code catering for it
             */

            if (amazonRequest.getContent() != null) {
                method.setPayload(toByteArray(amazonRequest.getContent()));
            }
        } else if (amazonRequest.getMethodName() == HttpMethodName.DELETE) {
            if (nameValuePairs != null) uri += toQueryString(nameValuePairs);
            method = new HTTPRequest(new URL(uri), HTTPMethod.DELETE);
        } else if (amazonRequest.getMethodName() == HttpMethodName.HEAD) {
            if (nameValuePairs != null) uri += toQueryString(nameValuePairs);
            method = new HTTPRequest(new URL(uri), HTTPMethod.HEAD);
        } else {
            throw new AmazonClientException("Unknown HTTP method name: " + amazonRequest.getMethodName());
        }

        // No matter what type of HTTP method we're creating, we need to copy
        // all the headers from the request.
        for (Map.Entry<String, String> entry : amazonRequest.getHeaders().entrySet()) {
            method.addHeader(new HTTPHeader(entry.getKey(), entry.getValue()));
        }

        return method;
    }

    static class NameValuePair {
        private String name;
        private String value;

        public NameValuePair(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }
    }

    public static String toQueryString(NameValuePair[] nameValuePairs) {
        StringBuilder queryString = new StringBuilder("?");
        for (NameValuePair nameValuePair : nameValuePairs) {
            try {
                queryString.append(URLEncoder.encode(nameValuePair.getName(), "UTF-8"));
                queryString.append("=");
                queryString.append(URLEncoder.encode(nameValuePair.getValue(), "UTF-8"));
                queryString.append("&");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return queryString.substring(0, queryString.length() - 1);
    }

    public static byte[] toByteArray(InputStream inputStream) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n;
        try {
            while (-1 != (n = inputStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return output.toByteArray();
    }

}<commit_msg>Invert all negatives in concatenation logic and remove unnecessary inner class<commit_after>package com.amazonaws.http;

import com.amazonaws.AmazonClientException;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class AmazonHttpRequestToGoogleHttpRequestAdaptor {

    /**
     * Creates an HttpClient method object based on the specified request and
     * populates any parameters, headers, etc. from the original request.
     *
     * @param amazonRequest
     *            The request to convert to an HttpClient method object.
     *
     * @return The converted HttpClient method object with any parameters,
     *         headers, etc. from the original request set.
     */
    public HTTPRequest convert(HttpRequest amazonRequest) throws MalformedURLException {
        URI endpoint = amazonRequest.getEndpoint();
        String uri = concatenateEnsuringCorrectNumberOfSlashes(endpoint.toString(), amazonRequest.getResourcePath());

        HTTPRequest method;
        if (amazonRequest.getMethodName() == HttpMethodName.POST) {
            /*
             * If there isn't any payload content to include in this request,
             * then try to include the POST parameters in the query body,
             * otherwise, just use the query string. For all AWS Query services,
             * the best behavior is putting the params in the request body for
             * POST requests, but we can't do that for S3.
             */
            method = new HTTPRequest(new URL(uri + toQueryString(amazonRequest.getParameters())), HTTPMethod.POST);
            if (amazonRequest.getContent() != null) {
                method.setPayload(toByteArray(amazonRequest.getContent()));
            }
        } else if (amazonRequest.getMethodName() == HttpMethodName.GET) {
            method = new HTTPRequest(new URL(uri + toQueryString(amazonRequest.getParameters())), HTTPMethod.GET);
        } else if (amazonRequest.getMethodName() == HttpMethodName.PUT) {
            method = new HTTPRequest(new URL(uri + toQueryString(amazonRequest.getParameters())), HTTPMethod.PUT);

            /*
             * URLFetchService doesn't explicitly support 100-continue behaviour, so remove code catering for it
             */

            if (amazonRequest.getContent() != null) {
                method.setPayload(toByteArray(amazonRequest.getContent()));
            }
        } else if (amazonRequest.getMethodName() == HttpMethodName.DELETE) {
            method = new HTTPRequest(new URL(uri + toQueryString(amazonRequest.getParameters())), HTTPMethod.DELETE);
        } else if (amazonRequest.getMethodName() == HttpMethodName.HEAD) {
            method = new HTTPRequest(new URL(uri + toQueryString(amazonRequest.getParameters())), HTTPMethod.HEAD);
        } else {
            throw new AmazonClientException("Unknown HTTP method name: " + amazonRequest.getMethodName());
        }

        // No matter what type of HTTP method we're creating, we need to copy
        // all the headers from the request.
        for (Map.Entry<String, String> entry : amazonRequest.getHeaders().entrySet()) {
            method.addHeader(new HTTPHeader(entry.getKey(), entry.getValue()));
        }

        return method;
    }

    private static String concatenateEnsuringCorrectNumberOfSlashes(String endpoint, String path) {
        if (path == null || path.isEmpty()) {
            if (endpoint.endsWith("/")) {
                return endpoint;
            } else {
                return endpoint + "/";
            }
        } else {
            if (path.startsWith("/")) {
                if (endpoint.endsWith("/")) {
                    return endpoint + path.substring(1);
                } else {
                    return endpoint + path;
                }
            } else {
                if (endpoint.endsWith("/")) {
                    return endpoint + path;
                } else {
                    return endpoint + "/" + path;
                }
            }
        }
    }

    public static String toQueryString(Map<String, String> parameters) {
        if (parameters == null || parameters.isEmpty()) return "";

        StringBuilder queryString = new StringBuilder("?");
        for (Map.Entry<String,String> entry : parameters.entrySet()) {
            try {
                queryString.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                queryString.append("=");
                queryString.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                queryString.append("&");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return queryString.substring(0, queryString.length() - 1);
    }

    public static byte[] toByteArray(InputStream inputStream) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n;
        try {
            while (-1 != (n = inputStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return output.toByteArray();
    }

}
{'fraction_non_alphanumeric': 0.10294117647058823, 'fraction_numerical': 0.0028214774281805745, 'mean_word_length': 2.1673436230706744, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6723923', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2910, 'n_words': 781}