In R
, what is the best way to copy all content from one connection to another connection? In C#
, such functionality appears to be provided by the Steam.copyTo()
function.
I know that this could be performed manually, using read and write functions in a loop.
For example, consider this script to gzip
a file (here, the DESCRIPTION file from the base package):
file_to_be_gziped <- system.file("DESCRIPTION", package = "base")
in_connection <- file(file_to_be_gziped, open = "rb", raw = TRUE)
out_connection <- gzfile("DESCRIPTION.gz", open = "wb")
while(length(buf <- readBin(in_connection, "raw", n = 1024*1024))) {
writeBin(buf, out_connection)
}
close(in_connection)
close(out_connection)
However, such an approach is not only very lengthy but also error prone and probably inefficient (blocking).
How can this be achieved in R
in a concise, expressive and efficient way?