1fcopy(n)                     Tcl Built-In Commands                    fcopy(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       fcopy - Copy data from one channel to another.
9

SYNOPSIS

11       fcopy inchan outchan ?-size size? ?-command callback?
12_________________________________________________________________
13
14

DESCRIPTION

16       The  fcopy  command copies data from one I/O channel, inchan to another
17       I/O channel, outchan.  The fcopy command leverages the buffering in the
18       Tcl  I/O  system  to avoid extra copies and to avoid buffering too much
19       data in main memory when copying large files to slow destinations  like
20       network sockets.
21
22       The  fcopy command transfers data from inchan until end of file or size
23       bytes have been transferred. If no -size argument is  given,  then  the
24       copy  goes  until end of file.  All the data read from inchan is copied
25       to outchan.  Without the -command option, fcopy blocks until  the  copy
26       is complete and returns the number of bytes written to outchan.
27
28       The -command argument makes fcopy work in the background.  In this case
29       it returns immediately and the callback is invoked later when the  copy
30       completes.  The callback is called with one or two additional arguments
31       that indicates how many bytes were written to  outchan.   If  an  error
32       occurred  during  the background copy, the second argument is the error
33       string associated with the error.  With a background copy,  it  is  not
34       necessary  to  put  inchan or outchan into non-blocking mode; the fcopy
35       command takes care of that automatically.  However, it is necessary  to
36       enter the event loop by using the vwait command or by using Tk.
37
38       You  are  not allowed to do other I/O operations with inchan or outchan
39       during a background fcopy.  If either  inchan  or  outchan  get  closed
40       while the copy is in progress, the current copy is stopped and the com‐
41       mand callback is not made.  If inchan is closed, then all data  already
42       queued for outchan is written out.
43
44       Note  that  inchan  can  become readable during a background copy.  You
45       should turn off any fileevent handlers  during  a  background  copy  so
46       those  handlers do not interfere with the copy.  Any I/O attempted by a
47       fileevent handler will get a "channel busy" error.
48
49       Fcopy translates end-of-line sequences in inchan and outchan  according
50       to  the  -translation  option for these channels.  See the manual entry
51       for fconfigure for details on the -translation  option.   The  transla‐
52       tions  mean  that the number of bytes read from inchan can be different
53       than the number of bytes written to outchan.  Only the number of  bytes
54       written  to  outchan  is reported, either as the return value of a syn‐
55       chronous fcopy or as the argument to the callback for  an  asynchronous
56       fcopy.
57
58       Fcopy  obeys the encodings configured for the channels. This means that
59       the incoming characters are converted internally first UTF-8  and  then
60       into  the encoding of the channel fcopy writes to. See the manual entry
61       for fconfigure for details on the -encoding option.  No  conversion  is
62       done  if both channels are set to encoding "binary". If only the output
63       channel is set to encoding "binary" the system will write the  internal
64       UTF-8  representation  of  the  incoming  characters. If only the input
65       channel is set to encoding "binary" the system  will  assume  that  the
66       incoming bytes are valid UTF-8 characters and convert them according to
67       the output encoding. The behaviour of the system for  bytes  which  are
68       not valid UTF-8 characters is undefined in this case.
69
70

EXAMPLE

72       This  first  example  shows  how the callback gets passed the number of
73       bytes transferred.  It also uses vwait to put the application into  the
74       event  loop.   Of course, this simplified example could be done without
75       the command callback.  proc Cleanup {in out bytes {error {}}} {
76           global total
77           set total $bytes
78           close $in
79           close $out
80           if {[string length $error] != 0} {      # error occurred during the
81       copy
82           }  }  set in [open $file1] set out [socket $server $port] fcopy $in
83       $out -command [list Cleanup $in $out] vwait total
84
85
86       The second example copies in chunks and tests for end of  file  in  the
87       command callback proc CopyMore {in out chunk bytes {error {}}} {
88           global total done
89           incr total $bytes
90           if  {([string  length  $error]  !=  0) || [eof $in] {      set done
91       $total      close $in      close $out
92           } else {      fcopy $in  $out  -command  [list  CopyMore  $in  $out
93       $chunk] \          -size $chunk
94           }  }  set in [open $file1] set out [socket $server $port] set chunk
95       1024 set total 0 fcopy  $in  $out  -command  [list  CopyMore  $in  $out
96       $chunk] -size $chunk vwait done
97
98
99

SEE ALSO

101       eof(n), fblocked(n), fconfigure(n)
102
103

KEYWORDS

105       blocking,  channel, end of line, end of file, nonblocking, read, trans‐
106       lation
107
108
109
110Tcl                                   8.0                             fcopy(n)
Impressum