Example usage for java.lang Long toHexString

List of usage examples for java.lang Long toHexString

Introduction

In this page you can find the example usage for java.lang Long toHexString.

Prototype

public static String toHexString(long i) 

Source Link

Document

Returns a string representation of the long argument as an unsigned integer in base 16.

Usage

From source file:de.skuzzle.polly.http.internal.HttpServerImpl.java

private final String createSessionId(InetSocketAddress ip) {
    final long id = RANDOM.nextLong() * System.currentTimeMillis() * ip.hashCode();
    return Long.toHexString(id);
}

From source file:fi.hip.sicx.store.HIPStoreClient.java

@Override
public boolean storeFile(String localInputFilename, String fileInTheCloud, StorageClientObserver sco) {

    HttpURLConnection conn = null;
    boolean ret = false;
    String boundary = Long.toHexString(System.currentTimeMillis());
    String crlf = "\r\n";
    try {/*  w  w  w .ja v  a 2 s . c  o m*/
        // create the preamble to the file stream (incl. the other parameters ..)
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(bos, "UTF-8"));

        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put("permissions", "");
        params.put("path", fileInTheCloud); // this could be set in the filename

        for (Enumeration<String> e = params.keys(); e.hasMoreElements();) {
            String k = (String) e.nextElement();
            String v = params.get(k);
            writer.print("--" + boundary + crlf);
            writer.print("Content-Disposition: form-data; name=\"" + k + "\"" + crlf);
            writer.print("Content-Type: text/plain; charset=UTF-8" + crlf + crlf);
            writer.print(v + crlf);
        }

        writer.print("--" + boundary + crlf);
        writer.print(
                "Content-Disposition: form-data; name=\"data\"; filename=\"" + fileInTheCloud + "\"" + crlf);
        writer.print("Content-Type: application/octet-stream" + crlf + crlf);
        writer.close();
        byte[] preamble = bos.toByteArray();

        bos = new ByteArrayOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(bos, "UTF-8"));
        writer.print(crlf + "--" + boundary + "--" + crlf);
        writer.close();
        byte[] postamble = bos.toByteArray();

        File f = new File(localInputFilename);
        long total = preamble.length + f.length() + postamble.length;
        long sent = 0;

        // do a multipart post
        conn = getConnection("/store/store");
        conn.setDoOutput(true); // do POST
        conn.setFixedLengthStreamingMode((int) total);
        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        OutputStream out = conn.getOutputStream();
        FileInputStream fis = new FileInputStream(f);
        byte[] buf = new byte[1024 * 64];
        int r = 0;

        /* note: we need to bundle the first bytes from the file with the preamble,
           becase the apache commons fileupload used on the other side is buggy. */
        while ((r = fis.read(buf)) > 0) {
            if (preamble != null) {
                byte[] tmp = new byte[preamble.length + r];
                System.arraycopy(preamble, 0, tmp, 0, preamble.length);
                System.arraycopy(buf, 0, tmp, preamble.length, r);

                r = preamble.length + r;
                out.write(tmp, 0, r);
                preamble = null;
            } else {
                out.write(buf, 0, r);
            }
            sent += r;
            sco.progressMade((int) ((sent * 100) / total));
        }

        fis.close();

        out.write(postamble);
        sent += postamble.length;
        sco.progressMade((int) ((sent * 100) / total));
        out.flush();

        ret = true;
    } catch (Exception ex) {
        System.out.println("Error connecting, " + ex);
        ex.printStackTrace();
        failed = true;
    }
    closeConnection(conn);
    return ret;
}

From source file:de.topobyte.osm4j.extra.extracts.query.Query.java

