toBuffer static method

BuBytes toBuffer(
  1. String uuidString, {
  2. bool validate = true,
})

Converts a UUID string to its raw 16-byte buffer.

Throws UUIDError if validate is true (the default) and uuidString isn't a validly-formatted UUID (any of the supported versions - see isValid).

Implementation

static BuBytes toBuffer(String uuidString, {bool validate = true}) {
  if (validate && !isValid(uuidString)) {
    throw BuError(
      DefaultErrorType.badUuid,
      'Invalid UUID string.',
      details: {
        'reason': 'not a valid UUID (v1/v3/v4/v5/v7) string',
        'value': uuidString,
      },
    );
  }
  final cleaned = uuidString.replaceAll('-', '');
  final bytes = BytesUtils.fromHexString(cleaned);
  if (bytes.length != 16) {
    throw BuError(
      DefaultErrorType.invalidLength,
      'Invalid UUID string.',
      details: {
        'reason': 'decoded to ${bytes.length} bytes, expected 16',
        'value': uuidString,
      },
    );
  }
  return bytes;
}