Example usage for java.nio ByteBuffer putLong

List of usage examples for java.nio ByteBuffer putLong

Introduction

In this page you can find the example usage for java.nio ByteBuffer putLong.

Prototype

public abstract ByteBuffer putLong(long value);

Source Link

Document

Writes the given long to the current position and increases the position by 8.

Usage

From source file:com.nestedbird.util.UUIDConverter.java

/**
 * Turns a UUID in string format to a Base64 encoded version
 *
 * @param uuidString String representation of the uuid
 * @return base64 encoded version of the uuid
 * @throws IllegalArgumentException String must be a valid uuid
 * @throws NullPointerException     String cannot be null
 */// w  w w.  jav  a  2  s  .c  om
public static String toBase64(final String uuidString) {
    if (uuidString == null)
        throw new NullPointerException("String cannot be null");
    if (!isUUID(uuidString))
        throw new IllegalArgumentException("string must be a valid uuid");

    final UUID uuid = UUID.fromString(uuidString);
    final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:com.srotya.tau.nucleus.Utils.java

public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(x);
    return buffer.array();
}

From source file:com.pinterest.deployservice.common.CommonUtils.java

/**
 * TODO figure out how to use guava to achive this
 *
 * @return base64 encoded shorten UUID, e.g. 11YozyYYTvKmuUXpRDvoJA
 *///from ww  w . j  a  v a  2  s  .  com
public static String getBase64UUID() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
    buffer.putLong(uuid.getMostSignificantBits());
    buffer.putLong(uuid.getLeastSignificantBits());
    String base64 = BaseEncoding.base64Url().omitPadding().encode(buffer.array());
    if (base64.charAt(0) == '_') {
        return 'x' + base64.substring(1);
    }
    if (base64.charAt(0) == '-') {
        return 'X' + base64.substring(1);
    }
    return base64;
}

From source file:io.datenwelt.cargo.rest.utils.Strings.java

public static String token() {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 100);
    for (int i = 0; i < 100; i++) {
        buffer.putLong(random.nextLong());
    }//from w w w.  ja v  a2 s . c  o  m
    return DigestUtils.md5Hex(buffer.array());
}

From source file:vellum.crypto.topt.Totps.java

static int getCode(byte[] secret, long timeIndex) throws NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1");
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(timeIndex);
    byte[] timeBytes = buffer.array();
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(signKey);//from ww w .ja  v a2  s  . co m
    byte[] hash = mac.doFinal(timeBytes);
    int offset = hash[19] & 0xf;
    long truncatedHash = hash[offset] & 0x7f;
    for (int i = 1; i < 4; i++) {
        truncatedHash <<= 8;
        truncatedHash |= hash[offset + i] & 0xff;
    }
    return (int) (truncatedHash %= 1000000);
}

From source file:com.networknt.light.util.HashUtil.java

public static String generateUUID() {
    UUID id = UUID.randomUUID();
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(id.getMostSignificantBits());
    bb.putLong(id.getLeastSignificantBits());
    Base64 base64 = new Base64();
    return base64.encodeBase64URLSafeString(bb.array());
}

From source file:com.knewton.mapreduce.util.RandomStudentEventGenerator.java

/**
 * Helper method for wrapping an id in a <code>ByteBuffer</code>.
 *
 * @return A byte buffer with a random ID
 *///w w  w  .  ja  v a  2  s  . c o  m
public static ByteBuffer getRandomIdBuffer() {
    long id = getRandomId();
    ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
    bb.putLong(id);
    bb.rewind();
    return bb;
}

From source file:org.linguafranca.pwdb.kdbx.Helpers.java

public static String base64FromUuid(UUID uuid) {
    byte[] buffer = new byte[16];
    ByteBuffer b = ByteBuffer.wrap(buffer);
    b.putLong(uuid.getMostSignificantBits());
    b.putLong(8, uuid.getLeastSignificantBits());
    // round the houses for Android
    return new String(Base64.encodeBase64(buffer));
}

From source file:org.linguafranca.pwdb.kdbx.Helpers.java

public static String hexStringFromUuid(UUID uuid) {
    byte[] buffer = new byte[16];
    ByteBuffer b = ByteBuffer.wrap(buffer);
    b.putLong(uuid.getMostSignificantBits());
    b.putLong(8, uuid.getLeastSignificantBits());
    // round the houses for Android
    return new String(Hex.encodeHex(buffer));
}

From source file:com.cnaude.mutemanager.UUIDFetcher.java

public static byte[] toBytes(UUID uuid) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
    byteBuffer.putLong(uuid.getMostSignificantBits());
    byteBuffer.putLong(uuid.getLeastSignificantBits());
    return byteBuffer.array();
}