2121import org .bson .BsonSerializationException ;
2222import org .bson .UuidRepresentation ;
2323
24+ import java .util .Arrays ;
2425import java .util .UUID ;
2526
2627/**
@@ -85,21 +86,28 @@ public static byte[] encodeUuidToBinary(final UUID uuid, final UuidRepresentatio
8586 return binaryData ;
8687 }
8788
89+ // This method will NOT modify the contents of the byte array
8890 public static UUID decodeBinaryToUuid (final byte [] data , final byte type , final UuidRepresentation uuidRepresentation ) {
8991 if (data .length != 16 ) {
9092 throw new BsonSerializationException (String .format ("Expected length to be 16, not %d." , data .length ));
9193 }
9294
95+ byte [] localData = data ;
96+
9397 if (type == BsonBinarySubType .UUID_LEGACY .getValue ()) {
9498 switch (uuidRepresentation ) {
9599 case C_SHARP_LEGACY :
96- reverseByteArray (data , 0 , 4 );
97- reverseByteArray (data , 4 , 2 );
98- reverseByteArray (data , 6 , 2 );
100+ localData = Arrays .copyOf (data , 16 );
101+
102+ reverseByteArray (localData , 0 , 4 );
103+ reverseByteArray (localData , 4 , 2 );
104+ reverseByteArray (localData , 6 , 2 );
99105 break ;
100106 case JAVA_LEGACY :
101- reverseByteArray (data , 0 , 8 );
102- reverseByteArray (data , 8 , 8 );
107+ localData = Arrays .copyOf (data , 16 );
108+
109+ reverseByteArray (localData , 0 , 8 );
110+ reverseByteArray (localData , 8 , 8 );
103111 break ;
104112 case PYTHON_LEGACY :
105113 break ;
@@ -111,7 +119,7 @@ public static UUID decodeBinaryToUuid(final byte[] data, final byte type, final
111119 }
112120 }
113121
114- return new UUID (readLongFromArrayBigEndian (data , 0 ), readLongFromArrayBigEndian (data , 8 ));
122+ return new UUID (readLongFromArrayBigEndian (localData , 0 ), readLongFromArrayBigEndian (localData , 8 ));
115123 }
116124
117125 private UuidHelper () {
0 commit comments