Class: BufferedIO::ReadBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/buffered_io/read_buffer.rb

Overview

Maintains a read buffer for an IO object.

Constant Summary collapse

DEFAULT_BUFFER_SIZE =

:nodoc:

1024 * 16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ReadBuffer

Returns a new instance of ReadBuffer.



7
8
9
10
11
12
# File 'lib/buffered_io/read_buffer.rb', line 7

def initialize(io)
  @io = io
  @buffer = ''
  @read_timeout = 60
  @buffer_size = DEFAULT_BUFFER_SIZE
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



14
15
16
# File 'lib/buffered_io/read_buffer.rb', line 14

def io
  @io
end

#read_timeoutObject

Returns the value of attribute read_timeout.



15
16
17
# File 'lib/buffered_io/read_buffer.rb', line 15

def read_timeout
  @read_timeout
end

Instance Method Details

#consume(len) ⇒ Object

Consume bytes from the IOBuffer



29
30
31
# File 'lib/buffered_io/read_buffer.rb', line 29

def consume(len)
  return @buffer.slice!(0, len)
end

#fillObject

Attempt to fill the IOBuffer



18
19
20
21
22
23
24
25
26
# File 'lib/buffered_io/read_buffer.rb', line 18

def fill
  begin
    @buffer << @io.read_nonblock(@buffer_size)
  rescue ::IO::WaitReadable
    IO.select([@io], nil, nil, @read_timeout) ? retry : (raise ::Timeout::Error)
  rescue ::IO::WaitWritable
    IO.select(nil, [@io], nil, @read_timeout) ? retry : (raise ::Timeout::Error)
  end
end

#index(i) ⇒ Object



38
39
40
# File 'lib/buffered_io/read_buffer.rb', line 38

def index(i)
  @buffer.index(i)
end

#sizeObject

Number of bytes in the buffer



34
35
36
# File 'lib/buffered_io/read_buffer.rb', line 34

def size
  @buffer.size
end