Sha256: 7432cfe0e4a74c303cace4c6221c175624b4e3a5d479c4b1101233d43a2f48f3

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# This is free and unencumbered software released into the public domain.

require 'stringio'
require_relative 'readable'
require_relative 'writable'

##
# A buffer for reading and writing Borsh data.
class Borsh::Buffer
  include Borsh::Readable
  include Borsh::Writable

  ##
  # @param [String, nil] data
  # @yield [buffer]
  # @yieldreturn [Object]
  # @return [String]
  def self.open(data = nil, &block)
    buffer = self.new(data || '', &block)
    buffer.close
    buffer.data
  end

  ##
  # @param [String] data
  # @yield [buffer]
  # @yieldreturn [void]
  # @return [void]
  def initialize(data = '', &block)
    @buffer = StringIO.new(data)
    @buffer.binmode
    block.call(self) if block_given?
  end

  ##
  # Returns the buffer data.
  #
  # @return [String]
  def data
    @buffer.string
  end
  alias_method :string, :data

  ##
  # Returns `true` if the buffer is closed.
  #
  # @return [Boolean]
  def closed?
    @buffer.closed?
  end

  ##
  # Closes the buffer.
  #
  # @return [void]
  def close;
    @buffer.close
  end

  ##
  # Reads the specified number of bytes from the buffer.
  #
  # @param [Integer, #to_i] length
  # @return [String]
  def read(length)
    @buffer.read(length.to_i)
  end

  ##
  # Writes data to the buffer.
  #
  # @param [String, #to_s] data
  # @return [Integer]
  def write(data)
    @buffer.write(data.to_s)
  end
end # Borsh::Buffer

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
borsh-0.1.0 lib/borsh/buffer.rb