Skip to content

Commit 5a98e62

Browse files
committed
add reopen_connection generic
1 parent f6f9ed3 commit 5a98e62

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export(rbindlist)
2020
export(fifelse)
2121
export(fcase)
2222
export(fread)
23+
export(reopen_connection)
2324
export(fwrite)
2425
export(foverlaps)
2526
export(shift)

R/fread.R

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
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+
131
fread = function(
232
input="", file=NULL, text=NULL, cmd=NULL, sep="auto", sep2="auto", dec="auto", quote="\"", nrows=Inf, header="auto",
333
na.strings=getOption("datatable.na.strings","NA"), stringsAsFactors=FALSE, verbose=getOption("datatable.verbose",FALSE),
@@ -112,13 +142,7 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC")
112142

113143
if (needs_reopen) {
114144
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)
122146
close_con = input
123147
} else if (!con_open) {
124148
open(input, "rb")

man/reopen_connection.Rd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
\name{reopen_connection}
2+
\alias{reopen_connection}
3+
\title{ Reopen a connection in binary mode }
4+
\description{
5+
S3 generic to reopen a connection in binary read mode. Used internally by \code{fread}. Exported so packages with custom connection classes can define methods.
6+
}
7+
\usage{
8+
reopen_connection(con, ...)
9+
}
10+
\arguments{
11+
\item{con}{ A connection object. }
12+
\item{...}{ Additional arguments for methods. }
13+
}
14+
\details{
15+
Reopens a connection in binary read mode (\code{"rb"}). Methods are provided for \code{file}, \code{gzfile}, \code{bzfile}, \code{url}, and \code{pipe} connections.
16+
17+
To support custom connection types with \code{fread}, define a method for your connection class that returns a new connection opened in binary mode.
18+
}
19+
\value{
20+
A connection object opened in binary read mode.
21+
}
22+
\seealso{
23+
\code{\link{fread}}
24+
}
25+
\keyword{ data }

0 commit comments

Comments
 (0)