semeru/Code-Code-CloneDetection-BigCloneBench huggingface dataset:
public Collection<Module> loadModules() {
        URL url = getClass().getResource("/modules.xml");
        if (url == null) {
            java.util.logging.Logger.getLogger(ModuleLoader.class.getName()).log(java.util.logging.Level.SEVERE, "Cannot find modules.xml file in classpath");
            return Collections.<Module>emptyList();
        }
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        Document document = null;
        InputStream input = null;
        try {
            input = url.openStream();
            DocumentBuilder bui = fac.newDocumentBuilder();
            document = bui.parse(url.openStream());
        } catch (SAXException ex) {
            Logger.getLogger(ModuleLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ModuleLoader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ModuleLoader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException ex) {
                    Logger.getLogger(ModuleLoader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (document == null) {
            return Collections.<Module>emptyList();
        }
        List<Module> modules = new LinkedList<Module>();
        NodeList moduleListNodes = document.getElementsByTagName("module-list");
        for (int i = 0; i < moduleListNodes.getLength(); i++) {
            Element moduleListNode = (Element) moduleListNodes.item(i);
            NodeList moduleNodes = moduleListNode.getElementsByTagName("module");
            for (int j = 0; j < moduleNodes.getLength(); j++) {
                Element moduleNode = (Element) moduleNodes.item(j);
                String moduleClass = moduleNode.getAttribute("class");
                if (moduleClass != null) {
                    instantiateModule(moduleClass, modules);
                }
            }
        }
        return modules;
    }
public static void proxyRequest(IPageContext context, Writer writer, String proxyPath) throws IOException {
        URLConnection connection = new URL(proxyPath).openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(false);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
        connection.setReadTimeout(30000);
        connection.setConnectTimeout(5000);
        Enumeration<String> e = context.httpRequest().getHeaderNames();
        while (e.hasMoreElements()) {
            String name = e.nextElement();
            if (name.equalsIgnoreCase("HOST") || name.equalsIgnoreCase("Accept-Encoding") || name.equalsIgnoreCase("Authorization")) continue;
            Enumeration<String> headers = context.httpRequest().getHeaders(name);
            while (headers.hasMoreElements()) {
                String header = headers.nextElement();
                connection.setRequestProperty(name, header);
            }
        }
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod(context.httpRequest().getMethod());
            if ("POST".equalsIgnoreCase(context.httpRequest().getMethod()) || "PUT".equalsIgnoreCase(context.httpRequest().getMethod())) {
                Enumeration<String> names = context.httpRequest().getParameterNames();
                StringBuilder body = new StringBuilder();
                while (names.hasMoreElements()) {
                    String key = names.nextElement();
                    for (String value : context.parameters(key)) {
                        if (body.length() > 0) {
                            body.append('&');
                        }
                        try {
                            body.append(key).append("=").append(URLEncoder.encode(value, "UTF-8"));
                        } catch (UnsupportedEncodingException ex) {
                        }
                    }
                }
                if (body.length() > 0) {
                    connection.setDoOutput(true);
                    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                    out.write(body.toString());
                    out.close();
                }
            }
        }
        try {
            IOUtils.copy(connection.getInputStream(), writer);
        } catch (IOException ex) {
            writer.write("<span>SSI Error: " + ex.getMessage() + "</span>");
        }
    }
public VersionInfo getVersionInfo(String url) {
        try {
            XmlContentHandler handler = new XmlContentHandler();
            XMLReader myReader = XMLReaderFactory.createXMLReader();
            myReader.setContentHandler(handler);
            myReader.parse(new InputSource(new URL(url).openStream()));
            return handler.getVersionInfo();
        } catch (SAXException e) {
            if (debug) {
                println("SAXException was thrown!");
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            if (debug) {
                println("MalformedURLException was thrown!");
                e.printStackTrace();
            }
        } catch (IOException e) {
            if (debug) {
                println("IOException was thrown!");
                e.printStackTrace();
            }
        }
        return null;
    }
public String getPasswordMD5() {
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(password.getBytes());
            byte messageDigest[] = algorithm.digest();
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException nsae) {
        }
        return null;
    }
public static String getMD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }
public static String createHash(String seed) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Can't happen!", e);
        }
        try {
            md.update(seed.getBytes(CHARSET));
            md.update(String.valueOf(System.currentTimeMillis()).getBytes(CHARSET));
            return toHexString(md.digest());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Can't happen!", e);
        }
    }
protected static String hashPassword(String password, String salt) throws NoSuchAlgorithmException {
        String s = salt + password;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(s.getBytes());
        byte bs[] = md.digest();
        String s1 = BASE64Encoder.encode(bs);
        return new StringBuffer(salt).append(':').append(s1).toString();
    }
private boolean dependencyOutOfDate(ScriptCacheEntry entry) {
        if (entry != null) {
            for (Iterator i = entry.dependencies.keySet().iterator(); i.hasNext(); ) {
                URLConnection urlc = null;
                URL url = (URL) i.next();
                try {
                    urlc = url.openConnection();
                    urlc.setDoInput(false);
                    urlc.setDoOutput(false);
                    long dependentLastModified = urlc.getLastModified();
                    if (dependentLastModified > ((Long) entry.dependencies.get(url)).longValue()) {
                        return true;
                    }
                } catch (IOException ioe) {
                    return true;
                }
            }
        }
        return false;
    }
