encode static method

String encode(
  1. List<int> bytes, [
  2. int lineSize = 0,
  3. int paddingSpace = 0
])

Implementation

static String encode(
  List<int> bytes, [
  int lineSize = 0,
  int paddingSpace = 0,
]) {
  var len = bytes.length;
  if (len == 0) {
    return '';
  }
  // Size of 24 bit chunks.
  final remainderLength = len.remainder(3);
  final chunkLength = len - remainderLength;
  // Size of base output.
  var outputLen =
      ((len ~/ 3) * 4) + ((remainderLength > 0) ? 4 : 0) + paddingSpace;
  // Add extra for line separators.
  var lineSizeGroup = lineSize >> 2;
  if (lineSizeGroup > 0) {
    outputLen +=
        ((outputLen - 1) ~/ (lineSizeGroup << 2)) * (1 + paddingSpace);
  }
  var out = List<int>.filled(outputLen, 0);

  // Encode 24 bit chunks.
  int j = 0, i = 0, c = 0;
  for (var i = 0; i < paddingSpace; ++i) {
    out[j++] = SP;
  }
  while (i < chunkLength) {
    var x =
        (((bytes[i++] % 256) << 16) & 0xFFFFFF) |
        (((bytes[i++] % 256) << 8) & 0xFFFFFF) |
        (bytes[i++] % 256);
    out[j++] = _encodeTable.codeUnitAt(x >> 18);
    out[j++] = _encodeTable.codeUnitAt((x >> 12) & 0x3F);
    out[j++] = _encodeTable.codeUnitAt((x >> 6) & 0x3F);
    out[j++] = _encodeTable.codeUnitAt(x & 0x3f);
    // Add optional line separator for each 76 char output.
    if (lineSizeGroup > 0 && ++c == lineSizeGroup && j < outputLen - 2) {
      out[j++] = LF;
      for (var i = 0; i < paddingSpace; ++i) {
        out[j++] = SP;
      }
      c = 0;
    }
  }

  // If input length if not a multiple of 3, encode remaining bytes and
  // add padding.
  if (remainderLength == 1) {
    var x = bytes[i] % 256;
    out[j++] = _encodeTable.codeUnitAt(x >> 2);
    out[j++] = _encodeTable.codeUnitAt((x << 4) & 0x3F);
    //     out[j++] = PAD;
    //     out[j++] = PAD;
    return String.fromCharCodes(out.sublist(0, outputLen - 2));
  } else if (remainderLength == 2) {
    var x = bytes[i] % 256;
    var y = bytes[i + 1] % 256;
    out[j++] = _encodeTable.codeUnitAt(x >> 2);
    out[j++] = _encodeTable.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F);
    out[j++] = _encodeTable.codeUnitAt((y << 2) & 0x3F);
    //     out[j++] = PAD;
    return String.fromCharCodes(out.sublist(0, outputLen - 1));
  }

  return String.fromCharCodes(out);
}