class Vertx::Buffer
A Buffer
represents a sequence of zero or more bytes that can be written to or read from, and which expands as necessary to accomodate any bytes written to it.
Buffers are used in many places in vert.x, for example to read/write data to/from {NetSocket}, {AsyncFile}, {WebSocket}, {HttpClientRequest}, {HttpClientResponse}, {HttpServerRequest}, {HttpServerResponse} etc.
There are two ways to write data to a Buffer: The first method involves methods that take the form set_XXX. These methods write data into the buffer starting at the specified position. The position does not have to be inside data that has already been written to the buffer; the buffer will automatically expand to encompass the position plus any data that needs to be written. All positions are measured in bytes and start with zero.
The second method involves methods that take the form append-XXX; these methods append data at the end of the buffer. Methods exist to both set and append all primitive types, String and other instances of Buffer
.
Data can be read from a buffer by invoking methods which take the form get_XXX. These methods take a parameter representing the position in the Buffer
from where to read data.
@author {tfox.org Tim Fox}
Public Class Methods
Creates a new empty buffer. The {#length} of the buffer immediately after creation will be zero. @param initial_size_hint [FixNum] is a hint to the system for how much memory to initially allocate to the buffer to prevent excessive automatic re-allocations as data is written to it.
# File lib/vertx/buffer.rb, line 47 def Buffer.create(initial_size_hint = 0) Buffer.new(org.vertx.java.core.buffer.Buffer.new(initial_size_hint)) end
@private
# File lib/vertx/buffer.rb, line 40 def initialize(j_buffer) @buffer = j_buffer end
Public Instance Methods
@private
# File lib/vertx/buffer.rb, line 246 def _to_java_buffer @buffer end
Appends another buffer to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. @param buff [Buffer] the buffer to append. @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 128 def append_buffer(buff) @buffer.appendBuffer(buff._to_java_buffer) self end
Appends a FixNum to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. @param num [FixNum] the fixnum to append. @param bytes [FixNum] the number of bytes to write in the buffer to represent the fixnum. Valid values are 1, 2, 4 and 8. @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 137 def append_fixnum(num, bytes) case bytes when 1 @buffer.appendByte(num) when 2 @buffer.appendShort(num) when 4 @buffer.appendInt(num) when 8 @buffer.appendLong(num) else raise "bytes must be 1, 2, 4, or 8" end self end
Appends a Float to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. @param num [Float] the float to append. @param bytes [FixNum] the number of bytes to write in the buffer to represent the float. Valid values are 4 and 8. @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 157 def append_float(num, bytes) case bytes when 4 @buffer.appendFloat(num) when 8 @buffer.appendDouble(num) else raise "bytes must be 4 or 8" end end
Appends a string to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written. @param str [String] the string to append. @param enc [String] the encoding to use. Defaults to “UTF-8” @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 172 def append_str(str, enc = "UTF-8") @buffer.appendString(str, enc) self end
Get a copy of the entire buffer. @return [Buffer] the copy
# File lib/vertx/buffer.rb, line 241 def copy Buffer.new(@buffer.copy()) end
Return bytes in the buffer as a Buffer
@param start_pos [FixNum] - the position in this buffer from where to start the copy. @param end_pos [FixNum] - the copy will be made up to index end_pos - 1 @return [Buffer] the copy
# File lib/vertx/buffer.rb, line 119 def get_buffer(pos, end_pos) j_buff = @buffer.getBuffer(pos, end_pos) Buffer.new(j_buff) end
Get the byte at position pos in the buffer. @param pos [FixNum] the position in the buffer from where to retrieve the byte @return [FixNum] the byte
# File lib/vertx/buffer.rb, line 68 def get_byte(pos) @buffer.getByte(pos) end
Get the FixNum represented by a sequence of bytes starting at position pos in the buffer. @param pos [FixNum] the position in the buffer from where to retrieve the FixNum. @param bytes [FixNum] the number of bytes to retrieve from position pos to create the FixNum. Valid values are 1, 2, 4 and 8. @return [FixNum] the FixNum
# File lib/vertx/buffer.rb, line 76 def get_fixnum(pos, bytes) case bytes when 1 @buffer.getByte(pos) when 2 @buffer.getShort(pos) when 4 @buffer.getInt(pos) when 8 @buffer.getLong(pos) else raise "bytes must be 1, 2, 4, or 8" end end
Get the Float represented by a sequence of bytes starting at position pos in the buffer. @param pos [Float] the position in the buffer from where to retrieve the Float. @param bytes [Float] the number of bytes to retrieve from position pos to create the Float. Valid values are 4 and 8. @return [Float] the Float
# File lib/vertx/buffer.rb, line 95 def get_float(pos, bytes) case bytes when 4 @buffer.getFloat(pos) when 8 @buffer.getDouble(pos) else raise "bytes must be 4 or 8" end end
Return bytes from the buffer interpreted as a String @param pos [FixNum] the position in the buffer from where to start reading @param end_pos [FixNum] the position in the buffer to end reading @param enc [String] The encoding to use @return [String] the String
# File lib/vertx/buffer.rb, line 111 def get_string(pos, end_pos, enc = 'UTF-8') @buffer.getString(pos, end_pos, enc) end
@return [FixNum] the length of this buffer, in bytes.
# File lib/vertx/buffer.rb, line 235 def length @buffer.length end
Sets bytes in this buffer to the bytes of the specified buffer. The buffer will expand as necessary to accomodate any bytes written. @param pos [FixNum] - the position in this buffer from where to start writing the buffer @param buff [Buffer] - the buffer to write into this buffer @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 219 def set_buffer(pos, buff) @buffer.setBytes(pos, buff._to_java_buffer) self end
Sets bytes in the buffer to a representation of a FixNum. The buffer will expand as necessary to accomodate any bytes written. @param pos [FixNum] - the position in the buffer from where to start writing the FixNum @param num [FixNum] - the FixNum to write @param bytes [FixNum] - the number of bytes to write to represent the FixNum. Valid values are 1, 2, 4, and 8 @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 182 def set_fixnum(pos, num, bytes) case bytes when 1 @buffer.setByte(pos, num) when 2 @buffer.setShort(pos, num) when 4 @buffer.setInt(pos, num) when 8 @buffer.setLong(pos, num) else raise "bytes must be 1, 2, 4, or 8" end self end
Sets bytes in the buffer to a representation of a Float. The buffer will expand as necessary to accomodate any bytes written. @param pos [FixNum] - the position in the buffer from where to start writing the Float @param num [Float] - the Float to write @param bytes [FixNum] - the number of bytes to write to represent the Float. Valid values are 4 and 8 @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 203 def set_float(pos, num, bytes) case bytes when 4 @buffer.setFloat(pos, num) when 8 @buffer.setDouble(pos, num) else raise "bytes must be 4 or 8" end self end
Set bytes in the buffer to the string encoding in the specified encoding @param pos [FixNum] - the position in this buffer from where to start writing the string @param str [String] the string @param enc [String] the encoding @return [Buffer] a reference to self so multiple operations can be appended together.
# File lib/vertx/buffer.rb, line 229 def set_string(pos, str, enc = 'UTF-8') @buffer.setString(pos, str, enc) self end
Return a String representation of the buffer. @param enc [String] The encoding to use. Defaults to “UTF-8” @return [String] a String representation of the buffer.
# File lib/vertx/buffer.rb, line 61 def to_s(enc = "UTF-8") @buffer.toString(enc) end