Class: LimitedIO

Inherits:
Struct
  • Object
show all
Defined in:
lib/protocol_buffers/limited_io.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#limitObject

Returns the value of attribute limit

Returns:

  • (Object)

    the current value of limit



1
2
3
# File 'lib/protocol_buffers/limited_io.rb', line 1

def limit
  @limit
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



1
2
3
# File 'lib/protocol_buffers/limited_io.rb', line 1

def parent
  @parent
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/protocol_buffers/limited_io.rb', line 16

def eof?
  limit == 0 || parent.eof?
end

#getbyteObject



20
21
22
23
24
# File 'lib/protocol_buffers/limited_io.rb', line 20

def getbyte
  return nil if limit == 0
  self.limit -= 1
  parent.getbyte
end

#read(length = nil, buffer = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/protocol_buffers/limited_io.rb', line 2

def read(length = nil, buffer = nil)
  length = length || limit
  length = limit if length > limit
  self.limit -= length
  # seems silly to check for buffer, but some IO#read methods implemented in C
  # barf if you pass nil, rather than treat it as an argument that wasn't
  # passed at all.
  if buffer
    parent.read(length, buffer)
  else
    parent.read(length)
  end
end