Example usage for java.util UUID getMostSignificantBits

List of usage examples for java.util UUID getMostSignificantBits

Introduction

In this page you can find the example usage for java.util UUID getMostSignificantBits.

Prototype

public long getMostSignificantBits() 

Source Link

Document

Returns the most significant 64 bits of this UUID's 128 bit value.

Usage

From source file:Main.java

public static void main(String[] args) {
    // creating UUID      
    UUID uid = UUID.randomUUID();

    // checking most significant bits
    System.out.println("Most significant bits: " + uid.getMostSignificantBits());
}

From source file:org.zoxweb.shared.util.Base64Test.java

public static void main(String[] arg) {
    byte[] num = { 0, 1, 2 };

    String toConvert = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";

    System.out.println(toConvert.length());
    //System.out.println(SharedBase64.computeBase64ArraySize(num));
    System.out.println(new String(SharedBase64.encode(num)));

    long tsOld = System.nanoTime();
    byte[] oldBase64 = SharedBase64.encode(toConvert.getBytes());

    byte[] decodedBase64 = SharedBase64.decode(oldBase64);

    tsOld = System.nanoTime() - tsOld;
    System.out.println(oldBase64.length);
    System.out.println(new String(oldBase64));
    System.out.println(new String(decodedBase64));
    System.out.println(tsOld + "nanos");

    String[] testArray = { "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789",
            "1234567890", "a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg", "abcdefgh", "abcdefghi",
            "abcdefghij", toConvert, "asure.", "sure.", "any carnal pleasure" };

    String partial = "naelmarwan";

    System.out.println("Length of Array: " + testArray.length);
    long tsJ, tsZW;
    byte[] sharedEncodedeBase64, sharedDecodedBase64, java64Encoded, java64Decodded;
    Encoder javaEncoder = java.util.Base64.getEncoder();
    Decoder javaDecoder = java.util.Base64.getDecoder();
    for (int i = 0; i < testArray.length; i++) {
        System.out.println(i + 1 + "." + "Original Value: " + testArray[i]);

        byte toEncode[] = testArray[i].getBytes();
        tsZW = System.nanoTime();
        sharedEncodedeBase64 = SharedBase64.encode(Base64Type.DEFAULT, toEncode, 0, toEncode.length);
        sharedDecodedBase64 = SharedBase64.decode(Base64Type.DEFAULT, sharedEncodedeBase64, 0,
                sharedEncodedeBase64.length);
        tsZW = System.nanoTime() - tsZW;

        tsZW = System.nanoTime();
        sharedEncodedeBase64 = SharedBase64.encode(Base64Type.DEFAULT, toEncode, 0, toEncode.length);
        sharedDecodedBase64 = SharedBase64.decode(Base64Type.DEFAULT, sharedEncodedeBase64, 0,
                sharedEncodedeBase64.length);
        tsZW = System.nanoTime() - tsZW;

        tsJ = System.nanoTime();/*  ww w  .  j a v  a  2  s  . c  o m*/
        java64Encoded = javaEncoder.encode(toEncode);//.encodeBase64(toEncode);
        java64Decodded = javaDecoder.decode(java64Encoded);
        tsJ = System.nanoTime() - tsJ;

        tsJ = System.nanoTime();
        java64Encoded = javaEncoder.encode(toEncode);//.encodeBase64(toEncode);
        java64Decodded = javaDecoder.decode(java64Encoded);
        tsJ = System.nanoTime() - tsJ;

        System.out.println(i + 1 + "." + "Encode Base64: " + new String(sharedEncodedeBase64) + ":\t\t"
                + Arrays.equals(sharedEncodedeBase64, java64Encoded));
        System.out.println(i + 1 + "." + "Decoded Base64: " + new String(sharedDecodedBase64) + ":\t\t"
                + Arrays.equals(sharedDecodedBase64, java64Decodded));
        System.out.println("zoxweb:" + tsZW + " java:" + tsJ + " delta:" + (tsJ - tsZW) + " factor:"
                + ((float) tsJ / (float) tsZW));
        System.out.println(tsZW + " nanos: " + testArray[i].equals(new String(sharedDecodedBase64)));
    }

    byte[] byteArray = new byte[256];

    for (int i = 0; i < byteArray.length; i++) {
        byteArray[i] = (byte) i;
    }

    byte[] byteArrayBase64 = SharedBase64.encode(byteArray);
    System.out.println(new String(byteArrayBase64));
    System.out
            .println("Original Array length:" + byteArray.length + " base 64 length:" + byteArrayBase64.length);
    byte[] byteArrayOriginal = SharedBase64.decode(byteArrayBase64);
    System.out.println(Arrays.equals(byteArray, byteArrayOriginal));

    System.out.println("BASE_64 length:" + SharedBase64.BASE_64.length + " REVERSE_BASE_64 length:"
            + SharedBase64.REVERSE_BASE_64.length);

    System.out.println("Partial test:" + new String(SharedBase64.encode(partial.getBytes(), 4, 6)) + ","
            + new String(SharedBase64.encode(partial.getBytes(), 1, 1)));
    byte fullname[] = SharedBase64.encode(partial.getBytes());
    System.out.println("Marwan " + new String(fullname));

    System.out.println("Equals:" + SharedUtil.slowEquals(
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(),
            SharedBase64.BASE_64));

    try {
        byte[] b64 = SharedBase64.encode("1234567890");
        String str = SharedStringUtil.toString(b64);
        System.out.println("1234567890 b64:" + str);
        System.out.println(SharedBase64.decodeAsString(Base64Type.DEFAULT, "MTIzNDU2Nzg5MA"));
        System.out.println(SharedBase64.decodeAsString(Base64Type.DEFAULT, "MTIzNDU2Nzg5MA="));

        b64 = SharedBase64.decode(str + "^");
    } catch (Exception e) {
        e.printStackTrace();
    }

    UUID uuid = UUID.randomUUID();

    byte[] uuidBytes = BytesValue.LONG.toBytes(null, 0, uuid.getMostSignificantBits(),
            uuid.getMostSignificantBits());
    String str = SharedStringUtil.toString(SharedBase64.encode(Base64Type.URL, uuidBytes));
    String str1 = Base64.getUrlEncoder().encodeToString(uuidBytes);
    System.out.println(str + " " + str1 + " " + str1.equals(str));

    byte[] b = SharedBase64.decode(Base64Type.URL, str);
    byte[] b1 = Base64.getUrlDecoder().decode(str);
    System.out.println(Arrays.equals(uuidBytes, b) + " " + Arrays.equals(uuidBytes, b1));
    str = SharedStringUtil.toString(SharedBase64.encode(Base64Type.URL, b));
    System.out.println(str);

    String base64URL = "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ";
    String plain = SharedBase64.decodeAsString(Base64Type.URL, base64URL);
    System.out.println(plain);
    System.out.println(SharedBase64.encodeAsString(Base64Type.URL, plain));
}

From source file:Main.java

/**
 * Check if the specified UUID style is short style.
 *
 * @param src the UUID//from ww  w. ja v a2  s.c o  m
 * @return true if the UUID is short style
 */
private static boolean isShortUuid(@NonNull final UUID src) {
    return (src.getMostSignificantBits() & 0xffff0000ffffffffL) == 0L && src.getLeastSignificantBits() == 0L;
}

From source file:Main.java

/**
 * UUID to short style value/*www.ja  v  a  2  s.  c  om*/
 *
 * @param uuid the UUID
 * @return short style value, -1 if the specified UUID is not short style
 */
public static int toShortValue(@NonNull UUID uuid) {
    return (int) (uuid.getMostSignificantBits() >> 32 & 0xffff);
}

From source file:Main.java

/**
 * UUID to short style value/*  w w  w . ja  v a2  s  . co  m*/
 *
 * @param uuid the UUID
 * @return short style value, -1 if the specified UUID is not short style
 */
public static int toShortValue(@NonNull UUID uuid) {
    return (int) ((uuid.getMostSignificantBits() >> 32) & 0xffff);
}

From source file:Main.java

/**
 * Check if the specified UUID style is short style.
 *
 * @param src the UUID//ww w  .j  av a 2 s .  c o m
 * @return true if the UUID is short style
 */
private static boolean isShortUuid(@NonNull final UUID src) {
    return ((src.getMostSignificantBits() & 0xffff0000ffffffffL) == 0L)
            && (src.getLeastSignificantBits() == 0L);
}

From source file:Main.java

public static byte[] randomUUID() {
    UUID uuid = UUID.randomUUID();
    long hi = uuid.getMostSignificantBits();
    long lo = uuid.getLeastSignificantBits();
    return ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
}

From source file:Main.java

public static final int toUUID16(UUID uuid128) {

    long bits = uuid128.getMostSignificantBits();

    return (int) ((bits >> 32) & MASK_UUID128);
}

From source file:Main.java

public static byte[] uuidToByteArray(UUID uuid) {
    long msb = uuid.getMostSignificantBits();
    long lsb = uuid.getLeastSignificantBits();
    byte[] buffer = new byte[16];

    for (int i = 0; i < 8; i++) {
        buffer[i] = (byte) (msb >>> 8 * (7 - i));
    }//  ww  w. j av  a  2 s  .  c o  m
    for (int i = 8; i < 16; i++) {
        buffer[i] = (byte) (lsb >>> 8 * (7 - i));
    }

    return buffer;
}

From source file:Main.java

/**
 * Extract the Service Identifier or the actual uuid from the Parcel Uuid.
 * For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
 * this function will return 110B/*from  w w  w  .  j a va 2s.  c  om*/
 * @param parcelUuid
 * @return the service identifier.
 */
public static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
    UUID uuid = parcelUuid.getUuid();
    long value = (uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >>> 32;
    return (int) value;
}