lib/s3io/read_wrapper.rb in s3io-1.1.1 vs lib/s3io/read_wrapper.rb in s3io-1.1.4
- old
+ new
@@ -28,34 +28,34 @@
end
# Reads data from S3 object.
#
# @param [Integer] bytes number of bytes to read
- def read(bytes = nil)
- content_length = @s3object.content_length
+ def read(bytes = nil, outbuf = nil)
+ @content_length ||= @s3object.content_length
- return '' if (@pos >= content_length) || (bytes == 0)
+ return '' if (@pos >= @content_length) || (bytes == 0)
- bytes ||= content_length
+ bytes ||= @content_length
upper_bound = @pos + bytes - 1
- upper_bound = (content_length - 1) if upper_bound >= content_length
+ upper_bound = (@content_length - 1) if upper_bound >= @content_length
- data = @s3object.read :range => @pos..upper_bound
+ data = @s3object.read :range => @pos..upper_bound, :if_unmodified_since => @last_modified
- 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
+ outbuf.replace data if outbuf
+
return data
+
+ rescue ::AWS::S3::Errors::PreconditionFailed
+ fail ReadModifiedError, "S3 object #{@s3object.key} was updated during read (modified since #{@last_modified.to_s})"
end
def eof?
- @pos >= @s3object.content_length
+ (@content_length = @s3object.content_length) && (@pos >= @content_length)
end
# Rewinds position to the very beginning of S3 object.
def rewind
@pos = 0
@@ -90,7 +90,17 @@
return self
end
alias lines each
alias each_line each
+
+ # Returns one string at a time.
+ #
+ # @param [String] separator line separator string
+ def gets(separator = $/)
+ @_gets ||= enum_for(:each, separator)
+ @_gets.next
+ rescue StopIteration
+ nil
+ end
end
end