Reads binary data from disjoint sections of a connection or a file.

# S3 method for default
readBinFragments(con, what, idxs=1, origin=c("current", "start"), size=NA, ...,
  verbose=FALSE)

Arguments

con

A connection or the pathname of an existing file.

what

A character string or an object specifying the the data type (mode()) to be read.

idxs

A vector of (non-duplicated) indices or a Nx2 matrix of N from-to index intervals specifying the elements to be read. Positions are either relative to the start or the current location of the file/connection as given by argument origin.

origin

A character string specify whether the indices in argument idxs are relative to the "start" or the "current" position of the file/connection.

size

The size of the data type to be read. If NA, the natural size of the data type is used.

...

Additional arguments passed to readBin().

verbose

A logical or a Verbose object.

Value

Returns a vector of the requested mode().

Author

Henrik Bengtsson

See also

Examples

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create a data file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
data <- 1:255
size <- 2
pathname <- tempfile("exampleReadBinFragments")
writeBin(con=pathname, data, size=size)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Read and write using index vectors
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat("Read file...\n")
#> Read file...
# Read every 16:th byte in the file
idxs <- seq(from=1, to=255, by=16)
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs)
stopifnot(identical(x, data[idxs]))
print(x)
#>  [1]   1  17  33  49  65  81  97 113 129 145 161 177 193 209 225 241
# Read every 16:th byte in a connection starting with the 6th.
idxs <- idxs + 5L
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs)
stopifnot(identical(x, data[idxs]))
print(x)
#>  [1]   6  22  38  54  70  86 102 118 134 150 166 182 198 214 230 246
cat("Read file...done\n")
#> Read file...done

cat("Write file...\n")
#> Write file...
# Update every 16:th byte in the file
idxs <- seq(from=1, to=255, by=16)
x0 <- data[idxs]
writeBinFragments(pathname, idxs=idxs, rev(x0), size=size)
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs)
print(x)
#>  [1] 241 225 209 193 177 161 145 129 113  97  81  65  49  33  17   1
stopifnot(identical(rev(x0), x))

# Update every 16:th byte in the file
idxs <- seq(from=1, to=255, by=16)
writeBinFragments(pathname, idxs=idxs, rev(x), size=size)
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs)
print(x)
#>  [1]   1  17  33  49  65  81  97 113 129 145 161 177 193 209 225 241
stopifnot(identical(x0, x))

# Assert everything is as expected
# Read the complete file
x <- readBin(pathname, what="integer", size=size, signed=FALSE, n=length(data))
stopifnot(identical(x, data))
cat("Write file...done\n")
#> Write file...done


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Ditto but via a connection
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat("Read connection...\n")
#> Read connection...
# Read every 16:th byte in a connection
con <- file(pathname, open="rb")
idxs <- seq(from=1, to=255, by=16)
x <- readBinFragments(con, what="integer", size=size, signed=FALSE, idxs=idxs)
stopifnot(identical(x, data[idxs]))
print(x)
#>  [1]   1  17  33  49  65  81  97 113 129 145 161 177 193 209 225 241

# Read every 16:th byte in a connection starting with the 6th.
idxs <- idxs + 5L
x <- readBinFragments(con, what="integer", size=size, signed=FALSE, idxs=idxs, origin="start")
stopifnot(identical(x, data[idxs]))
print(x)
#>  [1]   6  22  38  54  70  86 102 118 134 150 166 182 198 214 230 246
close(con)
cat("Read connection...done\n")
#> Read connection...done


# Update every 16:th byte in a connection
cat("Write connection...\n")
#> Write connection...
con <- file(pathname, open="r+b")
idxs <- seq(from=1, to=255, by=16)
x0 <- data[idxs]
writeBinFragments(pathname, idxs=idxs, rev(x0), size=size)
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs)
print(x)
#>  [1] 241 225 209 193 177 161 145 129 113  97  81  65  49  33  17   1
stopifnot(identical(rev(x0), x))

# Update every 16:th byte in the file
idxs <- seq(from=1, to=255, by=16)
writeBinFragments(pathname, idxs=idxs, rev(x), size=size)
x <- readBinFragments(pathname, what="integer", size=size, signed=FALSE, idxs=idxs, origin="start")
print(x)
#>  [1]   1  17  33  49  65  81  97 113 129 145 161 177 193 209 225 241
stopifnot(identical(x0, x))

close(con)

# Assert everything is as expected
# Read the complete file
x <- readBin(pathname, what="integer", size=size, signed=FALSE, n=length(data))
stopifnot(identical(x, data))
cat("Write connection...done\n")
#> Write connection...done


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clean up
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
file.remove(pathname)
#> [1] TRUE