Class: BinaryParser::BufferedStream

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_parser/general_class/buffered_stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream, buffer_size) ⇒ BufferedStream

Returns a new instance of BufferedStream.



5
6
7
# File 'lib/binary_parser/general_class/buffered_stream.rb', line 5

def initialize(stream, buffer_size)
  @stream, @buffer_size = stream, buffer_size
end

Instance Method Details

#closeObject



17
18
19
# File 'lib/binary_parser/general_class/buffered_stream.rb', line 17

def close
  @stream.close
end

#read(length) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/binary_parser/general_class/buffered_stream.rb', line 9

def read(length)
  if !@buffer || @buffer.eof?
    return nil unless next_buffer = @stream.read(@buffer_size)
    @buffer = StringIO.new(next_buffer)
  end
  return @buffer.read(length)
end