semeru/Code-Code-CloneDetection-BigCloneBench huggingface dataset:
public static String generate(String source) {
        byte[] SHA = new byte[20];
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(source.getBytes());
            SHA = digest.digest();
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NO SUCH ALGORITHM EXCEPTION: " + e.getMessage());
        }
        CommunicationLogger.warning("SHA1 DIGEST: " + SHA);
        return SHA.toString();
    }
@Override
    public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {
        if (soapAction == null) {
            soapAction = "\"\"";
        }
        byte[] requestData = createRequestData(envelope);
        requestDump = debug ? new String(requestData) : null;
        responseDump = null;
        HttpPost method = new HttpPost(url);
        method.addHeader("User-Agent", "kSOAP/2.0-Excilys");
        method.addHeader("SOAPAction", soapAction);
        method.addHeader("Content-Type", "text/xml");
        HttpEntity entity = new ByteArrayEntity(requestData);
        method.setEntity(entity);
        HttpResponse response = httpClient.execute(method);
        InputStream inputStream = response.getEntity().getContent();
        if (debug) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[256];
            while (true) {
                int rd = inputStream.read(buf, 0, 256);
                if (rd == -1) {
                    break;
                }
                bos.write(buf, 0, rd);
            }
            bos.flush();
            buf = bos.toByteArray();
            responseDump = new String(buf);
            inputStream.close();
            inputStream = new ByteArrayInputStream(buf);
        }
        parseResponse(envelope, inputStream);
        inputStream.close();
    }
String sendRequest(String[] getVars, String[] postVars, Object[] fileVars, boolean getSessionKey) throws IOException {
        String uri = wikiBaseURI;
        if (getVars != null) for (int i = 0; i + 1 < getVars.length; i += 2) uri += (i == 0 ? '?' : '&') + urlEncode(getVars[i]) + '=' + urlEncode(getVars[i + 1]);
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setUseCaches(false);
        if (!getSessionKey) {
            String cookie = "";
            for (String key : cookies.keySet()) cookie += (cookie.length() == 0 ? "" : "; ") + key + "=" + cookies.get(key);
            conn.setRequestProperty("Cookie", cookie);
        }
        if (fileVars != null) {
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.connect();
            PrintStream ps = new PrintStream(conn.getOutputStream());
            for (int i = 0; fileVars != null && i + 2 < fileVars.length; i += 3) {
                ps.print("--" + boundary + "\r\n");
                postFile(ps, conn, (String) fileVars[i], (String) fileVars[i + 1], (byte[]) fileVars[i + 2]);
            }
            for (int i = 0; postVars != null && i + 1 < postVars.length; i += 2) ps.print("--" + boundary + "\r\n" + "Content-Disposition: " + "form-data; name=\"" + postVars[i] + "\"\r\n\r\n" + postVars[i + 1] + "\r\n");
            ps.println("--" + boundary + "--");
            ps.close();
        } else if (postVars != null) {
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.connect();
            PrintStream ps = new PrintStream(conn.getOutputStream());
            for (int i = 0; postVars != null && i + 1 < postVars.length; i += 2) ps.print((i == 0 ? "" : "&") + urlEncode(postVars[i]) + "=" + urlEncode(postVars[i + 1]));
            ps.close();
        }
        int httpCode = conn.getResponseCode();
        if (httpCode != 200) throw new IOException("HTTP code: " + httpCode);
        if (getSessionKey) getCookies(conn.getHeaderFields().get("Set-Cookie"));
        InputStream in = conn.getInputStream();
        response = "";
        byte[] buffer = new byte[1 << 16];
        for (; ; ) {
            int len = in.read(buffer);
            if (len < 0) break;
            response += new String(buffer, 0, len);
        }
        in.close();
        return response;
    }
