[Demonstration 1]

[Buggy Method]
public static File downloadFile(File parent, String prefix, String suffix, URL url) throws IOException {
    File file = File.createTempFile(prefix, suffix, parent);
    URLConnection conn = null;
    final String protocol = url.getProtocol().toLowerCase();
    try {
        conn = url.openConnection();
        if ("http".equals(protocol) || "https".equals(protocol)) {
            HttpURLConnection http = (HttpURLConnection) conn;
            http.setInstanceFollowRedirects(false);
            http.setRequestMethod(HttpMethod.GET);
        }
        try (InputStream input = conn.getInputStream();
            FileOutputStream fOutput = new FileOutputStream(file)) {
            byte[] b = new byte[8192];
            int r;
            while ((r = input.read(b)) != -1) {
                fOutput.write(b, 0, r);
            }
        }
    } finally {
        if (conn != null && ("http".equals(protocol) || "https".equals(protocol))) {
            ((HttpURLConnection) conn).disconnect();
        }
    }
    return file;
}

[Buggy Hunk]
final String protocol = url.getProtocol().toLowerCase();

[Patch]
final String protocol = url.getProtocol().toLowerCase(Locale.ENGLISH);

[Demonstration 2]

[Buggy Method]
public static void main(String[] args) {
    AppArguments arguments = new AppArguments(args);
    Integer port = Integer.getInteger(arguments.getOrDefault("-p", "5000"));
    SocketServer server = new SocketServer(port);
    try {
        Thread.sleep(5000);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

[Buggy Hunk]
Integer port = Integer.getInteger(arguments.getOrDefault("-p", "5000"));

[Patch]
Integer port = Integer.parseInt(arguments.getOrDefault("-p", "5000"));

[Demonstration 3]

[Buggy Method]
private HostDirectives fetchDirectives(URL url) {
    WebURL robotsTxtUrl = new WebURL();
    String host = getHost(url);
    String port = ((url.getPort() == url.getDefaultPort()) || (url.getPort() == -1)) ? "" : (":" + url.getPort());
    robotsTxtUrl.setURL("http://" + host + port + "/robots.txt");
    HostDirectives directives = null;
    PageFetchResult fetchResult = null;
    try {
        fetchResult = pageFetcher.fetchPage(robotsTxtUrl);
        if (fetchResult.getStatusCode() == HttpStatus.SC_OK) {
            Page page = new Page(robotsTxtUrl);
            fetchResult.fetchContent(page);
            if (Util.hasPlainTextContent(page.getContentType())) {
                String content;
                if (page.getContentCharset() == null) {
                    content = new String(page.getContentData());
                } else {
                    content = new String(page.getContentData(), page.getContentCharset());
                }
                directives = RobotstxtParser.parse(content, config.getUserAgentName());
            } else if (page.getContentType().contains("html")) {
                // TODO This one should be upgraded to remove all html tags
                String content = new String(page.getContentData());
                directives = RobotstxtParser.parse(content, config.getUserAgentName());
            } else {
                logger.warn("Can't read this robots.txt: {}  as it is not written in plain text, contentType: {}", robotsTxtUrl.getURL(), page.getContentType());
            }
        } else {
            logger.debug("Can't read this robots.txt: {}  as it's status code is {}", robotsTxtUrl.getURL(), fetchResult.getStatusCode());
        }
    } catch (SocketException | UnknownHostException | SocketTimeoutException | NoHttpResponseException se) {
        // No logging here, as it just means that robots.txt doesn't exist on this server which is perfectly ok
    } catch (PageBiggerThanMaxSizeException pbtms) {
        logger.error("Error occurred while fetching (robots) url: {}, {}", robotsTxtUrl.getURL(), pbtms.getMessage());
    } catch (Exception e) {
        logger.error("Error occurred while fetching (robots) url: " + robotsTxtUrl.getURL(), e);
    } finally {
        if (fetchResult != null) {
            fetchResult.discardContentIfNotConsumed();
        }
    }
    if (directives == null) {
        // We still need to have this object to keep track of the time we fetched it
        directives = new HostDirectives();
    }
    synchronized (host2directivesCache) {
        if (host2directivesCache.size() == config.getCacheSize()) {
            String minHost = null;
            long minAccessTime = Long.MAX_VALUE;
            for (Map.Entry<String, HostDirectives> entry : host2directivesCache.entrySet()) {
                if (entry.getValue().getLastAccessTime() < minAccessTime) {
                    minAccessTime = entry.getValue().getLastAccessTime();
                    minHost = entry.getKey();
                }
            }
            host2directivesCache.remove(minHost);
        }
        host2directivesCache.put(host, directives);
    }
    return directives;
}

[Buggy Hunk]
robotsTxtUrl.setURL("http://" + host + port + "/robots.txt");

[Patch]
robotsTxtUrl.setURL(url.getProtocol() +"://" + host + port + "/robots.txt");