Class: ByteBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/bytebuffer.rb,
lib/bytebuffer/version.rb

Overview

A ByteBuffer provides functions for interacting with buffered IO data. This gem wraps the bytebuffer rust crate located at github.com/terahlunah/bytebuffer using FFI.

Constant Summary collapse

VERSION =

Returns version number of the library.

Returns:

  • (Integer)

    version number of the library

'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeByteBuffer

Constructs a new ByteBuffer.



62
# File 'lib/bytebuffer.rb', line 62

def initialize; @ptr = ByteBufferExtension.new; end

Instance Attribute Details

#VERSIONInteger (readonly)

Returns version number of the library.

Returns:

  • (Integer)

    version number of the library



4
# File 'lib/bytebuffer/version.rb', line 4

VERSION = '0.1.0'

Instance Method Details

#clearObject

Clear all bytes from the ByteBuffer.



75
# File 'lib/bytebuffer.rb', line 75

def clear; ByteBufferExtension.clear(@ptr); end

#freeObject Also known as: drop, destroy

Note:

This function will invoke the drop() macro in rust, passing in the pointer for the ByteBuffer. After the call is made, the pointer is set to nil.

Frees the memory allocated for this ByteBuffer.



94
95
96
97
# File 'lib/bytebuffer.rb', line 94

def free
  ByteBufferExtension.drop(@ptr)
  @ptr = nil # :gottem:
end

#get_read_positionInteger

Gets the read cursor’s current position.

Returns:

  • (Integer)

    the read cursor’s position.



113
# File 'lib/bytebuffer.rb', line 113

def get_read_position; ByteBufferExtension.get_read_position(@ptr); end

#get_write_positionInteger

Gets the write cursor’s current position.

Returns:

  • (Integer)

    the write cursor’s position.



109
# File 'lib/bytebuffer.rb', line 109

def get_write_position; ByteBufferExtension.get_write_position(@ptr); end

#is_empty?Boolean

Is the ByteBuffer empty?

Returns:

  • (Boolean)

    true if the ByteBuffer is empty, false otherwise.



79
# File 'lib/bytebuffer.rb', line 79

def is_empty?; ByteBufferExtension.is_empty?(@ptr); end

#lengthInteger

Returns the length of the ByteBuffer.

Returns:

  • (Integer)

    the length



66
# File 'lib/bytebuffer.rb', line 66

def length; ByteBufferExtension.length(@ptr); end

#read_bitBoolean

Read a single bit from the ByteBuffer as a boolean value.

Returns:

  • (Boolean)

    the read value.



168
# File 'lib/bytebuffer.rb', line 168

def read_bit; ByteBufferExtension.read_bit(@ptr); end

#read_bits(amount) ⇒ Integer

Read a series of bits from the ByteBuffer as a single unsigned 64-bit integer.

Returns:

  • (Integer)

    the read value.



172
# File 'lib/bytebuffer.rb', line 172

def read_bits(amount); ByteBufferExtension.read_bits(@ptr, amount); end

#read_f32Float

Note:

Float values read from this buffer are accurate to the millionth decimal place.

Read a single 32-bit signed float from the ByteBuffer.

Returns:

  • (Float)

    the read value.



209
# File 'lib/bytebuffer.rb', line 209

def read_f32; ByteBufferExtension.read_f32(@ptr); end

#read_f64Float

Note:

Float values read from this buffer are accurate to the millionth decimal place.

Read a single 32-bit signed float from the ByteBuffer.

Returns:

  • (Float)

    the read value.



214
# File 'lib/bytebuffer.rb', line 214

def read_f64; ByteBufferExtension.read_f64(@ptr); end

#read_i16Integer

Read a single 16-bit signed integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



188
# File 'lib/bytebuffer.rb', line 188

def read_i16; ByteBufferExtension.read_i16(@ptr); end

#read_i32Integer

Read a single 32-bit signed integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



196
# File 'lib/bytebuffer.rb', line 196

def read_i32; ByteBufferExtension.read_i32(@ptr); end

#read_i64Integer

Read a single 64-bit signed integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



204
# File 'lib/bytebuffer.rb', line 204

def read_i64; ByteBufferExtension.read_i64(@ptr); end

#read_i8Integer

Read a single 8-bit signed integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



180
# File 'lib/bytebuffer.rb', line 180

def read_i8; ByteBufferExtension.read_i8(@ptr); end

#read_u16Integer

Read a single 16-bit unsigned integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



184
# File 'lib/bytebuffer.rb', line 184

def read_u16; ByteBufferExtension.read_u16(@ptr); end

#read_u32Integer

Read a single 32-bit unsigned integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



192
# File 'lib/bytebuffer.rb', line 192

def read_u32; ByteBufferExtension.read_u32(@ptr); end

#read_u64Integer

Read a single 64-bit unsigned integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



200
# File 'lib/bytebuffer.rb', line 200

def read_u64; ByteBufferExtension.read_u64(@ptr); end

