sysread(p1, p2 = v2)
public
Reads integer bytes from ios
using a low-level read and returns them as a
string. Do not mix with other methods that read
from ios or you may get unpredictable results. If the optional
outbuf argument is present, it must reference a String, which will receive the data. Raises SystemCallError on error and EOFError at end of file.
f = File.new("testfile")
f.sysread(16)
Show source
static VALUE
rb_io_sysread(int argc, VALUE *argv, VALUE io)
{
VALUE len, str;
rb_io_t *fptr;
long n, ilen;
rb_scan_args(argc, argv, "11", &len, &str);
ilen = NUM2LONG(len);
if (NIL_P(str)) {
str = rb_str_new(0, ilen);
}
else {
StringValue(str);
rb_str_modify(str);
rb_str_resize(str, ilen);
}
if (ilen == 0) return str;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (READ_DATA_BUFFERED(fptr)) {
rb_raise(rb_eIOError, "sysread for buffered IO");
}
n = fptr->fd;
rb_thread_wait_fd(fptr->fd);
rb_io_check_closed(fptr);
if (RSTRING_LEN(str) != ilen) {
rb_raise(rb_eRuntimeError, "buffer string modified");
}
n = rb_read_internal(fptr->fd, RSTRING_PTR(str), ilen);
if (n == -1) {
rb_sys_fail_path(fptr->pathv);
}
rb_str_set_len(str, n);
if (n == 0 && ilen > 0) {
rb_eof_error();
}
rb_str_resize(str, n);
OBJ_TAINT(str);
return str;
}