private long doSeek(ThreadContext context, long pos, int whence) { OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { pos = fptr.seek(context, pos, whence); if (pos < 0 && fptr.errno() != null) { throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } } finally { if (locked) fptr.unlock(); } return 0; }
private long doSeek(ThreadContext context, long pos, int whence) { OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { pos = fptr.seek(context, pos, whence); if (pos < 0 && fptr.errno() != null) { throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } } finally { if (locked) fptr.unlock(); } return 0; }
@JRubyMethod(name = {"pos", "tell"}) public RubyFixnum pos(ThreadContext context) { OpenFile fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { long pos = fptr.tell(context); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); pos -= fptr.rbuf.len; return context.runtime.newFixnum(pos); } finally { if (locked) fptr.unlock(); } }
@JRubyMethod(name = {"pos", "tell"}) public RubyFixnum pos(ThreadContext context) { OpenFile fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { long pos = fptr.tell(context); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); pos -= fptr.rbuf.len; return context.runtime.newFixnum(pos); } finally { if (locked) fptr.unlock(); } }
@JRubyMethod(name = "pos=", required = 1) public RubyFixnum pos_set(ThreadContext context, IRubyObject offset) { OpenFile fptr; long pos; pos = offset.convertToInteger().getLongValue(); fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { pos = fptr.seek(context, pos, PosixShim.SEEK_SET); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return context.runtime.newFixnum(pos); }
@JRubyMethod(name = "pos=", required = 1) public RubyFixnum pos_set(ThreadContext context, IRubyObject offset) { OpenFile fptr; long pos; pos = offset.convertToInteger().getLongValue(); fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { pos = fptr.seek(context, pos, PosixShim.SEEK_SET); if (pos == -1 && fptr.errno() != null) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return context.runtime.newFixnum(pos); }
@JRubyMethod public RubyFixnum rewind(ThreadContext context) { Ruby runtime = context.runtime; OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { if (fptr.seek(context, 0L, 0) == -1 && fptr.errno() != null) { throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } if (RubyArgsFile.ArgsFileData.getArgsFileData(runtime).isCurrentFile(this)) { runtime.setCurrentLine(runtime.getCurrentLine() - fptr.getLineNumber()); } fptr.setLineNumber(0); if (fptr.readconv != null) { fptr.clearReadConversion(); } } finally { if (locked) fptr.unlock(); } return RubyFixnum.zero(runtime); }
@JRubyMethod public RubyFixnum rewind(ThreadContext context) { Ruby runtime = context.runtime; OpenFile fptr; fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { if (fptr.seek(context, 0L, 0) == -1 && fptr.errno() != null) { throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } if (RubyArgsFile.ArgsFileData.getArgsFileData(runtime).isCurrentFile(this)) { runtime.setCurrentLine(runtime.getCurrentLine() - fptr.getLineNumber()); } fptr.setLineNumber(0); if (fptr.readconv != null) { fptr.clearReadConversion(); } } finally { if (locked) fptr.unlock(); } return RubyFixnum.zero(runtime); }
if ((fptr.getMode() & OpenFile.WRITABLE) != 0) { if (fptr.io_fflush(context) < 0) throw context.runtime.newErrnoFromErrno(fptr.errno(), "");
if ((fptr.getMode() & OpenFile.WRITABLE) != 0) { if (fptr.io_fflush(context) < 0) throw context.runtime.newErrnoFromErrno(fptr.errno(), "");
/** * <p>Invoke a block for each byte.</p> * * MRI: rb_io_each_byte */ public IRubyObject each_byteInternal(ThreadContext context, Block block) { Ruby runtime = context.runtime; OpenFile fptr; if (!block.isGiven()) return enumeratorize(context.runtime, this, "each_byte"); fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { do { while (fptr.rbuf.len > 0) { byte[] pBytes = fptr.rbuf.ptr; int p = fptr.rbuf.off++; fptr.rbuf.len--; block.yield(context, runtime.newFixnum(pBytes[p] & 0xFF)); fptr.errno(null); } fptr.checkByteReadable(context); fptr.READ_CHECK(context); } while (fptr.fillbuf(context) >= 0); } finally { if (locked) fptr.unlock(); } return this; }
/** * <p>Invoke a block for each byte.</p> * * MRI: rb_io_each_byte */ public IRubyObject each_byteInternal(ThreadContext context, Block block) { Ruby runtime = context.runtime; OpenFile fptr; if (!block.isGiven()) return enumeratorize(context.runtime, this, "each_byte"); fptr = getOpenFileChecked(); boolean locked = fptr.lock(); try { do { while (fptr.rbuf.len > 0) { byte[] pBytes = fptr.rbuf.ptr; int p = fptr.rbuf.off++; fptr.rbuf.len--; block.yield(context, runtime.newFixnum(pBytes[p] & 0xFF)); fptr.errno(null); } fptr.checkByteReadable(context); fptr.READ_CHECK(context); } while (fptr.fillbuf(context) >= 0); } finally { if (locked) fptr.unlock(); } return this; }
public IRubyObject write(ThreadContext context, IRubyObject str, boolean nosync) { Ruby runtime = context.runtime; OpenFile fptr; long n; IRubyObject tmp; RubyIO io = GetWriteIO(); str = str.asString(); tmp = TypeConverter.ioCheckIO(runtime, io); if (tmp == context.nil) { /* port is not IO, call write method for it. */ return sites(context).write.call(context, io, io, str); } io = (RubyIO) tmp; if (((RubyString) str).size() == 0) return RubyFixnum.zero(runtime); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr = io.getOpenFileChecked(); fptr.checkWritable(context); n = fptr.fwrite(context, str, nosync); if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return RubyFixnum.newFixnum(runtime, n); }
public IRubyObject write(ThreadContext context, IRubyObject str, boolean nosync) { Ruby runtime = context.runtime; OpenFile fptr; long n; IRubyObject tmp; RubyIO io = GetWriteIO(); str = str.asString(); tmp = TypeConverter.ioCheckIO(runtime, io); if (tmp == context.nil) { /* port is not IO, call write method for it. */ return sites(context).write.call(context, io, io, str); } io = (RubyIO) tmp; if (((RubyString) str).size() == 0) return RubyFixnum.zero(runtime); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr = io.getOpenFileChecked(); fptr.checkWritable(context); n = fptr.fwrite(context, str, nosync); if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return RubyFixnum.newFixnum(runtime, n); }
@JRubyMethod(name = "syswrite", required = 1) public IRubyObject syswrite(ThreadContext context, IRubyObject str) { Ruby runtime = context.runtime; OpenFile fptr; long n; if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr.checkWritable(context); str = str.convertToString().dupFrozen(); if (fptr.wbuf.len != 0) { runtime.getWarnings().warn("syswrite for buffered IO"); } ByteList strByteList = ((RubyString) str).getByteList(); n = OpenFile.writeInternal(context, fptr, fptr.fd(), strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize()); if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return runtime.newFixnum(n); }
@JRubyMethod(name = "syswrite", required = 1) public IRubyObject syswrite(ThreadContext context, IRubyObject str) { Ruby runtime = context.runtime; OpenFile fptr; long n; if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr.checkWritable(context); str = str.convertToString().dupFrozen(); if (fptr.wbuf.len != 0) { runtime.getWarnings().warn("syswrite for buffered IO"); } ByteList strByteList = ((RubyString) str).getByteList(); n = OpenFile.writeInternal(context, fptr, fptr.fd(), strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize()); if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); } finally { if (locked) fptr.unlock(); } return runtime.newFixnum(n); }
private IRubyObject ioWriteNonblock(ThreadContext context, Ruby runtime, IRubyObject str, boolean no_exception) { OpenFile fptr; long n; if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr.checkWritable(context); if (fptr.io_fflush(context) < 0) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); fptr.setNonblock(runtime); ByteList strByteList = ((RubyString) str).getByteList(); n = fptr.posix.write(fptr.fd(), strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize(), true); if (n == -1) { if (fptr.posix.errno == Errno.EWOULDBLOCK || fptr.posix.errno == Errno.EAGAIN) { if (no_exception) { return runtime.newSymbol("wait_writable"); } else { throw runtime.newErrnoEAGAINWritableError("write would block"); } } throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } } finally { if (locked) fptr.unlock(); } return runtime.newFixnum(n); }
private IRubyObject ioWriteNonblock(ThreadContext context, Ruby runtime, IRubyObject str, boolean no_exception) { OpenFile fptr; long n; if (!(str instanceof RubyString)) str = str.asString(); RubyIO io = GetWriteIO(); fptr = io.getOpenFileChecked(); boolean locked = fptr.lock(); try { fptr.checkWritable(context); if (fptr.io_fflush(context) < 0) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath()); fptr.setNonblock(runtime); ByteList strByteList = ((RubyString) str).getByteList(); n = fptr.posix.write(fptr.fd(), strByteList.unsafeBytes(), strByteList.begin(), strByteList.getRealSize(), true); if (n == -1) { if (fptr.posix.errno == Errno.EWOULDBLOCK || fptr.posix.errno == Errno.EAGAIN) { if (no_exception) { return runtime.newSymbol("wait_writable"); } else { throw runtime.newErrnoEAGAINWritableError("write would block"); } } throw runtime.newErrnoFromErrno(fptr.posix.errno, fptr.getPath()); } } finally { if (locked) fptr.unlock(); } return runtime.newFixnum(n); }