mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
updated IOBuffer impl to use native Java ByteArrayOutputStream; added test
This commit is contained in:
parent
48593aeb2b
commit
9b9b2f8097
2 changed files with 56 additions and 6 deletions
|
@ -1,23 +1,35 @@
|
|||
require 'java'
|
||||
|
||||
# Conservative native JRuby/Java implementation of IOBuffer
|
||||
# backed by a ByteArrayOutputStream and conversion between
|
||||
# Ruby String and Java bytes
|
||||
module Puma
|
||||
class JavaIOBuffer < java.io.ByteArrayOutputStream
|
||||
field_reader :buf
|
||||
end
|
||||
|
||||
class IOBuffer
|
||||
BUF_DEFAULT_SIZE = 4096
|
||||
|
||||
def initialize
|
||||
@buf = ""
|
||||
@buf = JavaIOBuffer.new(BUF_DEFAULT_SIZE)
|
||||
end
|
||||
|
||||
def reset
|
||||
@buf = ""
|
||||
@buf.reset
|
||||
end
|
||||
|
||||
def <<(str)
|
||||
@buf << str
|
||||
bytes = str.to_java_bytes
|
||||
@buf.write(bytes, 0, bytes.length)
|
||||
end
|
||||
|
||||
def append(*strs)
|
||||
strs.each { |s| @buf << s }
|
||||
strs.each { |s| self << s; }
|
||||
end
|
||||
|
||||
def to_s
|
||||
@buf
|
||||
String.from_java_bytes @buf.to_byte_array
|
||||
end
|
||||
|
||||
alias_method :to_str, :to_s
|
||||
|
@ -27,7 +39,7 @@ module Puma
|
|||
end
|
||||
|
||||
def capacity
|
||||
@buf.size
|
||||
@buf.buf.length
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
38
test/test_iobuffer.rb
Normal file
38
test/test_iobuffer.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require 'puma/io_buffer'
|
||||
require 'test/unit'
|
||||
|
||||
class TestIOBuffer < Test::Unit::TestCase
|
||||
attr_accessor :iobuf
|
||||
def setup
|
||||
self.iobuf = Puma::IOBuffer.new
|
||||
end
|
||||
|
||||
def test_initial_size
|
||||
assert_equal 0, iobuf.used
|
||||
assert iobuf.capacity > 0
|
||||
end
|
||||
|
||||
def test_append_op
|
||||
iobuf << "abc"
|
||||
assert_equal "abc", iobuf.to_s
|
||||
iobuf << "123"
|
||||
assert_equal "abc123", iobuf.to_s
|
||||
assert_equal 6, iobuf.used
|
||||
end
|
||||
|
||||
def test_append
|
||||
expected = "mary had a little lamb"
|
||||
iobuf.append("mary", " ", "had ", "a little", " lamb")
|
||||
assert_equal expected, iobuf.to_s
|
||||
assert_equal expected.length, iobuf.used
|
||||
end
|
||||
|
||||
def test_reset
|
||||
iobuf << "content"
|
||||
assert_equal "content", iobuf.to_s
|
||||
iobuf.reset
|
||||
assert_equal 0, iobuf.used
|
||||
assert_equal "", iobuf.to_s
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue