write method

void write (
  1. dynamic output
)

Implementation

void write(dynamic output) {
  fileSize = size;

  // The name, linkname, magic, uname, and gname are null-terminated
  // character strings. All other fields are zero-filled octal numbers in
  // ASCII. Each numeric field of width w contains w minus 1 digits, and a null.
  final header = OutputStream();
  _writeString(header, filename, 100);
  _writeInt(header, mode, 8);
  _writeInt(header, ownerId, 8);
  _writeInt(header, groupId, 8);
  _writeInt(header, fileSize, 12);
  _writeInt(header, lastModTime, 12);
  _writeString(header, '        ', 8); // checksum placeholder
  _writeString(header, typeFlag, 1);

  final remainder = 512 - header.length;
  var nulls = Uint8List(remainder); // typed arrays default to 0.
  header.writeBytes(nulls);

  final headerBytes = header.getBytes();

  // The checksum is calculated by taking the sum of the unsigned byte values
  // of the header record with the eight checksum bytes taken to be ascii
  // spaces (decimal value 32). It is stored as a six digit octal number
  // with leading zeroes followed by a NUL and then a space.
  var sum = 0;
  for (var b in headerBytes) {
    sum += b;
  }

  var sum_str = sum.toRadixString(8); // octal basis
  while (sum_str.length < 6) {
    sum_str = '0' + sum_str;
  }

  var checksum_index = 148; // checksum is at 148th byte
  for (var i = 0; i < 6; ++i) {
    headerBytes[checksum_index++] = sum_str.codeUnits[i];
  }
  headerBytes[154] = 0;
  headerBytes[155] = 32;

  output.writeBytes(header.getBytes());

  if (_content != null) {
    output.writeBytes(_content);
  } else if (_rawContent != null) {
    output.writeInputStream(_rawContent);
  }

  if (isFile && fileSize > 0) {
    // Pad to 512-byte boundary
    final remainder = fileSize % 512;
    if (remainder != 0) {
      final skiplen = 512 - remainder;
      nulls = Uint8List(skiplen); // typed arrays default to 0.
      output.writeBytes(nulls);
    }
  }
}