ReadAvail ( stream )

Parameters

stream - a file stream (ReadFile, OpenFile) or a TCP stream (OpenTCPStream, AcceptTCPStream)

Description

Returns how many bytes are ready to be read from a stream without waiting.

For TCP streams this is the command that keeps a game responsive: it tells you how many bytes have actually arrived from the network. The read commands block until they get what they asked for (or the TCPTimeouts limit kills the stream), so the safe pattern is to check ReadAvail first and only read once enough bytes are in - that way your main loop never stalls waiting on a slow connection.

For disk files it merely reports how much data is internally buffered, which is not very interesting - use FileSize and FilePos to reason about position in a file.

See also: Eof, ReadByte, OpenTCPStream, TCPTimeouts.

Example

; ReadAvail Example
; -----------------

; Save five 4-byte integers to a demo file
file=WriteFile("demo_data.dat")
For i=1 To 5
    WriteInt file,i*111
Next
CloseFile file

; For files, ReadAvail reports how many bytes sit ready in the
; stream's internal buffer; for TCP streams, how much has arrived
file=ReadFile("demo_data.dat")
Print "Buffered before any read: "+ReadAvail(file)+" (the buffer fills on first read)"

For i=1 To 5
    value=ReadInt(file)
    Print "Read "+value+"   ReadAvail now: "+ReadAvail(file)
Next
CloseFile file

; Clean up the demo file
DeleteFile "demo_data.dat"
Print "Deleted demo_data.dat"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index