public static byte[] readHTTPFile(String url, StringBuffer contentType, StringBuffer encoding) {
        try {
            URL u = new URL(url);
            URLConnection urlConn = u.openConnection();
            urlConn.setReadTimeout(10 * 1000);
            urlConn.setConnectTimeout(10 * 1000);
            urlConn.setDoInput(true);
            urlConn.setDoOutput(false);
            String status = urlConn.getHeaderField(null).toLowerCase();
            String location = urlConn.getHeaderField("Location");
            String cookie = urlConn.getHeaderField("Set-Cookie");
            int times = 0;
            while ((status.indexOf("http/1.1 3") >= 0 || status.indexOf("http/1.0 3") >= 0) && !HelperStd.isEmpty(location)) {
                if (!HelperStd.isEmpty(urlConn.getHeaderField("Set-Cookie"))) cookie = urlConn.getHeaderField("Set-Cookie");
                u = new URL(location);
                urlConn = u.openConnection();
                urlConn.setReadTimeout(10 * 1000);
                urlConn.setConnectTimeout(10 * 1000);
                urlConn.setDoInput(true);
                urlConn.setDoOutput(false);
                urlConn.setRequestProperty("Cookie", cookie);
                status = urlConn.getHeaderField(null).toLowerCase();
                location = urlConn.getHeaderField("Location");
                times++;
                if (times > 10) break;
            }
            System.out.println(urlConn.getHeaderField(null) + ":" + urlConn.getContentLength() + ":" + u);
            if (contentType != null) contentType.append(urlConn.getContentType());
            if (encoding != null) {
                String enc = null, ct = urlConn.getContentType();
                if (ct != null && ct.indexOf("charset=") > 0) {
                    int a = ct.indexOf("charset=") + "charset=".length();
                    enc = ct.substring(a);
                }
                if (enc == null) enc = urlConn.getContentEncoding();
                if (enc == null) enc = "ISO-8859-1";
                encoding.append(enc);
            }
            BufferedInputStream in = new BufferedInputStream(urlConn.getInputStream());
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int read = 0;
            while (read != -1) {
                read = in.read(b);
                if (read > 0) bout.write(b, 0, read);
            }
            in.close();
            System.out.println(bout.size());
            return bout.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("readHTTPFile:" + e.getMessage() + "," + url);
        }
        return new byte[0];
    }
public void serveResource(HTTPResource resource, HttpServletRequest request, HttpServletResponse response) throws IOException {
        JavaScriptResource jsr = (JavaScriptResource) resource;
        response.setContentType("text/javascript");
        if (jsr.getScriptText() != null) {
            PrintWriter pw = response.getWriter();
            pw.println(jsr.getScriptText());
        } else if (jsr.getResourceName() != null) {
            URL url = ClassLoaderUtil.getResource(jsr.getResourceName());
            IOUtil.copyData(response.getOutputStream(), url.openStream());
        } else {
            throw new IOException("No Javascript to Serve");
        }
    }
public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
        Assert.notNull(resourceName, "Resource name must not be null");
        ClassLoader clToUse = classLoader;
        if (clToUse == null) {
            clToUse = ClassUtils.getDefaultClassLoader();
        }
        Properties properties = new Properties();
        Enumeration urls = clToUse.getResources(resourceName);
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            InputStream is = null;
            try {
                URLConnection con = url.openConnection();
                con.setUseCaches(false);
                is = con.getInputStream();
                properties.load(is);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }
        return properties;
    }
@Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException {
        String context = request.getContextPath();
        String resource = request.getRequestURI().replace(context, "");
        resource = resource.replaceAll(RESOURCES_PATH + "/", "");
        if ((StringUtils.isEmpty(resource)) || (resource.endsWith("/"))) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        this.getLogger().info("Getting resource: " + resource);
        URL url = ClassLoaderUtils.getResource(resource);
        if (url == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        InputStream input = url.openStream();
        OutputStream output = response.getOutputStream();
        URLConnection connection = url.openConnection();
        String contentEncoding = connection.getContentEncoding();
        int contentLength = connection.getContentLength();
        String contentType = connection.getContentType();
        if (contentEncoding != null) {
            response.setCharacterEncoding(contentEncoding);
        }
        response.setContentLength(contentLength);
        response.setContentType(contentType);
        IOUtils.copy(input, output, true);
    }
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
        if (destFile.exists() && destFile.isDirectory()) {
            throw new IOException("Destination '" + destFile + "' exists but is a directory");
        }
        long time = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel input = null;
        FileChannel output = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            input = fis.getChannel();
            output = fos.getChannel();
            long size = input.size();
            long pos = 0;
            long count = 0;
            while (pos < size && continueWriting(pos, size)) {
                count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
                pos += output.transferFrom(input, pos, count);
            }
        } finally {
            output.close();
            IOUtils.closeQuietly(fos);
            input.close();
            IOUtils.closeQuietly(fis);
        }
        if (srcFile.length() != destFile.length()) {
            if (DiskManager.isLocked()) throw new IOException("Copy stopped since VtM was working"); else throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
        } else {
            time = System.currentTimeMillis() - time;
            long speed = (destFile.length() / time) / 1000;
            DiskManager.addDiskSpeed(speed);
        }
        if (preserveFileDate) {
            destFile.setLastModified(srcFile.lastModified());
        }
    }
