fromBytes static method

TXTResponseRecord fromBytes({
  1. required String name,
  2. required int ttl,
  3. required Uint8List bytes,
  4. required int offset,
  5. required int length,
})

Implementation

static TXTResponseRecord fromBytes({
    required String name,
    required int ttl,
    required Uint8List bytes,
    required int offset,
    required int length }) {

    if (offset + length > bytes.length) {
        throw FormatException('TXT record out of bounds');
    }

    final List<String> texts = [];
    int i = offset;
    final int end = offset + length;

    while (i < end) {
        final int len = bytes[i];
        i += 1;

        if (i + len > end) {
            throw FormatException('Invalid TXT string length');
        }

        final String txt = utf8.decode(bytes.sublist(i, i + len));
        texts.add(txt);
        i += len;
    }

    return TXTResponseRecord(
        name: name,
        ttl: ttl,
        texts: texts);
}