Class Writer

extends Object
A consumer of bytes.
Inheritance
The method try-write_ must be implemented by subclasses.
The method flush may be implemented by subclasses. The default implementation does nothing.

Class summary


Constructors

Statics

adapt writer/any -> Writer
Constructor to convert old-style writers to this writer.
The writer must either be a old-writer.Writer or a class with a write function that returns the number of bytes written.

Methods

Provides endian-aware functions to write to this instance.
The big-endian byte order writes higher-order (big) bytes first. For example, if the target of the write operation is a byte array, then the first byte written (at position 0) is the most significant byte of the number that is written.
Examples

import io

main:
  buffer := io.Buffer
  buffer.big-endian.write-int32 0x12345678
  // The most significant byte 0x12 is at index 0.
  print buffer.bytes  // => #[0x12, 0x34, 0x56, 0x78]

flush -> none
Flushes any buffered data to the underlying resource.
Often, one can just use the --flush flag of the write, write-byte or write-from functions instead.
Inheritance
This method may be overwritten by subclasses. The default implementation does nothing.

Provides endian-aware functions to write to this instance.
The little-endian byte order writes lower-order ("little") bytes first. For example, if the target of the write operation is a byte array, then the first byte written (at position 0) is the least significant byte of the number that is written.
Examples

import io

main:
  buffer := io.Buffer  // A writer.
  buffer.little-endian.write-int32 0x12345678
  // The least significant byte 0x78 is at index 0.
  print buffer.bytes  // => #[0x78, 0x56, 0x34, 0x12]

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

The amount of bytes that have been written to this writer so far.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

try-write data/Data from/int=0 to/int=data.byte-size -> int
Tries to write the given data to this writer.
If the writer can't write all the data at once, it writes as much as possible.
If the writer is closed while writing, throws, or returns the number of bytes written.
Otherwise always returns the number of bytes written.
Inheritance
Implementations are not required to check whether the writer is closed.
If the writer is closed while this operation is in progress, the writer may throw an exception or return a number smaller than to - from.

write data/Data from/int=0 to/int=data.byte-size --flush/bool=false -> int
Writes the given data to this writer.
Returns the amount of bytes written (to - from).
If the writer can't write all the data at once tries again until all of the data is written. This method is blocking.

write-byte value/int --flush/bool=false -> none
Writes a single byte.

write-from reader/Reader --flush/bool=false -> none
Writes all data that is provided by the given reader.