class Writer

class Writer

lib/binary/writer.tya:2

Writer provides the binary/Writer standard library API.

Source
# Writer provides the binary/Writer standard library API.
class Writer
  # Writer.endian stores instance state.
  # @type Nil
  endian: nil

  # Writer.parts stores instance state.
  # @type Array
  parts: []

  # Writer.initialize provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @param _unused Any  unused value.
  # @return Self the initialized object.
  initialize: options, _unused ->
    self.parts = []
    self.endian = self.option_endian(options)

  # Writer.bytes provides the binary/Writer standard library operation.
  # @return Any the resulting value.
  bytes: ->
    self.to_bytes()

  # Writer.new provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @return Self the resulting value.
  new: options ->
    Writer(options, nil)

  # Writer.option_endian provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @return Any the resulting value.
  option_endian: options ->
    if options == nil
      return "big"
    if options.class != Dict
      raise error("binary.Writer: options must be a dict")
    if not options.has("endian")
      return "big"
    endian = options["endian"]
    if endian != "big" and endian != "little"
      raise error("binary.Writer: invalid endian")
    endian

  # Writer.position provides the binary/Writer standard library operation.
  # @return Int the resulting value.
  position: ->
    self.parts.len()

  # Writer.pow2 provides the binary/Writer standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow2: n ->
    out = 1
    i = 0
    while i < n
      out = out * 2
      i = i + 1
    out

  # Writer.pow256 provides the binary/Writer standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow256: n ->
    out = 1
    i = 0
    while i < n
      out = out * 256
      i = i + 1
    out

  # Writer.to_bytes provides the binary/Writer standard library operation.
  # @return Any the resulting value.
  to_bytes: ->
    bytes(self.parts)

  # Writer.write_bytes provides the binary/Writer standard library operation.
  # @param data Array data value.
  # @return Any the resulting value.
  write_bytes: data ->
    arr = bytes_array(data)
    i = 0
    while i < arr.len()
      self.parts.push(arr[i])
      i = i + 1
    self

  # Writer.write_f32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32: value ->
    self.write_bytes(binary_write_f32(value, self.endian))

  # Writer.write_f32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32_be: value ->
    self.write_bytes(binary_write_f32(value, "big"))

  # Writer.write_f32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32_le: value ->
    self.write_bytes(binary_write_f32(value, "little"))

  # Writer.write_f64 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64: value ->
    self.write_bytes(binary_write_f64(value, self.endian))

  # Writer.write_f64_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64_be: value ->
    self.write_bytes(binary_write_f64(value, "big"))

  # Writer.write_f64_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64_le: value ->
    self.write_bytes(binary_write_f64(value, "little"))

  # Writer.write_i16 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16: value ->
    self.write_int(value, 2, -32768, 32767, self.endian)

  # Writer.write_i16_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16_be: value ->
    self.write_int(value, 2, -32768, 32767, "big")

  # Writer.write_i16_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16_le: value ->
    self.write_int(value, 2, -32768, 32767, "little")

  # Writer.write_i32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32: value ->
    self.write_int(value, 4, -2147483648, 2147483647, self.endian)

  # Writer.write_i32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32_be: value ->
    self.write_int(value, 4, -2147483648, 2147483647, "big")

  # Writer.write_i32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32_le: value ->
    self.write_int(value, 4, -2147483648, 2147483647, "little")

  # Writer.write_i8 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i8: value ->
    self.write_int(value, 1, -128, 127, self.endian)

  # Writer.write_int provides the binary/Writer standard library operation.
  # @param value String value value.
  # @param width Int width value.
  # @param min Any min value.
  # @param max Any max value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  write_int: value, width, min, max, endian ->
    if value == nil or value.class != Number or value < min or value > max
      raise error("binary.Writer: integer out of range")
    if value < 0
      value = value + self.pow2(width * 8)
    arr = []
    i = 0
    while i < width
      shift = i
      if endian == "big"
        shift = width - i - 1
      divisor = self.pow256(shift)
      byte = value / divisor.to_i() % 256
      arr.push(byte)
      i = i + 1
    self.write_bytes(bytes(arr))

  # Writer.write_u16 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16: value ->
    self.write_int(value, 2, 0, 65535, self.endian)

  # Writer.write_u16_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16_be: value ->
    self.write_int(value, 2, 0, 65535, "big")

  # Writer.write_u16_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16_le: value ->
    self.write_int(value, 2, 0, 65535, "little")

  # Writer.write_u32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32: value ->
    self.write_int(value, 4, 0, 4294967295, self.endian)

  # Writer.write_u32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32_be: value ->
    self.write_int(value, 4, 0, 4294967295, "big")

  # Writer.write_u32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32_le: value ->
    self.write_int(value, 4, 0, 4294967295, "little")

  # Writer.write_u8 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u8: value ->
    self.write_int(value, 1, 0, 255, self.endian)

