semeru/Code-Code-CloneDetection-BigCloneBench huggingface dataset:
public int extract() throws Exception {
        int count = 0;
        if (VERBOSE) System.out.println("IAAE:Extractr.extract: getting ready to extract " + getArtDir().toString());
        ITCFileFilter iff = new ITCFileFilter();
        RecursiveFileIterator rfi = new RecursiveFileIterator(getArtDir(), iff);
        FileTypeDeterminer ftd = new FileTypeDeterminer();
        File artFile = null;
        File targetFile = null;
        broadcastStart();
        while (rfi.hasMoreElements()) {
            artFile = (File) rfi.nextElement();
            targetFile = getTargetFile(artFile);
            if (VERBOSE) System.out.println("IAAE:Extractr.extract: working ont " + artFile.toString());
            BufferedInputStream in = null;
            BufferedOutputStream out = null;
            try {
                in = new BufferedInputStream((new FileInputStream(artFile)));
                out = new BufferedOutputStream((new FileOutputStream(targetFile)));
                byte[] buffer = new byte[10240];
                int read = 0;
                int total = 0;
                read = in.read(buffer);
                while (read != -1) {
                    if ((total <= 491) && (read > 491)) {
                        out.write(buffer, 492, (read - 492));
                    } else if ((total <= 491) && (read <= 491)) {
                    } else {
                        out.write(buffer, 0, read);
                    }
                    total = total + read;
                    read = in.read(buffer);
                }
            } catch (Exception e) {
                e.printStackTrace();
                broadcastFail();
            } finally {
                in.close();
                out.close();
            }
            broadcastSuccess();
            count++;
        }
        broadcastDone();
        return count;
    }
private String hash(String text) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        BigInteger hash = new BigInteger(1, md5.digest());
        return hash.toString(16);
    }
public static String createPseudoUUID() {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(new UID().toString().getBytes());
            try {
                String localHost = InetAddress.getLocalHost().toString();
                messageDigest.update(localHost.getBytes());
            } catch (UnknownHostException e) {
                throw new OXFException(e);
            }
            byte[] digestBytes = messageDigest.digest();
            StringBuffer sb = new StringBuffer();
            sb.append(toHexString(NumberUtils.readIntBigEndian(digestBytes, 0)));
            sb.append('-');
            sb.append(toHexString(NumberUtils.readShortBigEndian(digestBytes, 4)));
            sb.append('-');
            sb.append(toHexString(NumberUtils.readShortBigEndian(digestBytes, 6)));
            sb.append('-');
            sb.append(toHexString(NumberUtils.readShortBigEndian(digestBytes, 8)));
            sb.append('-');
            sb.append(toHexString(NumberUtils.readShortBigEndian(digestBytes, 10)));
            sb.append(toHexString(NumberUtils.readIntBigEndian(digestBytes, 12)));
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new OXFException(e);
        }
    }
public static void main(final String... args) {
        int returnCode = 0;
        if (args.length == 0) {
            System.err.println("Usage: JWGet url...");
            returnCode++;
        }
        final byte[] buf = new byte[8192];
        for (final String arg : args) {
            try {
                final URL url = new URL(arg);
                OutputStream out = null;
                InputStream in = null;
                try {
                    final URLConnection con = url.openConnection();
                    in = con.getInputStream();
                    final String location = con.getHeaderField("Content-Location");
                    final String outputFilename = new File((location != null ? new URL(url, location) : url).getFile()).getName();
                    System.err.println(outputFilename);
                    out = new FileOutputStream(outputFilename);
                    for (int bytesRead; (bytesRead = in.read(buf)) != -1; out.write(buf, 0, bytesRead)) ;
                } catch (final IOException e) {
                    System.err.println(e);
                    returnCode++;
                } finally {
                    try {
                        in.close();
                    } catch (final Exception ignore) {
                    }
                    try {
                        out.close();
                    } catch (final Exception ignore) {
                    }
                }
            } catch (final MalformedURLException e) {
                System.err.println(e);
                returnCode++;
            }
        }
        System.exit(returnCode);
    }
public static String hash(String text) {
        try {
            MessageDigest md;
            md = MessageDigest.getInstance("SHA-1");
            md.update(text.getBytes("UTF-8"), 0, text.length());
            byte[] md5hash = md.digest();
            return convertToHex(md5hash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
@Override
    public ISource writeTo(ISource output) throws ResourceException {
        try {
            Document doc = getParent().getDocument();
            Nodes places = doc.query(getPosition().getXpath());
            if (places.size() == 0) {
                places = doc.query("//html");
            }
            if (places.size() > 0 && places.get(0) instanceof Element) {
                Element target = (Element) places.get(0);
                List<URL> urls = getResourceURLs();
                if (getType() == EType.TEXT) {
                    Element tag = getHeaderTag();
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    UtilIO.writeAllTo(urls, out);
                    String content = out.toString();
                    out.close();
                    tag.appendChild(content);
                    if (getPosition().getPlace() == EPlace.START) {
                        target.insertChild(tag, 0);
                    } else {
                        target.appendChild(tag);
                    }
                } else {
                    for (URL url : urls) {
                        String file = url.toString();
                        String name = file.substring(file.lastIndexOf("/") + 1) + "_res_" + (serialNumber++);
                        Element tag = getHeaderTag(output, name);
                        File resFile = getFile(output, name);
                        if (!resFile.getParentFile().exists()) {
                            if (!resFile.getParentFile().mkdirs()) {
                                throw new ResourceException("Could not create resource directory '" + resFile.getParent() + "'.");
                            }
                        }
                        UtilIO.writeToClose(url.openStream(), new FileOutputStream(resFile));
                        if (getPosition().getPlace() == EPlace.START) {
                            target.insertChild(tag, 0);
                        } else {
                            target.appendChild(tag);
                        }
                    }
                }
            } else {
                throw new ResourceException("Head element not found.");
            }
        } catch (IOException e) {
            throw new ResourceException(e);
        } catch (SourceException e) {
            throw new ResourceException(e);
        }
        return output;
    }
public static Image readImage(URL url, ImageMimeType type, int page) throws IOException {
        if (type.javaNativeSupport()) {
            return ImageIO.read(url.openStream());
        } else if ((type.equals(ImageMimeType.DJVU)) || (type.equals(ImageMimeType.VNDDJVU)) || (type.equals(ImageMimeType.XDJVU))) {
            com.lizardtech.djvu.Document doc = new com.lizardtech.djvu.Document(url);
            doc.setAsync(false);
            DjVuPage[] p = new DjVuPage[1];
            int size = doc.size();
            if ((page != 0) && (page >= size)) {
                page = 0;
            }
            p[0] = doc.getPage(page, 1, true);
            p[0].setAsync(false);
            DjVuImage djvuImage = new DjVuImage(p, true);
            Rectangle pageBounds = djvuImage.getPageBounds(0);
            Image[] images = djvuImage.getImage(new JPanel(), new Rectangle(pageBounds.width, pageBounds.height));
            if (images.length == 1) {
                Image img = images[0];
                return img;
            } else return null;
        } else if (type.equals(ImageMimeType.PDF)) {
            PDDocument document = null;
            try {
                document = PDDocument.load(url.openStream());
                int resolution = 96;
                List<?> pages = document.getDocumentCatalog().getAllPages();
                PDPage pdPage = (PDPage) pages.get(page);
                BufferedImage image = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, resolution);
                return image;
            } finally {
                if (document != null) {
                    document.close();
                }
            }
        } else throw new IllegalArgumentException("unsupported mimetype '" + type.getValue() + "'");
    }
public static void writeFullImageToStream(Image scaledImage, String javaFormat, OutputStream os) throws IOException {
        BufferedImage bufImage = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
        Graphics gr = bufImage.getGraphics();
        gr.drawImage(scaledImage, 0, 0, null);
        gr.dispose();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bufImage, javaFormat, bos);
        IOUtils.copyStreams(new ByteArrayInputStream(bos.toByteArray()), os);
    }
private static void grab(String urlString) throws MalformedURLException, IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();
        BufferedReader in = null;
        StringBuffer sb = new StringBuffer();
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        boolean f = false;
        while ((inputLine = in.readLine()) != null) {
            inputLine = inputLine.trim();
            if (inputLine.startsWith("<tbody>")) {
                f = true;
                continue;
            }
            if (inputLine.startsWith("</table>")) {
                f = false;
                continue;
            }
            if (f) {
                sb.append(inputLine);
                sb.append("\n");
            }
        }
        process(sb.toString());
    }
public static void main(String[] args) throws Exception {
        String uri = args[0];
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        Path inputPath = new Path(uri);
        CompressionCodecFactory factory = new CompressionCodecFactory(conf);
        CompressionCodec codec = factory.getCodec(inputPath);
        if (codec == null) {
            System.err.println("No codec found for " + uri);
            System.exit(1);
        }
        String outputUri = CompressionCodecFactory.removeSuffix(uri, codec.getDefaultExtension());
        InputStream in = null;
        OutputStream out = null;
        try {
            in = codec.createInputStream(fs.open(inputPath));
            out = fs.create(new Path(outputUri));
            IOUtils.copyBytes(in, out, conf);
        } finally {
            IOUtils.closeStream(in);
            IOUtils.closeStream(out);
        }
    }