public void testFidelity() throws ParserException, IOException {
        Lexer lexer;
        Node node;
        int position;
        StringBuffer buffer;
        String string;
        char[] ref;
        char[] test;
        URL url = new URL("http://sourceforge.net");
        lexer = new Lexer(url.openConnection());
        position = 0;
        buffer = new StringBuffer(80000);
        while (null != (node = lexer.nextNode())) {
            string = node.toHtml();
            if (position != node.getStartPosition()) fail("non-contiguous" + string);
            buffer.append(string);
            position = node.getEndPosition();
            if (buffer.length() != position) fail("text length differed after encountering node " + string);
        }
        ref = lexer.getPage().getText().toCharArray();
        test = new char[buffer.length()];
        buffer.getChars(0, buffer.length(), test, 0);
        assertEquals("different amounts of text", ref.length, test.length);
        for (int i = 0; i < ref.length; i++) if (ref[i] != test[i]) fail("character differs at position " + i + ", expected <" + ref[i] + "> but was <" + test[i] + ">");
    }
public static float medianElement(float[] array, int size) {
        float[] tmpArray = new float[size];
        System.arraycopy(array, 0, tmpArray, 0, size);
        boolean changed = true;
        while (changed) {
            changed = false;
            for (int i = 0; i < size - 1; i++) {
                if (tmpArray[i] > tmpArray[i + 1]) {
                    changed = true;
                    float tmp = tmpArray[i];
                    tmpArray[i] = tmpArray[i + 1];
                    tmpArray[i + 1] = tmp;
                }
            }
        }
        return tmpArray[size / 2];
    }
public static String sha1Hash(String input) {
        try {
            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
            sha1Digest.update(input.getBytes());
            return byteArrayToString(sha1Digest.digest());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "";
    }
public static String md5hash(String input) {
        try {
            MessageDigest sha1Digest = MessageDigest.getInstance("MD5");
            sha1Digest.update(input.getBytes());
            return byteArrayToString(sha1Digest.digest());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "";
    }
public void restoreBackup(File backupFile, File destDir, boolean replaceExistingFile) throws BackupException, FileAlreadyExistsException {
        try {
            File databaseEncryptionFile = new File(destDir.getPath() + File.separator + getDatabase() + DATABASE_FILE_END);
            if (databaseEncryptionFile.exists() == true && replaceExistingFile == false) {
                throw new FileAlreadyExistsException("File " + databaseEncryptionFile.getName() + "already exists");
            }
            IOUtils.copy(FileUtils.openInputStream(backupFile), FileUtils.openOutputStream(databaseEncryptionFile));
        } catch (FileNotFoundException e) {
            throw new BackupException(e);
        } catch (IOException e) {
            throw new BackupException(e);
        }
    }
public synchronized String encrypt(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = null;
        md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes("UTF-8"));
        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash;
    }
public static String digestString(String data, String algorithm) {
        String result = null;
        if (data != null) {
            try {
                MessageDigest _md = MessageDigest.getInstance(algorithm);
                _md.update(data.getBytes());
                byte[] _digest = _md.digest();
                String _ds;
                _ds = toHexString(_digest, 0, _digest.length);
                result = _ds;
            } catch (NoSuchAlgorithmException e) {
                result = null;
            }
        }
        return result;
    }
public static void copyDirectory(File sourceDirectory, File targetDirectory) throws IOException {
        File[] sourceFiles = sourceDirectory.listFiles(FILE_FILTER);
        File[] sourceDirectories = sourceDirectory.listFiles(DIRECTORY_FILTER);
        targetDirectory.mkdirs();
        if (sourceFiles != null && sourceFiles.length > 0) {
            for (int i = 0; i < sourceFiles.length; i++) {
                File sourceFile = sourceFiles[i];
                FileInputStream fis = new FileInputStream(sourceFile);
                FileOutputStream fos = new FileOutputStream(targetDirectory + File.separator + sourceFile.getName());
                FileChannel fcin = fis.getChannel();
                FileChannel fcout = fos.getChannel();
                ByteBuffer buf = ByteBuffer.allocateDirect(8192);
                long size = fcin.size();
                long n = 0;
                while (n < size) {
                    buf.clear();
                    if (fcin.read(buf) < 0) {
                        break;
                    }
                    buf.flip();
                    n += fcout.write(buf);
                }
                fcin.close();
                fcout.close();
                fis.close();
                fos.close();
            }
        }
        if (sourceDirectories != null && sourceDirectories.length > 0) {
            for (int i = 0; i < sourceDirectories.length; i++) {
                File directory = sourceDirectories[i];
                File newTargetDirectory = new File(targetDirectory, directory.getName());
                copyDirectory(directory, newTargetDirectory);
            }
        }
    }
public synchronized String encrypt(String plaintext) throws ServiceUnavailableException {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new ServiceUnavailableException(e.getMessage());
        }
        try {
            md.reset();
            md.update(plaintext.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new ServiceUnavailableException(e.getMessage());
        }
        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash;
    }
protected Properties load(URL url) {
        try {
            InputStream i = url.openStream();
            Properties p = new Properties();
            p.load(i);
            i.close();
            return p;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
public void parse(String file) throws IOException, URISyntaxException {
        if (file == null) {
            throw new IOException("File '" + file + "' file not found");
        }
        InputStream is = null;
        if (file.startsWith("http://")) {
            URL url = new URL(file);
            is = url.openStream();
        } else if (file.startsWith("file:/")) {
            is = new FileInputStream(new File(new URI(file)));
        } else {
            is = new FileInputStream(file);
        }
        if (file.endsWith(".gz")) {
            is = new GZIPInputStream(is);
        }
        parse(new InputStreamReader(is));
    }
public static void copyFile(String sourceFilePath, String destFilePath) throws IOException {
        FileChannel in = null;
        FileChannel out = null;
        try {
            in = new FileInputStream(sourceFilePath).getChannel();
            out = new FileOutputStream(destFilePath).getChannel();
            long inputSize = in.size();
            in.transferTo(0, inputSize, out);
            MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, inputSize);
            out.write(buf);
        } finally {
            if (in != null) in.close();
            if (out != null) out.close();
        }
    }
public static String getMD5(String s) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(s.getBytes(), 0, s.length());
            String result = new BigInteger(1, m.digest()).toString(16);
            while (result.length() < 32) {
                result = '0' + result;
            }
            return result;
        } catch (NoSuchAlgorithmException ex) {
            return null;
        }
    }
