[Rprotobuf-yada] read from arbitrary binary connection

Romain François francoisromain at free.fr
Sun Nov 8 20:27:16 CET 2009


Hi,

Same as for the serializing out, I have commited some code to read a 
message from a binary connection. It is a little bit more tricky because 
we don't know the size of the payload before we read the file, so I've 
had to subclass CopyingInputStream [1] with some 
RconnectionCopyingInputStream which essentially calls the readBin 
function of R many each time to get a part of the payload.

This is my first real C++ code in the package, so please check that it 
is sound. It seems to work anyway with this simple enough example :

 > require( RProtoBuf )
Loading required package: RProtoBuf
Loading required package: Rcpp

 > message <- new( tutorial.Person, name = "Romain", email = "", id = 2 )
 > serialize( message, "romain.bin" )
 > # using google's code to read rom the file
 > m <- read( tutorial.Person, "/tmp/romain.bin" )
 > writeLines( direct <- as.character(m) )
name: "Romain"
id: 2
email: ""

 > # reading from a binary R connection
 > f <- file( "/tmp/romain.bin", open = "rb" )
 > me <- read( tutorial.Person, f )
 > close( f)
 > writeLines( connect <- as.character(me) )
name: "Romain"
id: 2
email: ""

 > stopifnot( identical( direct, connect ) )



[1] 
http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream_impl_lite.html#CopyingInputStream

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/BcPw : celebrating R commit #50000
|- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
`- http://tr.im/yw8E : New R package : sos



More information about the Rprotobuf-yada mailing list