public static void cpdir(File src, File dest) throws BrutException {
        dest.mkdirs();
        File[] files = src.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            File destFile = new File(dest.getPath() + File.separatorChar + file.getName());
            if (file.isDirectory()) {
                cpdir(file, destFile);
                continue;
            }
            try {
                InputStream in = new FileInputStream(file);
                OutputStream out = new FileOutputStream(destFile);
                IOUtils.copy(in, out);
                in.close();
                out.close();
            } catch (IOException ex) {
                throw new BrutException("Could not copy file: " + file, ex);
            }
        }
    }
private void copyResource(final String resourceName, final File file) throws IOException {
        assertTrue(resourceName.startsWith("/"));
        InputStream in = null;
        boolean suppressExceptionOnClose = true;
        try {
            in = this.getClass().getResourceAsStream(resourceName);
            assertNotNull("Resource '" + resourceName + "' not found.", in);
            OutputStream out = null;
            try {
                out = new FileOutputStream(file);
                IOUtils.copy(in, out);
                suppressExceptionOnClose = false;
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (final IOException e) {
                    if (!suppressExceptionOnClose) {
                        throw e;
                    }
                }
            }
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
                if (!suppressExceptionOnClose) {
                    throw e;
                }
            }
        }
    }
public InputStream getExportFile() {
        URL url = ExportAction.class.getClassLoader().getResource("sysConfig.xml");
        if (url != null) try {
            return url.openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
public static String backupFile(File source) {
        File backup = new File(source.getParent() + "/~" + source.getName());
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(backup)));
            return FileUtil.backupFile(reader, writer, source.getAbsolutePath());
        } catch (FileNotFoundException fe) {
            String msg = "Failed to find file for backup [" + source.getAbsolutePath() + "].";
            _log.error(msg, fe);
            throw new InvalidImplementationException(msg, fe);
        }
    }
private String loadStatusResult() {
        try {
            URL url = new URL(getServerUrl());
            InputStream input = url.openStream();
            InputStreamReader is = new InputStreamReader(input, "utf-8");
            BufferedReader reader = new BufferedReader(is);
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return null;
        } catch (IOException e2) {
            e2.printStackTrace();
            return null;
        }
    }
@Override
    protected URLConnection openConnection(URL url) throws IOException {
        if (url.getQuery() == null) throw new IllegalStateException("Missing TemplateAccount number in rest URL " + url);
        MSResource msResource = null;
        try {
            long templateAccountId = Long.parseLong(url.getQuery());
            msResource = menuBean.findMSResource(templateAccountId, url.getPath());
        } catch (Exception e) {
            throw new IllegalStateException("Resource not found in database: " + url, e);
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(msResource.getValue());
        return new RestConnection(url, bais);
    }
protected List webservice(URL url, List locations, boolean followRedirect) throws GeoServiceException {
        long start = System.currentTimeMillis();
        int rowCount = 0, hitCount = 0;
        try {
            HttpURLConnection con;
            try {
                con = (HttpURLConnection) url.openConnection();
                try {
                    con.getClass().getMethod("setConnectTimeout", new Class[] { Integer.TYPE }).invoke(con, new Object[] { TIMEOUT });
                } catch (Throwable t) {
                    LOG.info("can't set connection timeout");
                }
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                con.setDoInput(true);
                Writer out = new OutputStreamWriter(con.getOutputStream(), UTF8);
                out.write(HEADER + "\n");
                for (int i = 0; i < locations.size(); i++) {
                    if (i > 0) out.write("\n");
                    out.write(encode((GeoLocation) locations.get(i)));
                }
                out.close();
            } catch (IOException e) {
                throw new GeoServiceException("Accessing GEO Webservice failed", e);
            }
            List rows = new ArrayList();
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), UTF8));
                for (int l = 0; l < locations.size(); l++) {
                    String line = in.readLine();
                    LOG.finer(line);
                    if (line == null) break;
                    if (l == 0 && followRedirect) {
                        try {
                            return webservice(new URL(line), locations, false);
                        } catch (MalformedURLException e) {
                        }
                    }
                    rowCount++;
                    List row = new ArrayList();
                    if (!line.startsWith("?")) {
                        StringTokenizer hits = new StringTokenizer(line, ";");
                        while (hits.hasMoreTokens()) {
                            GeoLocation hit = decode(hits.nextToken());
                            if (hit != null) {
                                row.add(hit);
                                hitCount++;
                            }
                        }
                    }
                    rows.add(row);
                }
                in.close();
            } catch (IOException e) {
                throw new GeoServiceException("Reading from GEO Webservice failed", e);
            }
            if (rows.size() < locations.size()) throw new GeoServiceException("GEO Webservice returned " + rows.size() + " rows for " + locations.size() + " locations");
            return rows;
        } finally {
            long secs = (System.currentTimeMillis() - start) / 1000;
            LOG.fine("query for " + locations.size() + " locations in " + secs + "s resulted in " + rowCount + " rows and " + hitCount + " total hits");
        }
    }
public void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        FileChannel source = null;
        FileChannel destination = null;
        Closer c = new Closer();
        try {
            source = c.register(new FileInputStream(sourceFile).getChannel());
            destination = c.register(new FileOutputStream(destFile).getChannel());
            destination.transferFrom(source, 0, source.size());
        } catch (IOException e) {
            c.doNotThrow();
            throw e;
        } finally {
            c.closeAll();
        }
    }
@Override
        public boolean exists() {
            if (local_file.exists()) {
                return true;
            } else {
                try {
                    URLConnection c = remote_url.openConnection();
                    try {
                        c.setConnectTimeout(CIO.getLoadingTimeOut());
                        c.connect();
                        return c.getContentLength() > 0;
                    } catch (Exception err) {
                        err.printStackTrace();
                        return false;
                    } finally {
                        if (c instanceof HttpURLConnection) {
                            ((HttpURLConnection) c).disconnect();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }
private static ArrayList<String> BingSearch(String query) {
        ArrayList<String> bingSearchResults = new ArrayList<String>();
        try {
            String request = "http://api.bing.net/xml.aspx?AppId=731DD1E61BE6DE4601A3008DC7A0EB379149EC29" + "&Version=2.2&Market=en-US&Query=" + URLEncoder.encode("Java example for " + query, "UTF-8") + "&Sources=web+spell&Web.Count=30";
            URL url = new URL(request);
            System.out.println("Host : " + url.getHost());
            url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            String finalContents = "";
            while ((inputLine = reader.readLine()) != null) {
                finalContents += "\n" + inputLine;
            }
            Document doc = Jsoup.parse(finalContents);
            Elements eles = doc.getElementsByTag("web:Url");
            for (Element ele : eles) {
                String urlText = ele.text();
                if (!urlText.endsWith(".pdf") && !urlText.endsWith(".doc") && !urlText.endsWith(".ppt") && !urlText.endsWith(".PDF") && !urlText.endsWith(".DOC") && !urlText.endsWith(".PPT")) bingSearchResults.add(ele.text());
                System.out.println("BingResult: " + ele.text());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bingSearchResults;
    }
private InputStream getPart() throws IOException {
        HttpGet get = new HttpGet(url);
        get.addHeader("Range", "bytes=" + startAt + "-");
        HttpResponse res = client.execute(get);
        System.out.println("requesting kBs from " + startAt + "     server reply:" + res.getStatusLine());
        if (res.getStatusLine().getStatusCode() == 403 || res.getStatusLine().toString().toLowerCase().contains("forbidden")) {
            get.abort();
            get = new HttpGet(url);
            get.addHeader("Range", "bytes=" + startAt + "-" + (startAt + downLimit));
            res = client.execute(get);
            System.out.println("Again requesting from kBs " + startAt + "     server reply:" + res.getStatusLine());
            startAt += downLimit;
        } else {
            complete = true;
        }
        return res.getEntity() == null ? null : res.getEntity().getContent();
    }
public static boolean copyFile(final File src, final File dst) throws FileNotFoundException {
        if (src == null || dst == null || src.equals(dst)) {
            return false;
        }
        boolean result = false;
        if (src.exists()) {
            if (dst.exists() && !dst.canWrite()) {
                return false;
            }
            final FileInputStream srcStream = new FileInputStream(src);
            final FileOutputStream dstStream = new FileOutputStream(dst);
            final FileChannel srcChannel = srcStream.getChannel();
            final FileChannel dstChannel = dstStream.getChannel();
            FileLock dstLock = null;
            FileLock srcLock = null;
            try {
                srcLock = srcChannel.tryLock(0, Long.MAX_VALUE, true);
                dstLock = dstChannel.tryLock();
                if (srcLock != null && dstLock != null) {
                    int maxCount = 64 * 1024 * 1024 - 32 * 1024;
                    long size = srcChannel.size();
                    long position = 0;
                    while (position < size) {
                        position += srcChannel.transferTo(position, maxCount, dstChannel);
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(FileUtils.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (srcChannel != null) {
                    try {
                        if (srcLock != null) {
                            srcLock.release();
                        }
                        srcChannel.close();
                        srcStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(FileUtils.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (dstChannel != null) {
                    try {
                        if (dstLock != null) {
                            dstLock.release();
                        }
                        dstChannel.close();
                        dstStream.close();
                        result = true;
                    } catch (IOException ex) {
                        Logger.getLogger(FileUtils.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        return result;
    }
public static int[] sortstring(int[] a1) {
        int temp;
        for (int j = 0; j < (a1.length * a1.length); j++) {
            for (int i = 0; i < a1.length - 1; i++) {
                if (a1[i] > a1[i + 1]) {
                    temp = a1[i];
                    a1[i] = a1[i + 1];
                    a1[i + 1] = temp;
                }
            }
        }
        return a1;
    }