fcopy (n)
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 the Tcl I/O system to
avoid extra copies and to avoid buffering too much data in
main memory when copying large files to slow destinations like
network sockets.
The fcopy
command transfers data from inchan until end of file
or size bytes have been
transferred. If no -size argument is given,
then the copy goes until end of file.
All the data read 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 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 occurred during the background copy, the second argument is the
error string 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 progress, the current copy is stopped
and the command 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 inchan and outchan
according to the -translation option
for these channels.
See the manual entry for fconfigure for details on the
-translation option.
The translations mean that the number of bytes read 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 synchronous fcopy or
as the argument to the callback for an asynchronous fcopy.
EXAMPLE
This first example shows how the callback gets
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 Cleanup {in out bytes {error {}}} {
global total
set total $bytes
close $in
close $out
if {[string length $error] != 0} {
# error occurred during the copy
}
}
set in [open $file1]
set out [socket $server $port]
fcopy $in $out -command [list Cleanup $in $out]
vwait total
The second example copies in chunks and tests for end of file
in the command callback
proc CopyMore {in out chunk bytes {error {}}} {
global total done
incr total $bytes
if {([string length $error] != 0) || [eof $in] {
set done $total
close $in
close $out
} else {
fcopy $in $out -command [list CopyMore $in $out $chunk] \\
-size $chunk
}
}
set in [open $file1]
set out [socket $server $port]
set chunk 1024
set total 0
fcopy $in $out -command [list CopyMore $in $out $chunk] -size $chunk
vwait done
SEE ALSO
KEYWORDS
blocking, channel, end of line, end of file, nonblocking, read, translation
'\"
'\" Copyright (c) 1993 The Regents of the University of California.
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
'\" RCS: @(#) $Id: file.n,v 1.2 1998/09/14 18:39:52 stanton Exp $
'\"
'\" The definitions below are for supplemental macros used in Tcl/Tk
'\" manual entries.
'\"
'\" .AP type name in/out ?indent?
'\" Start paragraph describing an argument to a library procedure.
'\" type is type of argument (int, etc.), in/out is either "in", "out",
'\" or "in/out" to describe whether procedure reads or modifies arg,
'\" and indent is equivalent to second arg of .IP (shouldn't ever be
'\" needed; use .AS below instead)
'\"
'\" .AS ?type? ?name?
'\" Give maximum sizes of arguments for setting tab stops. Type and
'\" name are examples of largest possible arguments that will be passed
'\" to .AP later. If args are omitted, default tab stops are used.
'\"
'\" .BS
'\" Start box enclosure. From here until next .BE, everything will be
'\" enclosed in one large box.
'\"
'\" .BE
'\" End of box enclosure.
'\"
'\" .CS
'\" Begin code excerpt.
'\"
'\" .CE
'\" End code excerpt.
'\"
'\" .VS ?version? ?br?
'\" Begin vertical sidebar, for use in marking newly-changed parts
'\" of man pages. The first argument is ignored and used for recording
'\" the version when the .VS was added, so that the sidebars can be
'\" found and removed when they reach a certain age. If another argument
'\" is present, then a line break is forced before starting the sidebar.
'\"
'\" .VE
'\" End of vertical sidebar.
'\"
'\" .DS
'\" Begin an indented unfilled display.
'\"
'\" .DE
'\" End of indented unfilled display.
'\"
'\" .SO
'\" Start of list of standard options for a Tk widget. The
'\" options follow on successive lines, in four columns separated
'\" by tabs.
'\"
'\" .SE
'\" End of list of standard options for a Tk widget.
'\"
'\" .OP cmdName dbName dbClass
'\" Start of description of a specific option. cmdName gives the
'\" option's name as specified in the class command, dbName gives
'\" the option's name in the option database, and dbClass gives
'\" the option's class in the option database.
'\"
'\" .UL arg1 arg2
'\" Print arg1 underlined, then print arg2 normally.
'\"
'\" RCS: @(#) $Id: man.macros,v 1.2 1998/09/14 18:39:54 stanton Exp $
'\"
'\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
t .wh -1.3i ^B
^l \n(.l
b
'\" # Start an argument description
AP
!"\\$4"" .TP \\$4
\{\
!"\\$2"" .TP \\n()Cu
.TP 15
|
|