All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.socket.engineio.server.utils.ServerYeast Maven / Gradle / Ivy

package io.socket.engineio.server.utils;

import java.security.SecureRandom;

public interface ServerYeast {

    /**
     * A ThreadLocal is used to improve thread performance since {@link SecureRandom} do not perform well with
     * thread contention.
     */
    ThreadLocal THREAD_RANDOM = new ThreadLocal<>();
    char[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_".toCharArray();

    static String yeast() {
        SecureRandom secureRandom = THREAD_RANDOM.get();
        if (secureRandom == null) {
            secureRandom = new SecureRandom();
            THREAD_RANDOM.set(secureRandom);
        }

        return encode(secureRandom.nextLong() & 0x7fffffffffffffffL);
    }

    static String encode(long num) {
        final StringBuilder encoded = new StringBuilder();
        long dividedNum = num;
        do {
            encoded.insert(0, ALPHABET[(int)(dividedNum % ALPHABET.length)]);
            dividedNum = dividedNum / ALPHABET.length;
        } while (dividedNum > 0);

        return encoded.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy