2013-07-27 4 views
14

Io sono la conversione UUID di byte utilizzando questo codiceconvertito uuid al byte, che funziona quando si utilizza UUID.nameUUIDFromBytes (b)

public byte[] getIdAsByte(UUID uuid) 
{ 
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]); 
    bb.putLong(uuid.getMostSignificantBits()); 
    bb.putLong(uuid.getLeastSignificantBits()); 
    return bb.array(); 
} 

Tuttavia, se provo a ricreare l'UUID di utilizzare questa funzione,

public UUID frombyte(byte[] b) 
{ 
    return UUID.nameUUIDFromBytes(b); 
} 

Non è lo stesso UUID. La conversione casuale di unUUIDO indietro e indietro ne restituisce due diversi.

UUID u = UUID.randomUUID(); 
System.out.println(u.toString()); 
System.out.println(frombyte(getIdAsByte(u)).toString()); 

stampe:

1ae004cf-0f48-469f-8a94-01339afaec41 
8b5d1a71-a4a0-3b46-bec3-13ab9ab12e8e 
+0

correlati, Eventuali duplicati: [? Cosa namespace fa l'uso JDK per generare un UUID con nameUUIDFromBytes] (http://stackoverflow.com/questions/9504519/what-namespace-does -the-jdk-usa-per-generare-un-uuid-con-nomeuuidfrombytes) –

risposta

35
public class UuidUtils { 
    public static UUID asUuid(byte[] bytes) { 
    ByteBuffer bb = ByteBuffer.wrap(bytes); 
    long firstLong = bb.getLong(); 
    long secondLong = bb.getLong(); 
    return new UUID(firstLong, secondLong); 
    } 

    public static byte[] asBytes(UUID uuid) { 
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]); 
    bb.putLong(uuid.getMostSignificantBits()); 
    bb.putLong(uuid.getLeastSignificantBits()); 
    return bb.array(); 
    } 
} 

@Test 
public void verifyUUIDBytesCanBeReconstructedBackToOriginalUUID() { 
    UUID u = UUID.randomUUID(); 
    byte[] uBytes = UuidUtils.asBytes(u); 
    UUID u2 = UuidUtils.asUuid(uBytes); 
    Assert.assertEquals(u, u2); 
} 

@Test 
public void verifyNameUUIDFromBytesMethodDoesNotRecreateOriginalUUID() { 
    UUID u = UUID.randomUUID(); 
    byte[] uBytes = UuidUtils.asBytes(u); 
    UUID u2 = UUID.nameUUIDFromBytes(uBytes); 
    Assert.assertNotEquals(u, u2); 
} 
+0

Ciò implica che tutti sono d'accordo sulla rappresentazione little-endian. –

+0

perché dovresti primaLungo == secondLong con il doppio dello stesso calcolo? Si suppone che questo parametro sia il bit più/meno significativo nella rappresentazione di byte. Qualche opinione? – belka

17

questo perché nameUUIDFromBytes costruisce uno specifico tipo di UUID (come afferma Javadoc).

se si desidera convertire un byte [] di nuovo in un UUID, è necessario utilizzare il costruttore UUID. Avvolgi un ByteBuffer attorno al byte [], leggi i 2 long e passali al costruttore UUID.

+4

Un codice di esempio per questo sarebbe: 'ByteBuffer bb = ByteBuffer.wrap (inputByteArray); long firstLong = bb.getLong(); long secondLong = bb.getLong(); ritorno nuovo UUID (firstLong, secondLong); ' – djvdorp

+0

Cosa succede se l'accesso al contructor UUID è limitato come in Minecraft? Ho questo problema perché sto cercando di condensare un UUID e alcuni dati in byte in un grande file per creare una caratteristica Shop di un plugin per un server bukkit. il file è di tipo binario in array: [0] UUID [1] denaro [2] punti. Questo si ripeterà semplicemente andando giù nell'elenco nel file. –

+2

@LinkTheProgrammer - se hai un problema diverso, probabilmente dovresti aprire una nuova domanda. – jtahlborn

Problemi correlati