21
21
import org .bson .BsonSerializationException ;
22
22
import org .bson .UuidRepresentation ;
23
23
24
+ import java .util .Arrays ;
24
25
import java .util .UUID ;
25
26
26
27
/**
@@ -85,21 +86,28 @@ public static byte[] encodeUuidToBinary(final UUID uuid, final UuidRepresentatio
85
86
return binaryData ;
86
87
}
87
88
89
+ // This method will NOT modify the contents of the byte array
88
90
public static UUID decodeBinaryToUuid (final byte [] data , final byte type , final UuidRepresentation uuidRepresentation ) {
89
91
if (data .length != 16 ) {
90
92
throw new BsonSerializationException (String .format ("Expected length to be 16, not %d." , data .length ));
91
93
}
92
94
95
+ byte [] localData = data ;
96
+
93
97
if (type == BsonBinarySubType .UUID_LEGACY .getValue ()) {
94
98
switch (uuidRepresentation ) {
95
99
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 );
99
105
break ;
100
106
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 );
103
111
break ;
104
112
case PYTHON_LEGACY :
105
113
break ;
@@ -111,7 +119,7 @@ public static UUID decodeBinaryToUuid(final byte[] data, final byte type, final
111
119
}
112
120
}
113
121
114
- return new UUID (readLongFromArrayBigEndian (data , 0 ), readLongFromArrayBigEndian (data , 8 ));
122
+ return new UUID (readLongFromArrayBigEndian (localData , 0 ), readLongFromArrayBigEndian (localData , 8 ));
115
123
}
116
124
117
125
private UuidHelper () {
0 commit comments