Instance Variables

endian

Writer.endian

lib/binary/writer.tya:5

Writer.endian stores instance state.

Source
  # Writer.endian stores instance state.
  # @type Nil
  endian: nil

parts

Writer.parts

lib/binary/writer.tya:9

Writer.parts stores instance state.

Source
  # Writer.parts stores instance state.
  # @type Array
  parts: []

Methods

bytes

Writer.bytes()

lib/binary/writer.tya:21

Writer.bytes provides the binary/Writer standard library operation.

Source
  # Writer.bytes provides the binary/Writer standard library operation.
  # @return Any the resulting value.
  bytes: ->
    self.to_bytes()

initialize

Writer.initialize(options, _unused)

lib/binary/writer.tya:15

Writer.initialize provides the binary/Writer standard library operation.

Source
  # Writer.initialize provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @param _unused Any  unused value.
  # @return Self the initialized object.
  initialize: options, _unused ->
    self.parts = []
    self.endian = self.option_endian(options)

new

Writer.new(options)

lib/binary/writer.tya:27

Writer.new provides the binary/Writer standard library operation.

Source
  # Writer.new provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @return Self the resulting value.
  new: options ->
    Writer(options, nil)

option_endian

Writer.option_endian(options)

lib/binary/writer.tya:33

Writer.option_endian provides the binary/Writer standard library operation.

Source
  # Writer.option_endian provides the binary/Writer standard library operation.
  # @param options Dict options value.
  # @return Any the resulting value.
  option_endian: options ->
    if options == nil
      return "big"
    if options.class != Dict
      raise error("binary.Writer: options must be a dict")
    if not options.has("endian")
      return "big"
    endian = options["endian"]
    if endian != "big" and endian != "little"
      raise error("binary.Writer: invalid endian")
    endian

position

Writer.position()

lib/binary/writer.tya:47

Writer.position provides the binary/Writer standard library operation.

Source
  # Writer.position provides the binary/Writer standard library operation.
  # @return Int the resulting value.
  position: ->
    self.parts.len()

pow2

Writer.pow2(n)

lib/binary/writer.tya:53

Writer.pow2 provides the binary/Writer standard library operation.

Source
  # Writer.pow2 provides the binary/Writer standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow2: n ->
    out = 1
    i = 0
    while i < n
      out = out * 2
      i = i + 1
    out

pow256

Writer.pow256(n)

lib/binary/writer.tya:64

Writer.pow256 provides the binary/Writer standard library operation.

Source
  # Writer.pow256 provides the binary/Writer standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow256: n ->
    out = 1
    i = 0
    while i < n
      out = out * 256
      i = i + 1
    out

to_bytes

Writer.to_bytes()

lib/binary/writer.tya:74

Writer.to_bytes provides the binary/Writer standard library operation.

Source
  # Writer.to_bytes provides the binary/Writer standard library operation.
  # @return Any the resulting value.
  to_bytes: ->
    bytes(self.parts)

write_bytes

Writer.write_bytes(data)

lib/binary/writer.tya:80

Writer.write_bytes provides the binary/Writer standard library operation.

Source
  # Writer.write_bytes provides the binary/Writer standard library operation.
  # @param data Array data value.
  # @return Any the resulting value.
  write_bytes: data ->
    arr = bytes_array(data)
    i = 0
    while i < arr.len()
      self.parts.push(arr[i])
      i = i + 1
    self

write_f32

Writer.write_f32(value)

lib/binary/writer.tya:91

Writer.write_f32 provides the binary/Writer standard library operation.

Source
  # Writer.write_f32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32: value ->
    self.write_bytes(binary_write_f32(value, self.endian))

write_f32_be

Writer.write_f32_be(value)

lib/binary/writer.tya:97

Writer.write_f32_be provides the binary/Writer standard library operation.

Source
  # Writer.write_f32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32_be: value ->
    self.write_bytes(binary_write_f32(value, "big"))

write_f32_le

Writer.write_f32_le(value)

lib/binary/writer.tya:103

Writer.write_f32_le provides the binary/Writer standard library operation.

Source
  # Writer.write_f32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f32_le: value ->
    self.write_bytes(binary_write_f32(value, "little"))

write_f64

Writer.write_f64(value)

lib/binary/writer.tya:109

Writer.write_f64 provides the binary/Writer standard library operation.

Source
  # Writer.write_f64 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64: value ->
    self.write_bytes(binary_write_f64(value, self.endian))

write_f64_be

Writer.write_f64_be(value)

lib/binary/writer.tya:115

Writer.write_f64_be provides the binary/Writer standard library operation.

Source
  # Writer.write_f64_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64_be: value ->
    self.write_bytes(binary_write_f64(value, "big"))

write_f64_le

Writer.write_f64_le(value)

