Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.util.Base64;

import java.nio.ByteBuffer;

import java.util.UUID;

public class Main {
    /**
     * Generates a Base64 version of UUID
     * @return a string in Base64 URL-safe format without padding at the end and no wrapping
     */
    public static String randomUuidBase64() {
        UUID uuid = UUID.randomUUID();
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());
        return encodeUrlSafe64(bb.array());
    }

    public static String encodeUrlSafe64(byte[] array) {
        int flags = Base64.NO_PADDING | Base64.NO_WRAP | Base64.URL_SAFE;
        return Base64.encodeToString(array, flags);
    }
}