public void execute() throws IOException {
    createTemporaryDirectory();/*from ww  w .  j  av  a  2 s .  c o  m*/

    // Query setup

    openTree();

    Geometry box = factory.toGeometry(queryEnvelope);
    List<Node> leafs = tree.query(box);

    // Query data tree

    for (Node leaf : leafs) {
        String leafName = Long.toHexString(leaf.getPath());

        if (test.contains(leaf.getEnvelope())) {
            System.out.println("Leaf is completely contained: " + leafName);
            addCompletelyContainedLeaf(leaf);
            continue;
        }

        System.out.println("Loading data from leaf: " + leafName);
        addIntersectingLeaf(leaf);
    }

    System.out.println(String.format("Total number of nodes: %d", nNodes));
    System.out.println(String.format("Total number of ways: %d", nWays));
    System.out.println(String.format("Total number of simple relations: %d", nSimpleRelations));
    System.out.println(String.format("Total number of complex relations: %d", nComplexRelations));

    // Query relations

    List<IdBboxEntry> entriesSimple = IdBboxUtil.read(paths.getSimpleRelationsBboxes());
    List<IdBboxEntry> entriesComplex = IdBboxUtil.read(paths.getComplexRelationsBboxes());

    for (IdBboxEntry entry : entriesSimple) {
        long id = entry.getId();
        if (test.contains(entry.getEnvelope())) {
            System.out.println("Simple batch completely contained: " + id);
            addCompletelyContainedBatch(paths.getSimpleRelations(), id, filesSimpleRelations);
        } else if (test.intersects(entry.getEnvelope())) {
            System.out.println("Loading data from simple batch: " + id);
            tmpIndexSimple++;
            String tmpFilenames = filename(tmpIndexSimple);
            System.out.println("Writing to files: " + tmpFilenames);

            Path pathDir = paths.getSimpleRelations().resolve(Long.toString(entry.getId()));
            Path pathNodes = pathDir.resolve(relationNames.getNodes());
            Path pathWays = pathDir.resolve(relationNames.getWays());
            Path pathRelations = pathDir.resolve(relationNames.getRelations());

            Path pathOutNodes = pathTmpSimpleNodes.resolve(tmpFilenames);
            Path pathOutWays = pathTmpSimpleWays.resolve(tmpFilenames);
            Path pathOutRelations = pathTmpSimpleRelations.resolve(tmpFilenames);

            runRelationsQuery(true, tmpFilenames, pathNodes, pathWays, pathRelations, pathOutNodes, pathOutWays,
                    pathOutRelations);
        }
    }

    for (IdBboxEntry entry : entriesComplex) {
        long id = entry.getId();
        if (test.contains(entry.getEnvelope())) {
            System.out.println("Complex batch completely contained: " + id);
            addCompletelyContainedBatch(paths.getComplexRelations(), id, filesComplexRelations);
        } else if (test.intersects(entry.getEnvelope())) {
            System.out.println("Loading data from complex batch: " + id);
            tmpIndexComplex++;
            String tmpFilenames = filename(tmpIndexComplex);
            System.out.println("Writing to files: " + tmpFilenames);

            Path pathDir = paths.getComplexRelations().resolve(Long.toString(entry.getId()));
            Path pathNodes = pathDir.resolve(relationNames.getNodes());
            Path pathWays = pathDir.resolve(relationNames.getWays());
            Path pathRelations = pathDir.resolve(relationNames.getRelations());

            Path pathOutNodes = pathTmpComplexNodes.resolve(tmpFilenames);
            Path pathOutWays = pathTmpComplexWays.resolve(tmpFilenames);
            Path pathOutRelations = pathTmpComplexRelations.resolve(tmpFilenames);

            runRelationsQuery(false, tmpFilenames, pathNodes, pathWays, pathRelations, pathOutNodes,
                    pathOutWays, pathOutRelations);
        }
    }

    // Merge intermediate files

    OsmStreamOutput output = createFinalOutput(pathOutput);

    List<OsmFileInput> mergeFiles = new ArrayList<>();

    mergeFiles.addAll(filesNodes);
    mergeFiles.addAll(filesWays);
    mergeFiles.addAll(filesSimpleRelations);
    mergeFiles.addAll(filesComplexRelations);

    System.out.println(String.format("Merging %d files", mergeFiles.size()));

    List<OsmIteratorInput> mergeIteratorInputs = new ArrayList<>();
    List<OsmIterator> mergeIterators = new ArrayList<>();
    for (OsmFileInput input : mergeFiles) {
        OsmIteratorInput iteratorInput = input.createIterator(true, outputConfig.isWriteMetadata());
        mergeIteratorInputs.add(iteratorInput);
        mergeIterators.add(iteratorInput.getIterator());
    }

    SortedMerge merge = new SortedMerge(output.getOsmOutput(), mergeIterators);
    merge.run();

    for (OsmIteratorInput input : mergeIteratorInputs) {
        input.close();
    }

    output.close();

    // Delete intermediate files

    if (!keepTmp) {
        FileUtils.deleteDirectory(pathTmp.toFile());
    }
}

From source file:czlab.wabbit.CljPodLoader.java

@Override
public String toString() {
    return "CljPodLoader@" + Long.toHexString(hashCode());
}

From source file:ch.entwine.weblounge.cache.impl.CacheEntry.java

/**
 * Returns the etag (including the extra pair of quotes) for the given value,
 * which will usually be the creation time of the cached entry.
 * //from  w  w w . ja v  a2s .c om
 * @param value
 *          the value
 * @return the etag
 */
