Class List Method List File List

# Class: Vertx::Buffer

Inherits:
  Object
  - Object
  - Vertx::Buffer
  show all
Defined in:
  src/main/ruby_scripts/core/buffer.rb

## Overview

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:

- Tim Fox

## Class Method Summary (collapse)

- + (Object) create(initial_size_hint = 0)
  Creates a new empty buffer.
- + (Object) create_from_str(str, enc = "UTF-8")
  Create a new Buffer from a String.

## Instance Method Summary (collapse)

- - (Buffer) append_buffer(buff)
  Appends a buffer to the end of this buffer.
- - (Buffer) append_fixnum(num, bytes)
  Appends a fixnum to the end of this buffer.
- - (Buffer) append_float(num, bytes)
  Appends a float to the end of this buffer.
- - (Buffer) append_str(str, enc = "UTF-8")
  Appends a string to the end of this buffer.
- - (Buffer) copy
  Get a copy of the entire buffer.
- - (Buffer) get_buffer(pos, end_pos)
  Return bytes in the buffer as a Buffer.
- - (FixNum) get_byte(pos)
  Get the byte at position pos in the buffer.
- - (FixNum) get_fixnum(pos, bytes)
  Get the fixnum represented by a sequence of bytes starting at position pos in the buffer.
- - (Float) get_float(pos, bytes)
  Get the float represented by a sequence of bytes starting at position pos in the buffer.
- - (String) get_string(pos, end_pos, enc = 'UTF-8')
  Return bytes from the buffer interpreted as a String.
- - (FixNum) length
  The length of this buffer, in bytes.
- - (Buffer) set_buffer(pos, buff)
  Sets bytes in this buffer to the bytes of the specified buffer.
- - (Buffer) set_fixnum(pos, num, bytes)
  Sets bytes in the buffer to a representation of a fixnum.
- - (Buffer) set_float(pos, num, bytes)
  Sets bytes in the buffer to a representation of a float.
- - (Buffer) set_string(pos, str, enc = 'UTF-8')
  Set bytes in the buffer to the string encoding in the specified encoding.
- - (String) to_s(enc = "UTF-8")
  Return a String representation of the buffer.

## Class Method Details

### + (`Object`) create(initial_size_hint = 0)

Creates a new empty buffer. The #length of the buffer immediately after creation will be zero. to initially allocate to the buffer to prevent excessive automatic re-allocations as data is written to it.

Parameters:

- initial_size_hint (`FixNum`) (defaults to: `0`) —
  is a hint to the system for how much memory

    47
    48
    49

    # File 'src/main/ruby_scripts/core/buffer.rb', line 47

    def Buffer.create(initial_size_hint = 0)
      Buffer.new(org.vertx.java.core.buffer.Buffer.new(initial_size_hint))
    end

### + (`Object`) create_from_str(str, enc = "UTF-8")

Create a new Buffer from a String

Parameters:

- str (`String`) —
  The String to encode into the Buffer
- enc (`String`) (defaults to: `"UTF-8"`) —
  Encoding to use. Defaults to "UTF-8"

    54
    55
    56

    # File 'src/main/ruby_scripts/core/buffer.rb', line 54

    def Buffer.create_from_str(str, enc = "UTF-8")
      Buffer.new(org.vertx.java.core.buffer.Buffer.new(str, enc))
    end

## Instance Method Details

### - (`Buffer`) append_buffer(buff)

Appends a buffer to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- buff (`Buffer`) —
  the buffer to append.

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    127
    128
    129
    130

    # File 'src/main/ruby_scripts/core/buffer.rb', line 127

    def append_buffer(buff)
      @buffer.appendBuffer(buff._to_java_buffer)
      self
    end

### - (`Buffer`) append_fixnum(num, bytes)

Appends a fixnum to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- num (`FixNum`) —
  the fixnum to append.
- bytes (`FixNum`) —
  the number of bytes to write in the buffer to represent the fixnum. Valid values are 1, 2, 4 and 8.

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150

    # File 'src/main/ruby_scripts/core/buffer.rb', line 136

    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

### - (`Buffer`) append_float(num, bytes)

Appends a float to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- num (`Float`) —
  the float to append.
- bytes (`FixNum`) —
  the number of bytes to write in the buffer to represent the float. Valid values are 4 and 8.

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    156
    157
    158
    159
    160
    161
    162
    163
    164
    165

    # File 'src/main/ruby_scripts/core/buffer.rb', line 156

    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

### - (`Buffer`) append_str(str, enc = "UTF-8")

Appends a string to the end of this buffer. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- str (`String`) —
  the string to append.
- enc (`String`) (defaults to: `"UTF-8"`) —
  the encoding to use. Defaults to "UTF-8"

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    171
    172
    173
    174

    # File 'src/main/ruby_scripts/core/buffer.rb', line 171

    def append_str(str, enc = "UTF-8")
      @buffer.appendString(str, enc)
      self
    end

### - (`Buffer`) copy

Get a copy of the entire buffer.

Returns:

- (`Buffer`) —
  the copy

    240
    241
    242

    # File 'src/main/ruby_scripts/core/buffer.rb', line 240

    def copy
      Buffer.new(@buffer.copy())
    end

### - (`Buffer`) get_buffer(pos, end_pos)

Return bytes in the buffer as a Buffer

Parameters:

- start_pos (`FixNum`) —
  - the position in this buffer from where to start the copy.
- end_pos (`FixNum`) —
  - the copy will be made up to index end_pos - 1

Returns:

- (`Buffer`) —
  the copy

    119
    120
    121
    122

    # File 'src/main/ruby_scripts/core/buffer.rb', line 119

    def get_buffer(pos, end_pos)
      j_buff = @buffer.getBuffer(pos, end_pos)
      Buffer.new(j_buff)
    end

### - (`FixNum`) get_byte(pos)

Get the byte at position pos in the buffer.

Parameters:

- pos (`FixNum`) —
  the position in the buffer from where to retrieve the byte

Returns:

- (`FixNum`) —
  the byte

    68
    69
    70

    # File 'src/main/ruby_scripts/core/buffer.rb', line 68

    def get_byte(pos)
      @buffer.getByte(pos)
    end

### - (`FixNum`) get_fixnum(pos, bytes)

Get the fixnum represented by a sequence of bytes starting at position pos in the buffer.

Parameters:

- pos (`FixNum`) —
  the position in the buffer from where to retrieve the FixNum.
- bytes (`FixNum`) —
  the number of bytes to retrieve from position pos to create the FixNum. Valid values are 1, 2, 4 and 8.

Returns:

- (`FixNum`) —
  the FixNum

    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89

    # File 'src/main/ruby_scripts/core/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

### - (`Float`) get_float(pos, bytes)

Get the float represented by a sequence of bytes starting at position pos in the buffer.

Parameters:

- pos (`Float`) —
  the position in the buffer from where to retrieve the Float.
- bytes (`Float`) —
  the number of bytes to retrieve from position pos to create the Float. Valid values are 4 and 8.

Returns:

- (`Float`) —
  the Float

    95
    96
    97
    98
    99
    100
    101
    102
    103
    104

    # File 'src/main/ruby_scripts/core/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

### - (`String`) get_string(pos, end_pos, enc = 'UTF-8')

Return bytes from the buffer interpreted as a String

Parameters:

- pos (`FixNum`) —
  the position in the buffer from where to start reading
- end_pos (`FixNum`) —
  the position in the buffer to end reading
- enc (`String`) (defaults to: `'UTF-8'`) —
  The encoding to use

Returns:

- (`String`) —
  the String

    111
    112
    113

    # File 'src/main/ruby_scripts/core/buffer.rb', line 111

    def get_string(pos, end_pos, enc = 'UTF-8')
      @buffer.getString(pos, end_pos, enc)
    end

### - (`FixNum`) length

The length of this buffer, in bytes.

Returns:

- (`FixNum`) —
  the length of this buffer, in bytes.

    234
    235
    236

    # File 'src/main/ruby_scripts/core/buffer.rb', line 234

    def length
      @buffer.length
    end

### - (`Buffer`) set_buffer(pos, buff)

Sets bytes in this buffer to the bytes of the specified buffer. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- pos (`FixNum`) —
  - the position in this buffer from where to start writing the buffer
- buff (`Buffer`) —
  - the buffer to write into this buffer

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    218
    219
    220
    221

    # File 'src/main/ruby_scripts/core/buffer.rb', line 218

    def set_buffer(pos, buff)
      @buffer.setBytes(pos, buff._to_java_buffer)
      self
    end

### - (`Buffer`) set_fixnum(pos, num, bytes)

Sets bytes in the buffer to a representation of a fixnum. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- pos (`FixNum`) —
  - the position in the buffer from where to start writing the fixnum
- num (`FixNum`) —
  - the fixnum to write
- bytes (`FixNum`) —
  - the number of bytes to write to represent the fixnum. Valid values are 1, 2, 4, and 8

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195

    # File 'src/main/ruby_scripts/core/buffer.rb', line 181

    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

### - (`Buffer`) set_float(pos, num, bytes)

Sets bytes in the buffer to a representation of a float. The buffer will expand as necessary to accomodate any bytes written.

Parameters:

- pos (`FixNum`) —
  - the position in the buffer from where to start writing the float
- num (`Float`) —
  - the float to write
- bytes (`FixNum`) —
  - the number of bytes to write to represent the float. Valid values are 4 and 8

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212

    # File 'src/main/ruby_scripts/core/buffer.rb', line 202

    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

### - (`Buffer`) set_string(pos, str, enc = 'UTF-8')

Set bytes in the buffer to the string encoding in the specified encoding

Parameters:

- pos (`FixNum`) —
  - the position in this buffer from where to start writing the string
- str (`String`) —
  the string
- enc (`String`) (defaults to: `'UTF-8'`) —
  the encoding

Returns:

- (`Buffer`) —
  a reference to self so multiple operations can be appended together.

    228
    229
    230
    231

    # File 'src/main/ruby_scripts/core/buffer.rb', line 228

    def set_string(pos, str, enc = 'UTF-8')
      @buffer.setString(pos, str, enc)
      self
    end

### - (`String`) to_s(enc = "UTF-8")

Return a String representation of the buffer.

Parameters:

- enc (`String`) (defaults to: `"UTF-8"`) —
  The encoding to use. Defaults to "UTF-8"

Returns:

- (`String`) —
  a String representation of the buffer.

    61
    62
    63

    # File 'src/main/ruby_scripts/core/buffer.rb', line 61

    def to_s(enc = "UTF-8")
      @buffer.toString(enc)
    end