feiyu02
2022-10-21 f22c4b9230808fed4fec80c435eccb4c833349a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cn.flightfeather.supervision.infrastructure.utils
 
import org.apache.commons.logging.LogFactory
import java.net.InetAddress
import java.security.SecureRandom
import java.util.*
import kotlin.experimental.and
 
class UUIDGenerator {
    private var midValue: String? = null
    private var seeder: SecureRandom? = null
 
    init {
        midValue = null
        seeder = null
        val buffer = StringBuffer(16)
        buffer.append(midValueStatic)
        buffer.append(toHex(System.identityHashCode(this), 8))
        midValue = buffer.toString()
        seeder = SecureRandom()
        seeder!!.nextInt()
    }
 
    fun generate(): String {
        val uid = StringBuffer(32)
        val currentTimeMillis = System.currentTimeMillis()
        uid.append(toHex((currentTimeMillis and -1L).toInt(), 8))
        uid.append(midValue)
        uid.append(toHex(seeder!!.nextInt(), 8))
        return uid.toString()
    }
 
    companion object {
 
        private val log = LogFactory.getLog(UUIDGenerator::class.java)
 
        private var seederStatic: SecureRandom? = null
        private var addr: ByteArray? = null
        private var midValueStatic: String? = null
        private var prevMillis = 0L
        private var addrBytes: ByteArray? = null
 
        init {
            seederStatic = null
            addr = null
            try {
                addr = InetAddress.getLocalHost().address
                addrBytes = InetAddress.getLocalHost().address
                val buffer = StringBuffer(8)
                buffer.append(toHex(toInt(addr!!), 8))
                midValueStatic = buffer.toString()
                seederStatic = SecureRandom()
                seederStatic!!.nextInt()
            } catch (ex: Exception) {
                log.error("", ex)
            }
 
        }
 
        fun generate(obj: Any): String {
            val uid = StringBuffer(32)
            val currentTimeMillis = System.currentTimeMillis()
            uid.append(toHex((currentTimeMillis and -1L).toInt(), 8))
            uid.append(midValueStatic)
            uid.append(toHex(System.identityHashCode(obj), 8))
            uid.append(toHex(random, 8))
            return uid.toString()
        }
 
        fun generate16(obj: Any): String {
            val uid = StringBuffer(16)
            uid.append(toHex(System.identityHashCode(obj), 8))
            uid.append(toHex(random, 8))
            return uid.toString()
        }
 
        private fun toHex(value: Int, length: Int): String {
            var value = value
            val hexDigits = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
            val buffer = StringBuffer(length)
            val shift = length - 1 shl 2
            var i = -1
            while (++i < length) {
                buffer.append(hexDigits[value shr shift and 0xf])
                value = value shl 4
            }
 
            return buffer.toString()
        }
 
        private fun toInt(bytes: ByteArray): Int {
            var value = 0
            var i = -1
            while (++i < bytes.size) {
                value = value shl 8
                value = value or bytes[i].toInt()
            }
 
            return value
        }
 
        private val random: Int
            @Synchronized get() = seederStatic!!.nextInt()
 
        private val systemTimeMillis: Long
            @Synchronized get() {
                val millis = System.currentTimeMillis()
                if (millis > prevMillis) {
                    prevMillis = millis
                } else {
                    prevMillis++
                }
                return prevMillis
            }
 
        val uniqueLong: Long
            get() {
                var l = systemTimeMillis
                l *= 1000L
                val b1 = (addrBytes!![3] and 0xff.toByte()).toLong()
                l += b1
                return java.lang.Long.valueOf(l)
            }
 
        var chars = arrayOf(
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h",
            "i",
            "j",
            "k",
            "l",
            "m",
            "n",
            "o",
            "p",
            "q",
            "r",
            "s",
            "t",
            "u",
            "v",
            "w",
            "x",
            "y",
            "z",
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
        )
 
 
        /*
    * 长度为8位的UUID
     */
        fun generateShortUUID(): String {
            val shortBuffer = StringBuffer()
            val uuid = UUID.randomUUID().toString().replace("-", "")
            for (i in 0..7) {
                val str = uuid.substring(i * 4, i * 4 + 4)
                val x = Integer.parseInt(str, 16)
                shortBuffer.append(chars[x % 0x3E])
            }
            return shortBuffer.toString()
 
        }
 
        /*
    * 长度为16位的UUID
     */
        fun generate16ShortUUID(): String {
            val shortBuffer = StringBuffer()
            val uuid = UUID.randomUUID().toString().replace("-", "")
            for (i in 0..15) {
                val str = uuid.substring(i * 2, i * 2 + 2)
                val x = Integer.parseInt(str, 16)
                shortBuffer.append(chars[x % 0x3E])
            }
            return shortBuffer.toString()
 
        }
 
        /**
         * 长度为指定位数的UUID
         * @param uuidLength
         * @return
         */
        fun generateUUID(uuidLength: Int): String {
            val shortBuffer = StringBuffer()
            val uuid = UUID.randomUUID().toString().replace("-", "")
            for (i in 0 until uuidLength) {
                val str = uuid.substring(i * 4, i * 4 + 4)
                val x = Integer.parseInt(str, 16)
                shortBuffer.append(chars[x % 0x3E])
            }
            return shortBuffer.toString()
 
        }
    }
}