#read_u8Integer

Read a single 8-bit unsigned integer from the ByteBuffer.

Returns:

  • (Integer)

    the read value.



176
# File 'lib/bytebuffer.rb', line 176

def read_u8; ByteBufferExtension.read_u8(@ptr); end

#reset_bits_cursorObject

Resets the bit cursor position.



72
# File 'lib/bytebuffer.rb', line 72

def reset_bits_cursor; ByteBufferExtension.reset_bits_cursor(@ptr); end

#reset_cursorsObject

Resets the read and write cursor positions.



69
# File 'lib/bytebuffer.rb', line 69

def reset_cursors; ByteBufferExtension.reset_cursors(@ptr); end

#resize(new_size) ⇒ Object

Note:

ByteBuffers can only increase in size. Lower or negative sizes will not work.

Resizes the ByteBuffer to the desired length.

Parameters:

  • new_size (Integer)

    the desired length.



84
85
86
87
88
89
90
# File 'lib/bytebuffer.rb', line 84

def resize(new_size)
  if new_size.negative?
    raise ArgumentError, "new_size must be positive"
  else
    ByteBufferExtension.resize(@ptr, new_size)
  end
end

#set_read_position(position) ⇒ Object

Set the read cursor to the desired position.

Parameters:

  • position (Integer)

    the desired position.



105
# File 'lib/bytebuffer.rb', line 105

def set_read_position(position); ByteBufferExtension.set_read_position(@ptr, position); end

#set_write_position(position) ⇒ Object

Set the write cursor to the desired position.

Parameters:

  • position (Integer)

    the desired position.



101
# File 'lib/bytebuffer.rb', line 101

def set_write_position(position); ByteBufferExtension.set_write_position(@ptr, position); end

#write_bit(value) ⇒ Object

Write a boolean value as a single bit to the ByteBuffer.

Parameters:

  • value (Boolean)

    the value to write.



117
# File 'lib/bytebuffer.rb', line 117

def write_bit(value); ByteBufferExtension.write_bit(@ptr, value); end

#write_bits(value, amount) ⇒ Object

Write a numeric value as a series of bits in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.

  • amount (Integer)

    the amount of bits to use.



122
# File 'lib/bytebuffer.rb', line 122

def write_bits(value, amount); ByteBufferExtension.write_bits(@ptr, value, amount); end

#write_f32(value) ⇒ Object

Note:

The C value used is accurate to the nearest millionth decimal place.

Write a float value as a 32-bit signed float in the ByteBuffer.

Parameters:

  • value (Float)

    the value to write.



159
# File 'lib/bytebuffer.rb', line 159

def write_f32(value); ByteBufferExtension.write_f32(@ptr, value); end

#write_f64(value) ⇒ Object

Note:

The C value used is accurate to the nearest millionth decimal place.

Write a float value as a 64-bit signed float in the ByteBuffer.

Parameters:

  • value (Float)

    the value to write.



164
# File 'lib/bytebuffer.rb', line 164

def write_f64(value); ByteBufferExtension.write_f64(@ptr, value); end

#write_i16(value) ⇒ Object

Write a numeric value as a 16-bit signed integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



138
# File 'lib/bytebuffer.rb', line 138

def write_i16(value); ByteBufferExtension.write_i16(@ptr, value); end

#write_i32(value) ⇒ Object

Write a numeric value as a 32-bit signed integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



146
# File 'lib/bytebuffer.rb', line 146

def write_i32(value); ByteBufferExtension.write_i32(@ptr, value); end

#write_i64(value) ⇒ Object

Write a numeric value as a 64-bit signed integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



154
# File 'lib/bytebuffer.rb', line 154

def write_i64(value); ByteBufferExtension.write_i64(@ptr, value); end

#write_i8(value) ⇒ Object

Write a numeric value as a 8-bit signed integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



130
# File 'lib/bytebuffer.rb', line 130

def write_i8(value); ByteBufferExtension.write_i8(@ptr, value); end

#write_u16(value) ⇒ Object

Write a numeric value as a 16-bit unsigned integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



134
# File 'lib/bytebuffer.rb', line 134

def write_u16(value); ByteBufferExtension.write_u16(@ptr, value); end

#write_u32(value) ⇒ Object

Write a numeric value as a 32-bit unsigned integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



142
# File 'lib/bytebuffer.rb', line 142

def write_u32(value); ByteBufferExtension.write_u32(@ptr, value); end

#write_u64(value) ⇒ Object

Write a numeric value as a 64-bit unsigned integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



150
# File 'lib/bytebuffer.rb', line 150

def write_u64(value); ByteBufferExtension.write_u64(@ptr, value); end

#write_u8(value) ⇒ Object

Write a numeric value as a 8-bit unsigned integer in the ByteBuffer.

Parameters:

  • value (Integer)

    the value to write.



126
# File 'lib/bytebuffer.rb', line 126

def write_u8(value); ByteBufferExtension.write_u8(@ptr, value); end