Class: Rack::Multipart::Parser::BoundedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multipart/parser.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(io, content_length) ⇒ BoundedIO

Returns a new instance of BoundedIO.


52
53
54
55
56
# File 'lib/rack/multipart/parser.rb', line 52

def initialize(io, content_length)
  @io             = io
  @content_length = content_length
  @cursor = 0
end

Instance Method Details

#read(size, outbuf = nil) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rack/multipart/parser.rb', line 58

def read(size, outbuf = nil)
  return if @cursor >= @content_length

  left = @content_length - @cursor

  str = if left < size
          @io.read left, outbuf
        else
          @io.read size, outbuf
        end

  if str
    @cursor += str.bytesize
  else
    # Raise an error for mismatching content-length and actual contents
    raise EOFError, "bad content body"
  end

  str
end