class Ronin::Support::Binary::Stream

Represents a binary stream of data.

## Examples

Creates a binary stream around a file:

stream = Stream.open('path/to/file.bin')
stream.read_uint32
# => 1234

Creates a binary stream around a socket:

socket = TCPSocket.new('example.com',1337)
stream = Stream.new(socket)
stream.read_uint32
# => 1234
stream.write_uint32(0xffffffff)

@api public

@since 1.0.0

Attributes

io[R]

The underlying IO stream.

@return [IO, StringIO]

Public Class Methods

new(io, **kwargs) click to toggle source

Initializes the stream.

@param [IO, StringIO] io

The underlying IO stream.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:little, :big, :net, nil] :endian

The desired endianness of the values of the IO stream.

@option kwargs [:x86, :x86_64,

              :ppc, :ppc64,
              :mips, :mips_le, :mips_be,
              :mips64, :mips64_le, :mips64_be,
              :arm, :arm_le, :arm_be,
              :arm64, :arm64_le, :arm64_be] :arch
The desired architecture for the values of the IO stream.
# File lib/ronin/support/binary/stream.rb, line 78
def initialize(io, **kwargs)
  initialize_type_system(**kwargs)

  @io = io
end
open(path,mode='r') click to toggle source

Opens a file in binary mode and returns a stream.

@param [String] path

The path to the file.

@param [“r”, “w”, “r+”, “w+”] mode

The mode to open the file in.

@return [Stream]

The newly created stream.
# File lib/ronin/support/binary/stream.rb, line 96
def self.open(path,mode='r')
  new(File.new(path,"#{mode}b"))
end

Public Instance Methods

eof?() click to toggle source

Determines if EOF has been reached.

@return [Boolean]

# File lib/ronin/support/binary/stream.rb, line 118
def eof?
  @io.eof?
end
external_encoding() click to toggle source

Returns the external encoding for the stream.

@return [Encoding]

# File lib/ronin/support/binary/stream.rb, line 109
def external_encoding
  @io.external_encoding
end
read(length=nil) click to toggle source

Reads a string from the IO stream.

@param [Integer, nil] length

The desired length of the string.

@return [String]

The read String.
# File lib/ronin/support/binary/stream.rb, line 131
def read(length=nil)
  @io.read(length)
end
write(data) click to toggle source

Writes String to the IO stream.

@param [String] data

The data to write to the IO stream.

@return [Integer]

The number of bytes written to the IO stream.
# File lib/ronin/support/binary/stream.rb, line 144
def write(data)
  @io.write(data)
end