fcopy(n) Tcl Built-In Commands fcopy(n) NAME fcopy - Copy data from one channel to another. SYNOPSIS fcopy inchan outchan ?-size size? ?-command callback? DESCRIPTION The fcopy command copies data from one I/O channel, inchan to another I/O channel, outchan. The fcopy command leverages the buffering in(1,8) the Tcl I/O system to avoid extra copies and to avoid buffering too much data in(1,8) main memory when copying large files to slow destinations like network sockets. The fcopy command transfers data from inchan until end of file(1,n) or size bytes have been transferred. If no -size argument is given, then the copy goes until end of file. All the data read(2,n,1 builtins) from inchan is copied to outchan. Without the -command option, fcopy blocks until the copy is complete and returns the number of bytes written to outchan. The -command argument makes fcopy work in(1,8) the background. In this case it returns immediately and the callback is invoked later when the copy completes. The callback is called with one or two additional arguments that indicates how many bytes were written to outchan. If an error(8,n) occurred during the background copy, the second argument is the error(8,n) string(3,n) associated with the error. With a background copy, it is not necessary to put inchan or outchan into non-blocking mode; the fcopy command takes care of that automatically. However, it is necessary to enter the event loop by using the vwait command or by using Tk. You are not allowed to do other I/O operations with inchan or outchan during a background fcopy. If either inchan or outchan get closed while the copy is in(1,8) progress, the current copy is stopped and the com- mand callback is not made. If inchan is closed, then all data already queued for outchan is written out. Note that inchan can become readable during a background copy. You should turn off any fileevent handlers during a background copy so those handlers do not interfere with the copy. Any I/O attempted by a fileevent handler will get a "channel busy" error. Fcopy translates end-of-line sequences in(1,8) inchan and outchan according to the -translation option for these channels. See the manual entry for fconfigure for details on the -translation option. The transla- tions mean that the number of bytes read(2,n,1 builtins) from inchan can be different than the number of bytes written to outchan. Only the number of bytes written to outchan is reported, either as the return value of a syn- chronous fcopy or as the argument to the callback for an asynchronous fcopy. Fcopy obeys the encodings configured for the channels. This means that the incoming characters are converted internally first UTF-8 and then into the encoding(3,n) of the channel fcopy writes to. See the manual entry for fconfigure for details on the -encoding option. No conversion is done if(3,n) both channels are set(7,n,1 builtins) to encoding(3,n) "binary". If only the output channel is set(7,n,1 builtins) to encoding(3,n) "binary" the system will write(1,2) the internal UTF-8 representation of the incoming characters. If only the input channel is set(7,n,1 builtins) to encoding(3,n) "binary" the system will assume that the incoming bytes are valid UTF-8 characters and convert them according to the output encoding. The behaviour of the system for bytes which are not valid UTF-8 characters is undefined in(1,8) this case. EXAMPLE This first example shows how the callback gets(3,n) passed the number of bytes transferred. It also uses vwait to put the application into the event loop. Of course, this simplified example could be done without the command callback. proc(5,n) Cleanup {in(1,8) out bytes {error(8,n) {}}} { global total set(7,n,1 builtins) total $bytes close(2,7,n) $in(1,8) close(2,7,n) $out if(3,n) {[string(3,n) length $error(8,n)] != 0} { # error(8,n) occurred during the copy } } set(7,n,1 builtins) in(1,8) [open(2,3,n) $file1] set(7,n,1 builtins) out [socket(2,7,n) $server $port] fcopy $in(1,8) $out -command [list Cleanup $in(1,8) $out] vwait total The second example copies in(1,8) chunks and tests for end of file(1,n) in(1,8) the command callback proc(5,n) CopyMore {in(1,8) out chunk bytes {error(8,n) {}}} { global total done incr total $bytes if(3,n) {([string(3,n) length $error(8,n)] != 0) || [eof $in(1,8)] { set(7,n,1 builtins) done $total close(2,7,n) $in(1,8) close(2,7,n) $out } else { fcopy $in(1,8) $out -command [list CopyMore $in(1,8) $out $chunk] \ -size $chunk } } set(7,n,1 builtins) in(1,8) [open(2,3,n) $file1] set(7,n,1 builtins) out [socket(2,7,n) $server $port] set(7,n,1 builtins) chunk 1024 set(7,n,1 builtins) total 0 fcopy $in(1,8) $out -command [list CopyMore $in(1,8) $out $chunk] -size $chunk vwait done SEE ALSO eof(n), fblocked(n), fconfigure(n) KEYWORDS blocking, channel, end of line, end of file(1,n), nonblocking, read(2,n,1 builtins), trans- lation Tcl 8.0 fcopy(n)