public MapInfo loadLocalMapData(String fileName) {
        MapInfo info = mapCacheLocal.get(fileName);
        if (info != null && info.getContent() == null) {
            try {
                BufferedReader bufferedreader;
                URL fetchUrl = new URL(localMapContextUrl, fileName);
                URLConnection urlconnection = fetchUrl.openConnection();
                if (urlconnection.getContentEncoding() != null) {
                    bufferedreader = new BufferedReader(new InputStreamReader(urlconnection.getInputStream(), urlconnection.getContentEncoding()));
                } else {
                    bufferedreader = new BufferedReader(new InputStreamReader(urlconnection.getInputStream(), "utf-8"));
                }
                String line;
                StringBuilder mapContent = new StringBuilder();
                while ((line = bufferedreader.readLine()) != null) {
                    mapContent.append(line);
                    mapContent.append("\n");
                }
                info.setContent(mapContent.toString());
                GameMapImplementation gameMap = GameMapImplementation.createFromMapInfo(info);
            } catch (IOException _ex) {
                System.err.println("HexTD::readFile:: Can't read from " + fileName);
            }
        } else {
            System.err.println("HexTD::readFile:: file not in cache: " + fileName);
        }
        return info;
    }
private InputStream getInputStream(final String pUrlStr) throws IOException {
        URL url;
        int responseCode;
        String encoding;
        url = new URL(pUrlStr);
        myActiveConnection = (HttpURLConnection) url.openConnection();
        myActiveConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        responseCode = myActiveConnection.getResponseCode();
        if (responseCode != RESPONSECODE_OK) {
            String message;
            String apiErrorMessage;
            apiErrorMessage = myActiveConnection.getHeaderField("Error");
            if (apiErrorMessage != null) {
                message = "Received API HTTP response code " + responseCode + " with message \"" + apiErrorMessage + "\" for URL \"" + pUrlStr + "\".";
            } else {
                message = "Received API HTTP response code " + responseCode + " for URL \"" + pUrlStr + "\".";
            }
            throw new OsmosisRuntimeException(message);
        }
        myActiveConnection.setConnectTimeout(TIMEOUT);
        encoding = myActiveConnection.getContentEncoding();
        responseStream = myActiveConnection.getInputStream();
        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            responseStream = new GZIPInputStream(responseStream);
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            responseStream = new InflaterInputStream(responseStream, new Inflater(true));
        }
        return responseStream;
    }
protected void download(URL url, File destination, long beginRange, long endRange, long totalFileSize, boolean appendToFile) throws DownloadException {
        System.out.println(" DOWNLOAD REQUEST RECEIVED " + url.toString() + " \n\tbeginRange : " + beginRange + " - EndRange " + endRange + " \n\t to -> " + destination.getAbsolutePath());
        try {
            if (destination.exists() && !appendToFile) {
                destination.delete();
            }
            if (!destination.exists()) destination.createNewFile();
            GetMethod get = new GetMethod(url.toString());
            HttpClient httpClient = new HttpClient();
            Header rangeHeader = new Header();
            rangeHeader.setName("Range");
            rangeHeader.setValue("bytes=" + beginRange + "-" + endRange);
            get.setRequestHeader(rangeHeader);
            httpClient.executeMethod(get);
            int statusCode = get.getStatusCode();
            if (statusCode >= 400 && statusCode < 500) throw new DownloadException("The file does not exist in this location : message from server ->  " + statusCode + " " + get.getStatusText());
            InputStream input = get.getResponseBodyAsStream();
            OutputStream output = new FileOutputStream(destination, appendToFile);
            try {
                int length = IOUtils.copy(input, output);
                System.out.println(" Length : " + length);
            } finally {
                input.close();
                output.flush();
                output.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Unable to figure out the length of the file from the URL : " + e.getMessage());
            throw new DownloadException("Unable to figure out the length of the file from the URL : " + e.getMessage());
        }
    }
protected boolean check(String username, String password, String realm, String nonce, String nc, String cnonce, String qop, String uri, String response, HttpServletRequest request) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(username.getBytes());
            md.update((byte) ':');
            md.update(realm.getBytes());
            md.update((byte) ':');
            md.update(password.getBytes());
            byte[] ha1 = md.digest();
            md.reset();
            md.update(request.getMethod().getBytes());
            md.update((byte) ':');
            md.update(uri.getBytes());
            byte[] ha2 = md.digest();
            md.update(TypeUtil.toString(ha1, 16).getBytes());
            md.update((byte) ':');
            md.update(nonce.getBytes());
            md.update((byte) ':');
            md.update(nc.getBytes());
            md.update((byte) ':');
            md.update(cnonce.getBytes());
            md.update((byte) ':');
            md.update(qop.getBytes());
            md.update((byte) ':');
            md.update(TypeUtil.toString(ha2, 16).getBytes());
            byte[] digest = md.digest();
            return response.equals(encode(digest));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
private void loadDDL() throws IOException {
        try {
            conn.createStatement().executeQuery("SELECT * FROM overrides").close();
        } catch (SQLException e) {
            Statement stmt = null;
            if (!e.getMessage().matches(ERR_MISSING_TABLE)) {
                LOG.trace(SQL_ERROR, e);
                LOG.fatal(e);
                throw new IOException("Error on initial data store read", e);
            }
            String[] qry = { "CREATE TABLE overrides (id INT NOT NULL, title VARCHAR(255) NOT NULL, subtitle VARCHAR(255) NOT NULL, PRIMARY KEY(id))", "CREATE TABLE settings (var VARCHAR(32) NOT NULL, val VARCHAR(255) NOT NULL, PRIMARY KEY(var))", "INSERT INTO settings (var, val) VALUES ('schema', '1')" };
            try {
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                for (String q : qry) stmt.executeUpdate(q);
                conn.commit();
            } catch (SQLException e2) {
                try {
                    conn.rollback();
                } catch (SQLException e3) {
                    LOG.trace(SQL_ERROR, e3);
                    LOG.error(e3);
                }
                LOG.trace(SQL_ERROR, e2);
                throw new IOException("Error initializing data store", e2);
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e4) {
                        LOG.trace(SQL_ERROR, e4);
                        LOG.error(e4);
                        throw new IOException("Unable to cleanup data store resources", e4);
                    }
                }
                try {
                    conn.setAutoCommit(true);
                } catch (SQLException e3) {
                    LOG.trace(SQL_ERROR, e3);
                    LOG.error(e3);
                    throw new IOException("Unable to reset data store auto commit", e3);
                }
            }
        }
        return;
    }
