diff --git a/CHANGELOG.md b/CHANGELOG.md index b5bc7ae..480a441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Change Log +### v0.3.5 + +- Fix the issue that StreamWriter will read more bytes than wanted + ### v0.3.4 - Fix handling gzip/deflate response diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb index 4fe2496..79271b8 100644 --- a/lib/aliyun/oss/http.rb +++ b/lib/aliyun/oss/http.rb @@ -52,8 +52,13 @@ def read(bytes = nil, outbuf = nil) ret = "" loop do if bytes - ret << @buffer.slice!(0, bytes) - break if ret.size >= bytes + fail if bytes < 0 + piece = @buffer.slice!(0, bytes) + if piece + ret << piece + bytes -= piece.size + break if bytes == 0 + end else ret << @buffer @buffer.clear diff --git a/lib/aliyun/version.rb b/lib/aliyun/version.rb index 046e387..39f855e 100644 --- a/lib/aliyun/version.rb +++ b/lib/aliyun/version.rb @@ -2,6 +2,6 @@ module Aliyun - VERSION = "0.3.4" + VERSION = "0.3.5" end # Aliyun diff --git a/spec/aliyun/oss/http_spec.rb b/spec/aliyun/oss/http_spec.rb index 923e764..4452f21 100644 --- a/spec/aliyun/oss/http_spec.rb +++ b/spec/aliyun/oss/http_spec.rb @@ -58,6 +58,24 @@ module OSS r = s.read expect(r).to eq('') end + + it "should read exactly bytes" do + s = HTTP::StreamWriter.new do |sr| + 100.times { sr << 'x' * 10 } + end + + r = s.read(11) + expect(r.size).to eq(11) + + r = s.read(25) + expect(r.size).to eq(25) + + r = s.read(900) + expect(r.size).to eq(900) + + r = s.read(1000) + expect(r.size).to eq(64) + end end # StreamWriter end # HTTP