Blitz3D+ Command Reference

CopyStream src_stream,dest_stream[,buffer_size]

Parameters

src_stream - stream to copy from

dest_stream - stream to copy to

buffer_size (optional) - size of the chunks copied at a time, in bytes, from 1 to 1048576; 16384 (default)

Description

Copies everything from one stream to another.

Both ends can be file streams or TCP streams, which makes this a one-liner for jobs that would otherwise need a read/write loop: duplicate a file (ReadFile in, WriteFile out), download data from a TCP connection into a file, or push a file out over the network.

Copying runs until the source ends - for a TCP source that means until the other side closes the connection, and reads obey the TCPTimeouts limit, so the call can take a while on a slow link.

The buffer size just tunes how much is moved per gulp; the default is fine for almost everything. A value outside 1 to 1048576 raises a runtime error.

See also: ReadFile, WriteFile, OpenTCPStream, ReadBytes, WriteBytes.

Example

; CopyStream Example
; ------------------

; Write a small save file that we can duplicate
source_name$="copystream_source.txt"
backup_name$="copystream_backup.txt"

out=WriteFile(source_name$)
WriteLine out,"hero=Blitz"
WriteLine out,"hp=250"
WriteLine out,"score=1234567"
CloseFile out
Print "Wrote "+source_name$

; CopyStream reads everything from one stream and writes it to
; another - here it duplicates the whole file in one call.
; It works with any streams: files, TCP streams and so on.
src=ReadFile(source_name$)
dest=WriteFile(backup_name$)
CopyStream src,dest
CloseFile dest
CloseFile src
Print "Copied it to "+backup_name$+" with CopyStream"

; Prove the backup matches by printing its contents
Print ""
Print "Contents of the backup:"
in=ReadFile(backup_name$)
While Not Eof(in)
    Print "    "+ReadLine$(in)
Wend
CloseFile in

; Tidy up the files this example created
DeleteFile source_name$
DeleteFile backup_name$

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

End

Index