private void upgradeSchema() throws IOException {
        Statement stmt = null;
        try {
            int i = getSchema();
            if (i < SCHEMA_VERSION) {
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                while (i < SCHEMA_VERSION) {
                    String qry;
                    switch(i) {
                        case 1:
                            qry = "CREATE TABLE log (id INTEGER PRIMARY KEY, context VARCHAR(16) NOT NULL, level VARCHAR(16) NOT NULL, time LONG INT NOT NULL, msg LONG VARCHAR NOT NULL, parent INT)";
                            stmt.executeUpdate(qry);
                            qry = "UPDATE settings SET val = '2' WHERE var = 'schema'";
                            stmt.executeUpdate(qry);
                            break;
                        case 2:
                            qry = "CREATE TABLE monitor (id INTEGER PRIMARY KEY NOT NULL, status INTEGER NOT NULL)";
                            stmt.executeUpdate(qry);
                            qry = "UPDATE settings SET val = '3' WHERE var = 'schema'";
                            stmt.executeUpdate(qry);
                            break;
                        case 3:
                            qry = "CREATE TABLE favs (id INTEGER PRIMARY KEY NOT NULL)";
                            stmt.executeUpdate(qry);
                            qry = "UPDATE settings SET val = '4' WHERE var = 'schema'";
                            stmt.executeUpdate(qry);
                            break;
                        case 4:
                            qry = "DROP TABLE log";
                            stmt.executeUpdate(qry);
                            qry = "UPDATE settings SET val = '5' WHERE var = 'schema'";
                            stmt.executeUpdate(qry);
                            break;
                        case 5:
                            qry = "UPDATE settings SET val = '120000' WHERE var = 'SleepTime'";
                            stmt.executeUpdate(qry);
                            qry = "UPDATE settings set val = '6' WHERE var = 'schema'";
                            stmt.executeUpdate(qry);
                            break;
                    }
                    i++;
                }
                conn.commit();
            }
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e2) {
                LOG.trace(SQL_ERROR, e2);
                LOG.error(e2);
            }
            LOG.trace(SQL_ERROR, e);
            LOG.fatal(e);
            throw new IOException("Error upgrading data store", e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                conn.setAutoCommit(true);
            } catch (SQLException e) {
                LOG.trace(SQL_ERROR, e);
                throw new IOException("Unable to cleanup SQL resources", e);
            }
        }
    }
public void run() {
        result.setValid(false);
        try {
            final HttpResponse response = client.execute(method, context);
            result.setValid(ArrayUtils.contains(validCodes, response.getStatusLine().getStatusCode()));
            result.setResult(response.getStatusLine().getStatusCode());
        } catch (final ClientProtocolException e) {
            LOGGER.error(e);
            result.setValid(false);
        } catch (final IOException e) {
            LOGGER.error(e);
            result.setValid(false);
        }
    }