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)


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

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

#getbyteObject



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

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

#getcObject



27
28
29
30
31
# File 'lib/protocol_buffers/limited_io.rb', line 27

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

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



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

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