Skip to content

Commit

Permalink
Adding byte functions for UUIDs (apache#11988)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgranderath authored Nov 14, 2023
1 parent b685a2e commit 53883ae
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.Base64;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -493,6 +495,37 @@ public static byte[] toAscii(String input) {
return input.getBytes(StandardCharsets.US_ASCII);
}

/**
* @param input UUID as string
* @return bytearray
* returns bytes and null on exception
*/
@ScalarFunction
public static byte[] toUUIDBytes(String input) {
try {
UUID uuid = UUID.fromString(input);
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
} catch (IllegalArgumentException e) {
return null;
}
}

/**
* @param input UUID serialized to bytes
* @return String representation of UUID
* returns bytes and null on exception
*/
@ScalarFunction
public static String fromUUIDBytes(byte[] input) {
ByteBuffer bb = ByteBuffer.wrap(input);
long firstLong = bb.getLong();
long secondLong = bb.getLong();
return new UUID(firstLong, secondLong).toString();
}

/**
* @see Normalizer#normalize(CharSequence, Normalizer.Form)
* @param input
Expand Down

0 comments on commit 53883ae

Please sign in to comment.