Example usage for java.util.concurrent ThreadLocalRandom nextBytes

List of usage examples for java.util.concurrent ThreadLocalRandom nextBytes

Introduction

In this page you can find the example usage for java.util.concurrent ThreadLocalRandom nextBytes.

Prototype

public void nextBytes(byte[] bytes) 

Source Link

Document

Generates random bytes and places them into a user-supplied byte array.

Usage

From source file:com.google.gerrit.server.mail.send.SmtpEmailSender.java

public static String generateMultipartBoundary(String textBody, String htmlBody) throws EmailException {
    byte[] bytes = new byte[8];
    ThreadLocalRandom rng = ThreadLocalRandom.current();

    // The probability of the boundary being valid is approximately
    // (2^64 - len(message)) / 2^64.
    ///*  ww  w  .  ja v a2s  . c o  m*/
    // The message is much shorter than 2^64 bytes, so if two tries don't
    // suffice, something is seriously wrong.
    for (int i = 0; i < 2; i++) {
        rng.nextBytes(bytes);
        String boundary = BaseEncoding.base64().encode(bytes);
        String encBoundary = "--" + boundary;
        if (textBody.contains(encBoundary) || htmlBody.contains(encBoundary)) {
            continue;
        }
        return boundary;
    }
    throw new EmailException("Gave up generating unique MIME boundary");
}