📄 uuidutil.java
字号:
/* * Copyright (c) 2003, The Regents of the University of California, through * Lawrence Berkeley National Laboratory (subject to receipt of any required * approvals from the U.S. Dept. of Energy). All rights reserved. */package gov.lbl.dsd.sea.nio.util;import java.security.SecureRandom;import java.util.Random;/** * Utilities to create Universally Unique Identifiers as defined by IETF standards. * * @author whoschek@lbl.gov * @author $Author: hoschek3 $ * @version $Revision: 1.2 $, $Date: 2004/07/27 00:18:19 $ */public class UUIDUtil { private static Random randomGenerator; /** * Make non-instantiable */ protected UUIDUtil() {} /** * Returns a type 4 (pseudo randomly generated) Universally Unique Identifier * as defined by IETF standards. Example UUID: <code>e96d0caf-76f4-44a9-904e-cad4c0f93b22</code>. * The returned UUID consists of 36 characters. Note that this method is thread-safe. * <p> * See <a href="http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/draft-leach-uuids-guids-01.txt">draft-leach-uuids-guids-01.txt</a> * for details. * * @param generator - * the (pseudo) random number generator used to generate random * bytes. Set this parameter to <code>null</code> to use a * built-in default secure random number generator. * * @return a randomly generated UUID. */ public static String createRandomUUID(Random generator) { if (generator == null) { if (randomGenerator == null) randomGenerator = new SecureRandom(); generator = randomGenerator; } byte[] data = new byte[16]; generator.nextBytes(data); data[6] &= 0x0f; data[6] |= 0x40; data[8] &= 0x3f; data[8] |= 0x80; String chars = "0123456789abcdefABCDEF"; StringBuffer buf = new StringBuffer(36); for (int i = 0; i < 16; i++) { if (i==4 || i==6 || i==8 || i==10) buf.append('-'); int val = data[i] & 0xFF; buf.append(chars.charAt(val >> 4)); buf.append(chars.charAt(val & 0x0f)); } return buf.toString(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -