Class: S3io::ReadWrapper
- Includes:
- Enumerable
- Defined in:
- lib/s3io/read_wrapper.rb
Constant Summary collapse
- LINE_BUFFER_SIZE =
Default buffer size for line parser in bytes
5 * 1024 * 1024
Instance Attribute Summary collapse
-
#pos ⇒ Object
Current byte position in S3 object.
Instance Method Summary collapse
-
#each(separator = $/) {|line| ... } ⇒ Object
(also: #lines, #each_line)
Iterates over S3 object lines.
- #eof? ⇒ Boolean
-
#initialize(s3object, options = {}) ⇒ ReadWrapper
constructor
A new instance of ReadWrapper.
-
#read(bytes = nil) ⇒ Object
Reads data from S3 object.
-
#rewind ⇒ Object
Rewinds position to the very beginning of S3 object.
Methods inherited from Wrapper
Constructor Details
#initialize(s3object, options = {}) ⇒ ReadWrapper
Returns a new instance of ReadWrapper.
21 22 23 24 25 26 27 28 |
# File 'lib/s3io/read_wrapper.rb', line 21 def initialize(s3object, = {}) super(s3object) = { :line_buffer_size => ([:line_buffer_size] || LINE_BUFFER_SIZE) } @last_modified = @s3object.last_modified end |
Instance Attribute Details
#pos ⇒ Object
Current byte position in S3 object
19 20 21 |
# File 'lib/s3io/read_wrapper.rb', line 19 def pos @pos end |
Instance Method Details
#each(separator = $/) {|line| ... } ⇒ Object Also known as: lines, each_line
Iterates over S3 object lines.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/s3io/read_wrapper.rb', line 67 def each(separator = $/) return enum_for(:each, separator) unless block_given? line = '' newline_pos = nil # Either trying to parse the remainder or reading some more data while newline_pos || !(buffer = read([:line_buffer_size])).empty? prev_newline_pos = newline_pos || 0 newline_pos = buffer.index(separator, prev_newline_pos) if newline_pos line << buffer[prev_newline_pos..newline_pos] newline_pos += 1 yield line line = '' else line << buffer[prev_newline_pos..-1] end end # Flush the remainder if body doesn't end with separator yield line unless line.empty? return self end |
#eof? ⇒ Boolean
55 56 57 |
# File 'lib/s3io/read_wrapper.rb', line 55 def eof? @pos >= @s3object.content_length end |
#read(bytes = nil) ⇒ Object
Reads data from S3 object.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/s3io/read_wrapper.rb', line 33 def read(bytes = nil) content_length = @s3object.content_length return '' if (@pos >= content_length) || (bytes == 0) bytes ||= content_length upper_bound = @pos + bytes - 1 upper_bound = (content_length - 1) if upper_bound >= content_length data = @s3object.read :range => @pos..upper_bound last_modified = @s3object.last_modified unless last_modified == @last_modified fail ReadModifiedError, "S3 object #{@s3object.key} was updated during read, last_modified=#{last_modified.to_s} (was #{@last_modified.to_s})" end @pos = upper_bound + 1 return data end |
#rewind ⇒ Object
Rewinds position to the very beginning of S3 object.
60 61 62 |
# File 'lib/s3io/read_wrapper.rb', line 60 def rewind @pos = 0 end |