Created
October 6, 2015 16:05
-
-
Save davidmc24/0588900f3200eba3ea80 to your computer and use it in GitHub Desktop.
Convert Microsoft GUID to Java UUID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original source: https://github.com/zagyi/adsync4j/blob/master/core/src/main/java/org/adsync4j/impl/UUIDUtils.java | |
/******************************************************************************* | |
* ADSync4J (https://github.com/zagyi/adsync4j) | |
* | |
* Copyright (c) 2013 Balazs Zagyvai | |
* | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Balazs Zagyvai | |
******************************************************************************/ | |
package org.adsync4j.impl; | |
import java.util.UUID; | |
/** | |
* Utility class dealing with the {@link UUID}s. | |
*/ | |
public class UUIDUtils { | |
/** | |
* Converts a byte array into an {@link UUID} object. | |
* <p/> | |
* Microsoft stores GUIDs in a binary format that differs from the RFC standard of UUIDs (RFC #4122). (See details | |
* at http://en.wikipedia.org/wiki/Globally_unique_identifier) This function takes a byte array read from Active | |
* Directory and correctly decodes it as a {@link UUID} object. | |
* | |
* @param bytes Byte array received as en entry attribute from Active Directory. | |
* @return {@link UUID} object created from the byte array, or null in case the passed array is not exactly 16 bytes long. | |
*/ | |
public static UUID bytesToUUID(byte[] bytes) { | |
if (bytes != null && bytes.length == 16) { | |
long msb = bytes[3] & 0xFF; | |
msb = msb << 8 | (bytes[2] & 0xFF); | |
msb = msb << 8 | (bytes[1] & 0xFF); | |
msb = msb << 8 | (bytes[0] & 0xFF); | |
msb = msb << 8 | (bytes[5] & 0xFF); | |
msb = msb << 8 | (bytes[4] & 0xFF); | |
msb = msb << 8 | (bytes[7] & 0xFF); | |
msb = msb << 8 | (bytes[6] & 0xFF); | |
long lsb = bytes[8] & 0xFF; | |
lsb = lsb << 8 | (bytes[9] & 0xFF); | |
lsb = lsb << 8 | (bytes[10] & 0xFF); | |
lsb = lsb << 8 | (bytes[11] & 0xFF); | |
lsb = lsb << 8 | (bytes[12] & 0xFF); | |
lsb = lsb << 8 | (bytes[13] & 0xFF); | |
lsb = lsb << 8 | (bytes[14] & 0xFF); | |
lsb = lsb << 8 | (bytes[15] & 0xFF); | |
return new UUID(msb, lsb); | |
} | |
return null; | |
} | |
} |
Thanks, robotdan. Glad it was helpful!
This is to read a GUID direct from a buffer
private static UUID getGuid(ByteBuffer buffer) {
if (buffer.remaining() >= 16) {
long msb = buffer.get(buffer.position() + 3) & 0xFF;
msb = msb << 8 | (buffer.get(buffer.position() + 2) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position() + 1) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position()) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position() + 5) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position() + 4) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position() + 7) & 0xff);
msb = msb << 8 | (buffer.get(buffer.position() + 6) & 0xff);
long lsb = buffer.get(buffer.position() + 8) & 0xff;
lsb = lsb << 8 | (buffer.get(buffer.position() + 9) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 10) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 11) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 12) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 13) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 14) & 0xff);
lsb = lsb << 8 | (buffer.get(buffer.position() + 15) & 0xff);
buffer.position(buffer.position() + 16);
return new UUID(msb, lsb);
}
return null;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This was very helpful.
Here is a version using
ByteBuffer
if anyone is interested.