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

Source

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

  // Encode 24 bit chunks.
  int j = 0, i = 0, c = 0;
  for (int i = 0; i < paddingSpace; ++i) {
    out[j++] = SP;
  }
  while (i < chunkLength) {
    int 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 (int 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) {
    int x = bytes[i] % 256;
    out[j++] = _encodeTable.codeUnitAt(x >> 2);
    out[j++] = _encodeTable.codeUnitAt((x << 4) & 0x3F);
//     out[j++] = PAD;
//     out[j++] = PAD;
    return new String.fromCharCodes(out.sublist(0, outputLen - 2));
  } else if (remainderLength == 2) {
    int x = bytes[i] % 256;
    int 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 new String.fromCharCodes(out.sublist(0, outputLen - 1));
  }

  return new String.fromCharCodes(out);
}