Class: BRS::Stream::Reader

Inherits:
Abstract show all
Includes:
ReaderHelpers
Defined in:
lib/brs/stream/reader.rb

Constant Summary

Constants included from Delegates

Delegates::DELEGATES

Instance Attribute Summary collapse

Attributes inherited from Abstract

#external_encoding, #internal_encoding, #io, #pos, #stat, #transcode_options

Instance Method Summary collapse

Methods included from ReaderHelpers

#each_byte, #each_char, #each_line, #getbyte, #getc, #gets, included, #readbyte, #readchar, #readline, #readlines, #ungetbyte, #ungetc, #ungetline

Methods inherited from Abstract

#advise, #closed?, #set_encoding, #to_io

Methods included from Delegates

included

Constructor Details

#initialize(source_io, options = {}, *args) ⇒ Reader

Returns a new instance of Reader.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/brs/stream/reader.rb', line 16

def initialize(source_io, options = {}, *args)
  @options = options

  super source_io, *args

  initialize_source_buffer_length
  reset_io_remainder
  reset_need_to_flush

  @lineno = 0
end

Instance Attribute Details

#linenoObject

Returns the value of attribute lineno.



14
15
16
# File 'lib/brs/stream/reader.rb', line 14

def lineno
  @lineno
end

Instance Method Details

#closeObject



85
86
87
88
89
# File 'lib/brs/stream/reader.rb', line 85

def close
  raw_wrapper :close

  super
end

#eof?Boolean

Returns:

  • (Boolean)

Raises:



91
92
93
94
95
# File 'lib/brs/stream/reader.rb', line 91

def eof?
  raise ValidateError, "io should be responsible to eof" unless @io.respond_to? :eof?

  empty? && @io.eof?
end

#read(bytes_to_read = nil, out_buffer = nil) ⇒ Object

– synchronous –

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brs/stream/reader.rb', line 53

def read(bytes_to_read = nil, out_buffer = nil)
  Validation.validate_not_negative_integer bytes_to_read unless bytes_to_read.nil?
  Validation.validate_string out_buffer unless out_buffer.nil?

  raise ValidateError, "io should be responsible to read and eof" unless
    @io.respond_to?(:read) && @io.respond_to?(:eof?)

  unless bytes_to_read.nil?
    return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
    return nil if eof?

    append_io_data @io.read(@source_buffer_length) while @buffer.bytesize < bytes_to_read && !@io.eof?
    flush_io_data if @buffer.bytesize < bytes_to_read

    return read_bytes_from_buffer bytes_to_read, out_buffer
  end

  append_io_data @io.read(@source_buffer_length) until @io.eof?
  flush_io_data

  read_buffer out_buffer
end

#read_nonblock(bytes_to_read, out_buffer = nil, *options) ⇒ Object

Raises:



105
106
107
108
109
# File 'lib/brs/stream/reader.rb', line 105

def read_nonblock(bytes_to_read, out_buffer = nil, *options)
  raise ValidateError, "io should be responsible to read nonblock" unless @io.respond_to? :read_nonblock

  read_more_nonblock(bytes_to_read, out_buffer) { @io.read_nonblock(@source_buffer_length, *options) }
end

#readpartial(bytes_to_read, out_buffer = nil) ⇒ Object

– asynchronous –

Raises:



99
100
101
102
103
# File 'lib/brs/stream/reader.rb', line 99

def readpartial(bytes_to_read, out_buffer = nil)
  raise ValidateError, "io should be responsible to readpartial" unless @io.respond_to? :readpartial

  read_more_nonblock(bytes_to_read, out_buffer) { @io.readpartial @source_buffer_length }
end

#rewindObject



76
77
78
79
80
81
82
83
# File 'lib/brs/stream/reader.rb', line 76

def rewind
  raw_wrapper :close

  reset_io_remainder
  reset_need_to_flush

  super
end