From 10da2c6473b4026e50f58ed47063597c70fb144e Mon Sep 17 00:00:00 2001 From: UpeksheJay Date: Wed, 17 Aug 2016 17:48:42 +0530 Subject: [PATCH] Modifications to read_from Signed-off-by: UpeksheJay --- ext/nio4r/bytebuffer.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ext/nio4r/bytebuffer.c b/ext/nio4r/bytebuffer.c index 4d90846..005044c 100644 --- a/ext/nio4r/bytebuffer.c +++ b/ext/nio4r/bytebuffer.c @@ -213,11 +213,25 @@ static VALUE NIO_ByteBuffer_writeTo(VALUE self, VALUE io){ return self; } -static VALUE NIO_ByteBuffer_readFrom(VALUE self, VALUE file){ +static VALUE NIO_ByteBuffer_readFrom(VALUE self, VALUE io){ struct NIO_ByteBuffer *byteBuffer; Data_Get_Struct(self, struct NIO_ByteBuffer, byteBuffer); int size = byteBuffer->limit + 1 - byteBuffer->position; + #if HAVE_RB_IO_T + rb_io_t *fptr; + #else + OpenFile *fptr; + #endif + + GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr); + rb_io_set_nonblock(fptr); + while(NIO_ByteBuffer_hasRemaining(self) == Qtrue){ + char* nextByte; + read(FPTR_TO_FD(fptr), &nextByte, 1); + VALUE byte = rb_str_new2(nextByte); + NIO_ByteBuffer_put(self, byte); + } return self; }