encode method

  1. @override
void encode(
  1. EncoderContext context
)
override

Implementation

@override
void encode(EncoderContext context) {
  final buffer = StringBuilder();
  buffer.write('\x00'); //Initialize length field
  while (context.hasMoreCharacters) {
    final c = context.currentChar;
    buffer.writeCharCode(c);

    context.pos++;

    final newMode = HighLevelEncoder.lookAheadTest(
      context.message,
      context.pos,
      encodingMode,
    );
    if (newMode != encodingMode) {
      // Return to ASCII encodation, which will actually handle latch to new mode
      context.signalEncoderChange(HighLevelEncoder.asciiEncodation);
      break;
    }
  }
  final dataCount = buffer.length - 1;
  final lengthFieldSize = 1;
  final currentSize = context.codewordCount + dataCount + lengthFieldSize;
  context.updateSymbolInfo(currentSize);
  final mustPad = (context.symbolInfo!.dataCapacity - currentSize) > 0;
  if (context.hasMoreCharacters || mustPad) {
    if (dataCount <= 249) {
      buffer.setCharAt(0, dataCount);
    } else if (dataCount <= 1555) {
      buffer.setCharAt(0, (dataCount ~/ 250) + 249);
      buffer.insert(1, dataCount % 250);
    } else {
      throw StateError('Message length not in valid ranges: $dataCount');
    }
  }
  final l = buffer.length;
  for (int i = 0; i < l; i++) {
    context.writeCodeword(
      _randomize255State(buffer.charAt(i), context.codewordCount + 1),
    );
  }
}