Class: Primate::Readable

Inherits:
Object
  • Object
show all
Defined in:
lib/primate/readable.rb,
sig/primate.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(typed_array, type = nil) ⇒ Readable

Returns a new instance of Readable.

Parameters:

  • typed_array (Object)
  • content_type (String, nil)


7
8
9
10
11
12
# File 'lib/primate/readable.rb', line 7

def initialize(typed_array, type = nil)
  @ta = typed_array
  @type = type
  @pos = 0
  @size = @ta[:length].to_i
end

Instance Attribute Details

#content_type ⇒ String? (readonly)

Returns the value of attribute content_type.

Returns:

  • (String, nil)


23
24
25
# File 'sig/primate.rbs', line 23

def content_type
  @content_type
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/primate/readable.rb', line 5

def type
  @type
end

Instance Method Details

#bytes(n = nil) ⇒ Array[Integer]

return an array of bytes (integers), advance position

Parameters:

  • n (Integer, nil) (defaults to: nil)

Returns:

  • (Array[Integer])


28
29
30
31
32
33
34
# File 'lib/primate/readable.rb', line 28

def bytes(n = nil)
  return [] if eof?
  n = n ? [n, @size - @pos].min : (@size - @pos)
  arr = Array.new(n) { |i| @ta[@pos + i].to_i }
  @pos += n
  arr
end

#each_chunk(chunk) ⇒ self #each_chunk(chunk) ⇒ Enumerator[String, self]

enumerate binary string chunks starting at current position (no rewind)

Overloads:

  • #each_chunk(chunk) ⇒ self

    Parameters:

    • chunk (Integer)

    Returns:

    • (self)
  • #each_chunk(chunk) ⇒ Enumerator[String, self]

    Parameters:

    • chunk (Integer)

    Returns:

    • (Enumerator[String, self])

Yields:

Yield Parameters:

  • arg0 (String)

Yield Returns:

  • (void)


49
50
51
52
53
54
55
56
57
58
# File 'lib/primate/readable.rb', line 49

def each_chunk(chunk = 64 * 1024)
  return enum_for(:each_chunk, chunk) unless block_given?
  off = @pos
  while off < @size
    n = [chunk, @size - off].min
    yield pack_range(off, n)
    off += n
  end
  self
end

#eof? ⇒ Boolean

Returns:

  • (Boolean)


15
# File 'lib/primate/readable.rb', line 15

def eof? = @pos >= @size

#head(n = 4) ⇒ Array[Integer]

first n bytes from the beginning (integers), does not affect position

Parameters:

  • n (Integer) (defaults to: 4)

Returns:

  • (Array[Integer])


43
44
45
46
# File 'lib/primate/readable.rb', line 43

def head(n = 4)
  n = [n, @size].min
  Array.new(n) { |i| @ta[i].to_i }
end

#peek(n) ⇒ Array[Integer]

look ahead without advancing position (integers)

Parameters:

  • n (Integer)

Returns:

  • (Array[Integer])


37
38
39
40
# File 'lib/primate/readable.rb', line 37

def peek(n)
  n = [n, @size - @pos].min
  Array.new(n) { |i| @ta[@pos + i].to_i }
end

#read(n = nil) ⇒ String

return a binary string (ASCII-8BIT), advance position

Parameters:

  • n (Integer, nil) (defaults to: nil)

Returns:

  • (String)


19
20
21
22
23
24
25
# File 'lib/primate/readable.rb', line 19

def read(n = nil)
  return ''.b if eof?
  n = n ? [n, @size - @pos].min : (@size - @pos)
  str = pack_range(@pos, n)
  @pos += n
  str
end

#rewind ⇒ self

Returns:

  • (self)


16
# File 'lib/primate/readable.rb', line 16

def rewind; @pos = 0; self; end

#size ⇒ Integer

Returns:

  • (Integer)


14
# File 'lib/primate/readable.rb', line 14

def size = @size