Skip to content

abstract class SF::InputStream
inherits Reference #

Abstract class for custom file input streams

This class allows users to define their own file input sources from which SFML can load resources.

SFML resource classes like SF::Texture and SF::SoundBuffer provide load_from_file and load_from_memory functions, which read data from conventional sources. However, if you have data coming from a different source (over a network, embedded, encrypted, compressed, etc) you can derive your own class from SF::InputStream and load SFML resources with their load_from_stream function.

Usage example:

# custom stream class that reads from inside a zip file
class ZipStream < SF::InputStream
    def initialize(archive : String)

    def open(filename : String)

    def read(data : Slice) : Int64

    def seek(position : Int) : Int64

    def tell() : Int64

    def size() : Int64

    ...
end

# now you can load textures...
stream = ZipStream.new("resources.zip")
stream.open("images/img.png")
texture = SF::Texture.from_stream(stream)

# musics...
stream = ZipStream.new("resources.zip")
stream.open("musics/msc.ogg")
music = SF::Music.from_stream(stream)

# etc.

Direct known subclasses

SF::FileInputStream SF::MemoryInputStream

Class methods#

.new #

View source

Methods#

#finalize #

View source

abstract #read(data : Slice) : Int64 #

Read data from the stream

After reading, the stream's reading position must be advanced by the amount of bytes read.

  • data - Buffer where to copy the read data

Returns: The number of bytes actually read, or -1 on error

View source

abstract #seek(position : Int) : Int64 #

Change the current reading position

  • position - The position to seek to, from the beginning

Returns: The position actually sought to, or -1 on error

View source

abstract #size : Int64 #

Return the size of the stream

Returns: The total number of bytes available in the stream, or -1 on error

View source

abstract #tell : Int64 #

Get the current reading position in the stream

Returns: The current position, or -1 on error.

View source