|
| 1 | +# S3 generic for reopening connections in binary mode |
| 2 | +reopen_connection = function(con, ...) { |
| 3 | + UseMethod("reopen_connection") |
| 4 | +} |
| 5 | + |
| 6 | +reopen_connection.default = function(con, ...) { |
| 7 | + con_class = class(con)[1L] |
| 8 | + stopf("Don't know how to reopen connection type '%s'. Need a connection opened in binary mode to continue.", con_class) |
| 9 | +} |
| 10 | + |
| 11 | +reopen_connection.file = function(con, ...) { |
| 12 | + file(summary(con)$description, "rb") |
| 13 | +} |
| 14 | + |
| 15 | +reopen_connection.gzfile = function(con, ...) { |
| 16 | + gzfile(summary(con)$description, "rb") |
| 17 | +} |
| 18 | + |
| 19 | +reopen_connection.bzfile = function(con, ...) { |
| 20 | + bzfile(summary(con)$description, "rb") |
| 21 | +} |
| 22 | + |
| 23 | +reopen_connection.url = function(con, ...) { |
| 24 | + url(summary(con)$description, "rb") |
| 25 | +} |
| 26 | + |
| 27 | +reopen_connection.pipe = function(con, ...) { |
| 28 | + pipe(summary(con)$description, "rb") |
| 29 | +} |
| 30 | + |
1 | 31 | fread = function( |
2 | 32 | input="", file=NULL, text=NULL, cmd=NULL, sep="auto", sep2="auto", dec="auto", quote="\"", nrows=Inf, header="auto", |
3 | 33 | na.strings=getOption("datatable.na.strings","NA"), stringsAsFactors=FALSE, verbose=getOption("datatable.verbose",FALSE), |
@@ -112,13 +142,7 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") |
112 | 142 |
|
113 | 143 | if (needs_reopen) { |
114 | 144 | close(input) |
115 | | - input = switch(con_class, |
116 | | - "file" = file(con_desc, "rb"), |
117 | | - "gzfile" = gzfile(con_desc, "rb"), |
118 | | - "bzfile" = bzfile(con_desc, "rb"), |
119 | | - "url" = url(con_desc, "rb"), |
120 | | - "pipe" = pipe(con_desc, "rb"), |
121 | | - stopf("Don't know how to reopen connection type '%s'. Need a connection opened in binary mode to continue.", con_class)) |
| 145 | + input = reopen_connection(input) |
122 | 146 | close_con = input |
123 | 147 | } else if (!con_open) { |
124 | 148 | open(input, "rb") |
|
0 commit comments