Repository navigation
Modern Fs Stream
Paul Squires edited this page Oct 24, 2026
·
4 revisions
Home › Filesystem › File Streams › Stream
Holds the base type of every byte stream, with read, write, seek and flush.
type Stream extends object
CanRead( ) / CanWrite( ) / CanSeek( ) '' const virtual
Read( buf, n ) / Write( buf, n ) '' virtual, FsResultI64
Seek( offset [, origin ] ) '' virtual, FsResultI64
Flush( ) '' virtual, FsResultBool
ReadExact / ReadToEnd / WriteAll / WriteBytes / WriteUtf8 / WriteText / Position
end type- Read, Write, Seek and Flush are virtual. A stream of your own extends Stream and overrides the ones it supports, plus CanRead, CanWrite and CanSeek.
- The versions Stream itself provides fail with FS_UNSUPPORTED, except Flush, which has nothing to flush and succeeds.
- An override keeps the contract below itself, the argument rule included. ReadExact, ReadToEnd, WriteAll, WriteBytes and WriteUtf8 are built on Read and Write and check their own arguments.
- The contract every stream keeps: Read may return fewer bytes than asked, and Ok( 0 ) for n > 0 means the end; Write may write fewer, but never 0; n < 0 or a null buf with n > 0 is FS_INVALID_ARGUMENT; Seek returns the new position and fails below 0 but not past the end; a failed call leaves the position unspecified.
- A stream is not thread-safe unless its own page says so.
#import "std/fs"
#import "std/outcome"
using fs, outcome
'' A stream that yields the letters a to z, then ends
type Alphabet extends Stream
as long nextLetter = 0
declare const function CanRead( ) as boolean override
declare function Read( byval buf as ubyte ptr, byval n as longint ) as FsResultI64 override
end type
const function Alphabet.CanRead( ) as boolean
return true
end function
function Alphabet.Read( byval buf as ubyte ptr, byval n as longint ) as FsResultI64
if( ( n < 0 ) orelse ( ( buf = nullptr ) andalso ( n > 0 ) ) ) then
return FsFail( of longint )( FS_INVALID_ARGUMENT, "Read", "" )
end if
dim as longint got = 0
while( ( got < n ) andalso ( this.nextLetter < 26 ) )
buf[got] = asc( "a" ) + this.nextLetter
got += 1
this.nextLetter += 1
wend
return Ok( of longint, FsError )( got )
end function
dim as Alphabet a
dim as MemoryStream m
print Copy( a, m ).Value( ) '' 26
print m.ToUtf8Text( ) '' abcdefghijklmnopqrstuvwxyz
print a.Write( @"x", 1 ).Failure( ).Message( ) '' unsupportedLibrary: Filesystem Library Category: File Streams Documented under: Streams
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing