*/
public class IdGenerator {
+ /** The default length of IDs (in bytes before conversion to a string). */
+ public static final int DEFAULT_LENGTH = 20;
+
/** The RNG used to created IDs. */
private static Random random = new Random();
/**
- * Generates a new random ID, consisting of 16 random bytes.
+ * Generates a new random ID, consisting of {@link #DEFAULT_LENGTH} random
+ * bytes.
*
* @return The new ID
*/
public static byte[] generateId() {
- byte[] idBytes = new byte[16];
+ return generateId(DEFAULT_LENGTH);
+ }
+
+ /**
+ * Generates a new random ID, consisting of <code>length</code> random
+ * bytes.
+ *
+ * @param length
+ * The length of the ID in bytes before conversion to a string
+ * @return The new ID
+ */
+ public static byte[] generateId(int length) {
+ byte[] idBytes = new byte[length];
random.nextBytes(idBytes);
return idBytes;
}