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.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeByteBuffer

Constructs a new ByteBuffer.



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

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.3'

Instance Method Details

#clearObject

Clear all bytes from the ByteBuffer.



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

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.



98
99
100
101
# File 'lib/bytebuffer.rb', line 98

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.



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

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.



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

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.



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

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

#lengthInteger

Returns the length of the ByteBuffer.

Returns:

  • (Integer)

    the length



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

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

#read_bitBoolean

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

Returns:

  • (Boolean)

    the read value.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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

#reset_bits_cursorObject

Resets the bit cursor position.



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

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

#reset_cursorsObject

Resets the read and write cursor positions.



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

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.



88
89
90
91
92
93
94
# File 'lib/bytebuffer.rb', line 88

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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