lib/binary/writer.tya:121

Writer.write_f64_le provides the binary/Writer standard library operation.

Source
  # Writer.write_f64_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_f64_le: value ->
    self.write_bytes(binary_write_f64(value, "little"))

write_i16

Writer.write_i16(value)

lib/binary/writer.tya:127

Writer.write_i16 provides the binary/Writer standard library operation.

Source
  # Writer.write_i16 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16: value ->
    self.write_int(value, 2, -32768, 32767, self.endian)

write_i16_be

Writer.write_i16_be(value)

lib/binary/writer.tya:133

Writer.write_i16_be provides the binary/Writer standard library operation.

Source
  # Writer.write_i16_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16_be: value ->
    self.write_int(value, 2, -32768, 32767, "big")

write_i16_le

Writer.write_i16_le(value)

lib/binary/writer.tya:139

Writer.write_i16_le provides the binary/Writer standard library operation.

Source
  # Writer.write_i16_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i16_le: value ->
    self.write_int(value, 2, -32768, 32767, "little")

write_i32

Writer.write_i32(value)

lib/binary/writer.tya:145

Writer.write_i32 provides the binary/Writer standard library operation.

Source
  # Writer.write_i32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32: value ->
    self.write_int(value, 4, -2147483648, 2147483647, self.endian)

write_i32_be

Writer.write_i32_be(value)

lib/binary/writer.tya:151

Writer.write_i32_be provides the binary/Writer standard library operation.

Source
  # Writer.write_i32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32_be: value ->
    self.write_int(value, 4, -2147483648, 2147483647, "big")

write_i32_le

Writer.write_i32_le(value)

lib/binary/writer.tya:157

Writer.write_i32_le provides the binary/Writer standard library operation.

Source
  # Writer.write_i32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i32_le: value ->
    self.write_int(value, 4, -2147483648, 2147483647, "little")

write_i8

Writer.write_i8(value)

lib/binary/writer.tya:163

Writer.write_i8 provides the binary/Writer standard library operation.

Source
  # Writer.write_i8 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_i8: value ->
    self.write_int(value, 1, -128, 127, self.endian)

write_int

Writer.write_int(value, width, min, max, endian)

lib/binary/writer.tya:173

Writer.write_int provides the binary/Writer standard library operation.

Source
  # Writer.write_int provides the binary/Writer standard library operation.
  # @param value String value value.
  # @param width Int width value.
  # @param min Any min value.
  # @param max Any max value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  write_int: value, width, min, max, endian ->
    if value == nil or value.class != Number or value < min or value > max
      raise error("binary.Writer: integer out of range")
    if value < 0
      value = value + self.pow2(width * 8)
    arr = []
    i = 0
    while i < width
      shift = i
      if endian == "big"
        shift = width - i - 1
      divisor = self.pow256(shift)
      byte = value / divisor.to_i() % 256
      arr.push(byte)
      i = i + 1
    self.write_bytes(bytes(arr))

write_u16

Writer.write_u16(value)

lib/binary/writer.tya:193

Writer.write_u16 provides the binary/Writer standard library operation.

Source
  # Writer.write_u16 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16: value ->
    self.write_int(value, 2, 0, 65535, self.endian)

write_u16_be

Writer.write_u16_be(value)

lib/binary/writer.tya:199

Writer.write_u16_be provides the binary/Writer standard library operation.

Source
  # Writer.write_u16_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16_be: value ->
    self.write_int(value, 2, 0, 65535, "big")

write_u16_le

Writer.write_u16_le(value)

lib/binary/writer.tya:205

Writer.write_u16_le provides the binary/Writer standard library operation.

Source
  # Writer.write_u16_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u16_le: value ->
    self.write_int(value, 2, 0, 65535, "little")

write_u32

Writer.write_u32(value)

lib/binary/writer.tya:211

Writer.write_u32 provides the binary/Writer standard library operation.

Source
  # Writer.write_u32 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32: value ->
    self.write_int(value, 4, 0, 4294967295, self.endian)

write_u32_be

Writer.write_u32_be(value)

lib/binary/writer.tya:217

Writer.write_u32_be provides the binary/Writer standard library operation.

Source
  # Writer.write_u32_be provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32_be: value ->
    self.write_int(value, 4, 0, 4294967295, "big")

write_u32_le

Writer.write_u32_le(value)

lib/binary/writer.tya:223

Writer.write_u32_le provides the binary/Writer standard library operation.

Source
  # Writer.write_u32_le provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u32_le: value ->
    self.write_int(value, 4, 0, 4294967295, "little")

write_u8

Writer.write_u8(value)

lib/binary/writer.tya:229

Writer.write_u8 provides the binary/Writer standard library operation.

Source
  # Writer.write_u8 provides the binary/Writer standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  write_u8: value ->
    self.write_int(value, 1, 0, 255, self.endian)