Class: ADSP::Stream::Reader

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

Overview

ADSP::Stream::Reader class.

Direct Known Subclasses

Test::Mock::Stream::Reader

Constant Summary collapse

DEFAULT_SOURCE_BUFFER_LENGTH =

Default source buffer length.

1 << 18
RawDecompressor =

Current raw stream class.

Raw::Decompressor

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

Initializes stream using source_io native stream and options. Option: :external_encoding encoding name for destination data. Option: :internal_encoding encoding name for source data. Option: :transcode_options transcode options for data.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adsp/stream/reader.rb', line 28

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

Current line for source data.



22
23
24
# File 'lib/adsp/stream/reader.rb', line 22

def lineno
  @lineno
end

Instance Method Details

#closeObject

Closes stream.



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

def close
  raw_wrapper :close

  super
end

#eof?Boolean

Returns whether we are at the end of stream.

Returns:

  • (Boolean)

Raises:



112
113
114
115
116
# File 'lib/adsp/stream/reader.rb', line 112

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

Reads bytes_to_read bytes from stream. If out_buffer is defined than it will be used as output destination.

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/adsp/stream/reader.rb', line 71

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

Reads bytes_to_read bytes from stream. If out_buffer is defined than it will be used as output destination. options will be passed to native stream.

Raises:



132
133
134
135
136
# File 'lib/adsp/stream/reader.rb', line 132

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

Reads bytes_to_read bytes from stream. If out_buffer is defined than it will be used as output destination. Raises ::EOFError when no data available.

Raises:



123
124
125
126
127
# File 'lib/adsp/stream/reader.rb', line 123

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

Resets stream.



95
96
97
98
99
100
101
102
# File 'lib/adsp/stream/reader.rb', line 95

def rewind
  raw_wrapper :close

  reset_io_remainder
  reset_need_to_flush

  super
end