public static String createETag(long value) {
    return "\"WL-" + Long.toHexString(value) + "\"";
}

From source file:com.chumbok.aauth.otp.TOTP.java

public static String getTOTPCode(String secretKey, long time) {
    Base32 base32 = new Base32();
    byte[] bytes = base32.decode(secretKey);
    String hexKey = Hex.encodeHexString(bytes);
    String hexTime = Long.toHexString(time);
    return TOTP.generateTOTP(hexKey, hexTime, "6");
}

From source file:net.wastl.webmail.xml.XMLUserData.java

public void addMailHost(String name, String host, String login, String password, String imapBaseDir) {
    // First, check whether a mailhost with this name already exists.
    // Delete, if yes.
    try {//from  w ww  .j  a  v a 2s  .c  om
        log.debug("Adding mailhost " + name);
        if (getMailHost(name) != null) {
            removeMailHost(name);
        }
        Element mailhost = root.createElement("MAILHOST");
        mailhost.setAttribute("name", name);
        mailhost.setAttribute("id",
                Long.toHexString(Math.abs(name.hashCode())) + Long.toHexString(System.currentTimeMillis()));

        Element mh_login = root.createElement("MH_LOGIN");
        XMLCommon.setElementTextValue(mh_login, login);
        mailhost.appendChild(mh_login);

        Element mh_pass = root.createElement("MH_PASSWORD");
        XMLCommon.setElementTextValue(mh_pass, Helper.encryptTEA(password));
        mailhost.appendChild(mh_pass);

        Element mh_uri = root.createElement("MH_URI");
        XMLCommon.setElementTextValue(mh_uri, host);
        mailhost.appendChild(mh_uri);

        if (imapBaseDir != null)
            log.fatal("Implement persistence of MH_IMAP_BASEDIR.  " + "Ignoring setting for now");

        data.appendChild(mailhost);
        log.debug("Done with mailhost " + name);
        //XMLCommon.writeXML(root,System.err,"");
        invalidateCache();
    } catch (Exception ex) {
        log.error("Failed to add mailhost.  Just aborting and continuing.", ex);
    }
}

From source file:bookkeepr.xmlable.DatabaseManager.java

private String getKey(long index) {
    long key = index / 0x1000L;
    if (index < 0) {
        return "negative";
    }/*  w w w.  ja v  a2  s.  c om*/

    //             1 2 3 4 5 6 7 8
    if (index < 0x0000100000000000L) {
        return "00000" + Long.toHexString(key);
    }
    //             1 2 3 4 5 6 7 8
    if (index < 0x0001000000000000L) {
        return "0000" + Long.toHexString(key);
    }
    if (index < 0x0010000000000000L) {
        return "000" + Long.toHexString(key);
    }
    if (index < 0x0100000000000000L) {
        return "00" + Long.toHexString(key);
    }
    if (index < 0x1000000000000000L) {
        return "0" + Long.toHexString(key);
    }
    return Long.toHexString(key);
}

From source file:net.ripe.ipresource.Ipv6Address.java

@Override
public String toString(boolean defaultMissingOctets) {
    long[] parts = new long[8];
    String[] formatted = new String[parts.length];
    for (int i = 0; i < parts.length; ++i) {
        parts[i] = getValue().shiftRight((7 - i) * 16).and(PART_MASK).longValue();
        formatted[i] = Long.toHexString(parts[i]);
    }/*from w ww.  j a  v  a2s  .co  m*/

    // Find longest sequence of zeroes. Use the first one if there are
    // multiple sequences of zeroes with the same length.
    int currentZeroPartsLength = 0;
    int currentZeroPartsStart = 0;
    int maxZeroPartsLength = 0;
    int maxZeroPartsStart = 0;
    for (int i = 0; i < parts.length; ++i) {
        if (parts[i] == 0) {
            if (currentZeroPartsLength == 0) {
                currentZeroPartsStart = i;
            }
            ++currentZeroPartsLength;
            if (currentZeroPartsLength > maxZeroPartsLength) {
                maxZeroPartsLength = currentZeroPartsLength;
                maxZeroPartsStart = currentZeroPartsStart;
            }
        } else {
            currentZeroPartsLength = 0;
        }
    }

    if (maxZeroPartsLength <= 1) {
        return StringUtils.join(formatted, COLON);
    } else {
        String init = StringUtils.join(formatted, COLON, 0, maxZeroPartsStart);
        String tail = StringUtils.join(formatted, COLON, maxZeroPartsStart + maxZeroPartsLength,
                formatted.length);
        return init + "::" + tail;
    }
}