handleEOD method

void handleEOD(
  1. EncoderContext context,
  2. StringBuilder buffer
)

Handle "end of data" situations

@param context the encoder context @param buffer the buffer with the remaining encoded characters

Implementation

void handleEOD(EncoderContext context, StringBuilder buffer) {
  final unwritten = (buffer.length ~/ 3) * 2;
  final rest = buffer.length % 3;

  final curCodewordCount = context.codewordCount + unwritten;
  context.updateSymbolInfo(curCodewordCount);
  final available = context.symbolInfo!.dataCapacity - curCodewordCount;

  if (rest == 2) {
    buffer.write('\x00'); //Shift 1
    while (buffer.length >= 3) {
      writeNextTriplet(context, buffer);
    }
    if (context.hasMoreCharacters) {
      context.writeCodeword(HighLevelEncoder.c40Unlatch);
    }
  } else if (available == 1 && rest == 1) {
    while (buffer.length >= 3) {
      writeNextTriplet(context, buffer);
    }
    if (context.hasMoreCharacters) {
      context.writeCodeword(HighLevelEncoder.c40Unlatch);
    }
    // else no unlatch
    context.pos--;
  } else if (rest == 0) {
    while (buffer.length >= 3) {
      writeNextTriplet(context, buffer);
    }
    if (available > 0 || context.hasMoreCharacters) {
      context.writeCodeword(HighLevelEncoder.c40Unlatch);
    }
  } else {
    throw StateError('Unexpected case. Please report!');
  }
  context.signalEncoderChange(HighLevelEncoder.asciiEncodation);
}