ErrorResponseMessage constructor

ErrorResponseMessage(
  1. Uint8List bytes,
  2. Encoding encoding
)

Implementation

ErrorResponseMessage(Uint8List bytes, Encoding encoding) {
  final reader = ByteDataReader()..add(bytes);

  int? identificationToken;
  List<int> currentFieldBytes = []; // Para acumular os bytes do campo atual

  while (reader.remainingLength > 0) {
    final byte = reader.readUint8();
    if (identificationToken == null) {
      identificationToken = byte;
      currentFieldBytes = []; // Começa um novo campo
    } else if (byte == 0) {
      // Fim do campo (null terminator)
      // Decodifica os bytes acumulados para este campo usando o encoding fornecido
      fields.add(ErrorField(
          identificationToken, encoding.decode(currentFieldBytes)));
      // Prepara para o próximo token de identificação
      identificationToken = null;
    } else {
      currentFieldBytes.add(byte); // Acumula byte do campo atual
    }
  }
  // Caso a mensagem termine sem um null terminator para o último campo (improvável com PG)
  if (identificationToken != null && currentFieldBytes.isNotEmpty) {
    fields.add(
        ErrorField(identificationToken, encoding.decode(currentFieldBytes)));
  }
}