Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/nio/bytebuffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ByteBuffer
include Enumerable

attr_reader :position, :limit, :capacity
alias size capacity

# Insufficient capacity in buffer
OverflowError = Class.new(IOError)
Expand Down Expand Up @@ -53,7 +54,7 @@ def clear
# @raise [ArgumentError] new position was invalid
def position=(new_position)
raise ArgumentError, "negative position given" if new_position < 0
raise ArgumentError, "specified position exceeds capacity" if new_position > @capacity
raise ArgumentError, "specified position exceeds limit" if new_position > @limit

@mark = nil if @mark && @mark > new_position
@position = new_position
Expand Down Expand Up @@ -99,7 +100,7 @@ def get(length = remaining)
raise ArgumentError, "negative length given" if length < 0
raise UnderflowError, "not enough data in buffer" if length > @limit - @position

result = @buffer[@position...length]
result = @buffer[@position, length]
@position += length
result
end
Expand Down Expand Up @@ -127,12 +128,13 @@ def [](index)
def put(str)
raise TypeError, "expected String, got #{str.class}" unless str.respond_to?(:to_str)

str = str.to_str
str = str.to_str.b

raise OverflowError, "buffer is full" if str.length > @limit - @position
length = str.bytesize
raise OverflowError, "buffer is full" if length > @limit - @position

@buffer[@position...str.length] = str
@position += str.length
@buffer[@position, length] = str
@position += length
self
end
alias << put
Expand Down