1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
177 md5Bytes[8] &= 0x3f; /* clear variant */
178 md5Bytes[8] |= 0x80; /* set to IETF variant */
179 return new UUID(md5Bytes);
180 }
181
182 /**
183 * Creates a {@code UUID} from the string standard representation as
184 * described in the {@link #toString} method.
185 *
186 * @param name
187 * A string that specifies a {@code UUID}
188 *
189 * @return A {@code UUID} with the specified value
190 *
191 * @throws IllegalArgumentException
192 * If name does not conform to the string representation as
193 * described in {@link #toString}
194 *
195 */
196 public static UUID fromString(String name) {
197 int len = name.length();
198 if (len > 36) {
199 throw new IllegalArgumentException("UUID string too large");
200 }
201
202 int dash1 = name.indexOf('-', 0);
203 int dash2 = name.indexOf('-', dash1 + 1);
204 int dash3 = name.indexOf('-', dash2 + 1);
205 int dash4 = name.indexOf('-', dash3 + 1);
206 int dash5 = name.indexOf('-', dash4 + 1);
207
208 // For any valid input, dash1 through dash4 will be positive and dash5
209 // negative, but it's enough to check dash4 and dash5:
210 // - if dash1 is -1, dash4 will be -1
211 // - if dash1 is positive but dash2 is -1, dash4 will be -1
212 // - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be
213 // positive, but so will dash5
214 if (dash4 < 0 || dash5 >= 0) {
215 throw new IllegalArgumentException("Invalid UUID string: " + name);
216 }
217
218 long mostSigBits = Long.parseLong(name, 0, dash1, 16) & 0xffffffffL;
219 mostSigBits <<= 16;
220 mostSigBits |= Long.parseLong(name, dash1 + 1, dash2, 16) & 0xffffL;
221 mostSigBits <<= 16;
222 mostSigBits |= Long.parseLong(name, dash2 + 1, dash3, 16) & 0xffffL;
223 long leastSigBits = Long.parseLong(name, dash3 + 1, dash4, 16) & 0xffffL;
224 leastSigBits <<= 48;
225 leastSigBits |= Long.parseLong(name, dash4 + 1, len, 16) & 0xffffffffffffL;
226
227 return new UUID(mostSigBits, leastSigBits);
228 }
229
230 // Field Accessor Methods
231
232 /**
233 * Returns the least significant 64 bits of this UUID's 128 bit value.
234 *
235 * @return The least significant 64 bits of this UUID's 128 bit value
236 */
237 public long getLeastSignificantBits() {
238 return leastSigBits;
239 }
240
241 /**
242 * Returns the most significant 64 bits of this UUID's 128 bit value.
243 *
244 * @return The most significant 64 bits of this UUID's 128 bit value
245 */
246 public long getMostSignificantBits() {
247 return mostSigBits;
|
1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
177 md5Bytes[8] &= 0x3f; /* clear variant */
178 md5Bytes[8] |= 0x80; /* set to IETF variant */
179 return new UUID(md5Bytes);
180 }
181
182 /**
183 * Creates a {@code UUID} from the string standard representation as
184 * described in the {@link #toString} method.
185 *
186 * @param name
187 * A string that specifies a {@code UUID}
188 *
189 * @return A {@code UUID} with the specified value
190 *
191 * @throws IllegalArgumentException
192 * If name does not conform to the string representation as
193 * described in {@link #toString}
194 *
195 */
196 public static UUID fromString(String name) {
197 int dash1 = name.indexOf('-', 0);
198 int dash2 = name.indexOf('-', dash1 + 1);
199 int dash3 = name.indexOf('-', dash2 + 1);
200 int dash4 = name.indexOf('-', dash3 + 1);
201 int dash5 = name.indexOf('-', dash4 + 1);
202
203 // For any valid input, dash1 through dash4 will be positive and dash5
204 // negative, but it's enough to check dash4 and dash5:
205 // - if dash1 is -1, dash4 will be -1
206 // - if dash1 is positive but dash2 is -1, dash4 will be -1
207 // - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be
208 // positive, but so will dash5
209
210 if (dash4 > 0 && dash5 < 0) {
211 long x1 = Long.parseLong(name, 0, dash1, 16);
212 long x2 = Long.parseLong(name, dash1 + 1, dash2, 16);
213 long x3 = Long.parseLong(name, dash2 + 1, dash3, 16);
214 long x4 = Long.parseLong(name, dash3 + 1, dash4, 16);
215 long x5 = Long.parseLong(name, dash4 + 1, name.length(), 16);
216
217 if (((x1 >> 32) | ((x2 | x3 | x4) >> 16) | (x5 >> 48)) == 0L) {
218 return new UUID((x1 << 32) | (x2 << 16) | x3, (x4 << 48) | x5);
219 }
220 }
221 throw new IllegalArgumentException("Invalid UUID string: " + name);
222 }
223
224 // Field Accessor Methods
225
226 /**
227 * Returns the least significant 64 bits of this UUID's 128 bit value.
228 *
229 * @return The least significant 64 bits of this UUID's 128 bit value
230 */
231 public long getLeastSignificantBits() {
232 return leastSigBits;
233 }
234
235 /**
236 * Returns the most significant 64 bits of this UUID's 128 bit value.
237 *
238 * @return The most significant 64 bits of this UUID's 128 bit value
239 */
240 public long getMostSignificantBits() {
241 return mostSigBits;
|