public static String getCssFile(String url) {
        StringBuffer buffer = new StringBuffer();
        try {
            buffer = new StringBuffer();
            URL urlToCrawl = new URL(url);
            URLConnection urlToCrawlConnection = urlToCrawl.openConnection();
            urlToCrawlConnection.setRequestProperty("User-Agent", "USER_AGENT");
            urlToCrawlConnection.setRequestProperty("Referer", "REFERER");
            urlToCrawlConnection.setUseCaches(false);
            InputStreamReader isr = new InputStreamReader(urlToCrawlConnection.getInputStream());
            BufferedReader in = new BufferedReader(isr);
            String str;
            while ((str = in.readLine()) != null) buffer.append(str);
            FileOutputStream fos = new FileOutputStream("c:\\downloads\\" + System.currentTimeMillis() + ".css");
            Writer out = new OutputStreamWriter(fos);
            out.write(buffer.toString());
            out.close();
        } catch (Exception e) {
            System.out.println("Error Downloading css file" + e);
        }
        return buffer.toString();
    }
@Override
            public void run() {
                try {
                    bitmapDrawable = new BitmapDrawable(new URL(url).openStream());
                    imageCache.put(id, new SoftReference<Drawable>(bitmapDrawable));
                    log("image::: put:  id = " + id + " ");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        iv.setImageDrawable(bitmapDrawable);
                    }
                });
            }
private String md5(String s) {
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.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 e) {
            e.printStackTrace();
        }
        return "";
    }
public void testDelegateUsage() throws IOException {
        MockControl elementParserControl = MockControl.createControl(ElementParser.class);
        ElementParser elementParser = (ElementParser) elementParserControl.getMock();
        elementParserControl.replay();
        URL url = getClass().getResource("/net/sf/ngrease/core/ast/simple-element.ngr");
        ElementSource source = new ElementSourceUrlImpl(url, elementParser);
        elementParserControl.verify();
        elementParserControl.reset();
        Element element = new ElementDefaultImpl("");
        elementParser.parse(url.openStream());
        elementParserControl.setMatcher(new ArgumentsMatcher() {

            public boolean matches(Object[] arg0, Object[] arg1) {
                if (!arg0[0].getClass().equals(arg1[0].getClass())) {
                    return false;
                }
                return true;
            }

            public String toString(Object[] arg0) {
                return Arrays.asList(arg0).toString();
            }
        });
        elementParserControl.setReturnValue(element, 1);
        elementParserControl.replay();
        Element elementAgain = source.getElement();
        elementParserControl.verify();
        assertTrue(element == elementAgain);
    }
private long getLastModification() {
        try {
            if (connection == null) connection = url.openConnection();
            return connection.getLastModified();
        } catch (IOException e) {
            LOG.warn("URL could not be opened: " + e.getMessage(), e);
            return 0;
        }
    }