Skip to content

RandomAccessFile.writeByteSync documented return value is misleading #42298

@jamesderlin

Description

@jamesderlin

The documentation for RandomAccessFile.writeByteSync states:

Synchronously writes a single byte to the file. Returns the number of bytes successfully written.

Throws a [FileSystemException] if the operation fails.

If it's writing a single byte, shouldn't the return value always be 1? It shouldn't ever return 0 since it is supposed to throw on failure.

But wait, if the write position is beyond the end of the file, writeByteSync apparently will extend the file, zero-filling the missing bytes. So maybe the return value isn't useless...

... except that the return value does not account for the file length extension. AFAICT, it always returns 1.

import 'dart:io';

void main() {
  final f = File('some_new_file');
  final h = f.openSync(mode: FileMode.write);
  print('Initial length: ${h.lengthSync()}'); // Prints: Initial length: 0
  h.setPositionSync(10);
  print('Length after setPosition: ${h.lengthSync()}'); // Prints: Length after setPosition: 0 
  final n = h.writeByteSync(0xFF);
  print('Wrote: $n bytes'); // Prints: 1
  h.closeSync();
}

Either the return value should be fixed or the documentation should just say "Always returns 1."

It also would be good to document that the file will be extended and zero-filled if the write position is out of bounds.

  • Dart SDK: 2.8.4 on Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-iotype-documentationA request to add or improve documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions