/** * waits until input available or timed out and returns self, or nil when EOF reached. */ @JRubyMethod(optional = 1) public static IRubyObject wait_writable(ThreadContext context, IRubyObject _io, IRubyObject[] argv) { RubyIO io = (RubyIO)_io; OpenFile fptr = io.getOpenFileChecked(); fptr.checkWritable(context); long tv = prepareTimeout(context, argv); return doWait(context, io, fptr, tv, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE); }
@JRubyMethod(name = {"to_output_stream", "to_outputstream"}) public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) { RubyIO io = (RubyIO) self; io.getOpenFile().checkWritable(context); return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream()); }
/** * waits until input available or timed out and returns self, or nil when EOF reached. */ @JRubyMethod(optional = 1) public static IRubyObject wait_writable(ThreadContext context, IRubyObject _io, IRubyObject[] argv) { RubyIO io = (RubyIO)_io; OpenFile fptr = io.getOpenFileChecked(); fptr.checkWritable(context); long tv = prepareTimeout(context, argv); return doWait(context, io, fptr, tv, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE); }
@JRubyMethod(name = {"to_output_stream", "to_outputstream"}) public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) { RubyIO io = (RubyIO) self; io.getOpenFile().checkWritable(context); return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream()); }
@Override public <T> T toJava(Class<T> target) { if (target == java.io.InputStream.class) { getOpenFile().checkReadable(getRuntime().getCurrentContext()); return target.cast(getInStream()); } if (target == java.io.OutputStream.class) { getOpenFile().checkWritable(getRuntime().getCurrentContext()); return target.cast(getOutStream()); } return super.toJava(target); }
@Override public <T> T toJava(Class<T> target) { if (target == java.io.InputStream.class) { getOpenFile().checkReadable(getRuntime().getCurrentContext()); return target.cast(getInStream()); } if (target == java.io.OutputStream.class) { getOpenFile().checkWritable(getRuntime().getCurrentContext()); return target.cast(getOutStream()); } return super.toJava(target); }
@JRubyMethod public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) { RubyIO io = (RubyIO)self; Ruby runtime = context.runtime; try { io.getOpenFile().checkWritable(context.runtime); } catch (IOException ex) { throw runtime.newIOErrorFromException(ex); } catch (BadDescriptorException ex) { throw runtime.newErrnoEBADFError(); } catch (InvalidValueException e) { throw runtime.newErrnoEINVALError(); } return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream()); }
@JRubyMethod public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) { RubyIO io = (RubyIO)self; Ruby runtime = context.runtime; try { io.getOpenFile().checkWritable(context.runtime); } catch (IOException ex) { throw runtime.newIOErrorFromException(ex); } catch (BadDescriptorException ex) { throw runtime.newErrnoEBADFError(); } catch (InvalidValueException e) { throw runtime.newErrnoEINVALError(); } return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream()); }
@JRubyMethod(name = "fsync") public RubyFixnum fsync(ThreadContext context) { Ruby runtime = context.runtime; try { OpenFile myOpenFile = getOpenFileChecked(); myOpenFile.checkWritable(runtime); Stream writeStream = myOpenFile.getWriteStream(); writeStream.fflush(); writeStream.sync(); } catch (InvalidValueException ex) { throw runtime.newErrnoEINVALError(); } catch (IOException e) { throw runtime.newIOErrorFromException(e); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } return RubyFixnum.zero(runtime); }
@JRubyMethod(name = "fsync") public RubyFixnum fsync(ThreadContext context) { Ruby runtime = context.runtime; try { OpenFile myOpenFile = getOpenFileChecked(); myOpenFile.checkWritable(runtime); Stream writeStream = myOpenFile.getWriteStream(); writeStream.fflush(); writeStream.sync(); } catch (InvalidValueException ex) { throw runtime.newErrnoEINVALError(); } catch (IOException e) { throw runtime.newIOErrorFromException(e); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } return RubyFixnum.zero(runtime); }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject arg) { RubyInteger newLength = arg.convertToInteger(); if (newLength.getLongValue() < 0) { throw context.runtime.newErrnoEINVALError(path); } try { openFile.checkWritable(context.runtime); openFile.getMainStreamSafe().ftruncate(newLength.getLongValue()); } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (PipeException e) { throw context.runtime.newErrnoESPIPEError(); } catch (InvalidValueException ex) { throw context.runtime.newErrnoEINVALError(); } catch (IOException e) { // Should we do anything? } return RubyFixnum.zero(context.runtime); }
@JRubyMethod(required = 1) public IRubyObject truncate(ThreadContext context, IRubyObject arg) { RubyInteger newLength = arg.convertToInteger(); if (newLength.getLongValue() < 0) { throw context.runtime.newErrnoEINVALError(path); } try { openFile.checkWritable(context.runtime); openFile.getMainStreamSafe().ftruncate(newLength.getLongValue()); } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (PipeException e) { throw context.runtime.newErrnoESPIPEError(); } catch (InvalidValueException ex) { throw context.runtime.newErrnoEINVALError(); } catch (IOException e) { // Should we do anything? } return RubyFixnum.zero(context.runtime); }
public static IRubyObject putc(ThreadContext context, IRubyObject maybeIO, IRubyObject object) { int c = RubyNumeric.num2chr(object); if (maybeIO instanceof RubyIO) { // FIXME we should probably still be dyncalling 'write' here RubyIO io = (RubyIO)maybeIO; try { OpenFile myOpenFile = io.getOpenFileChecked(); myOpenFile.checkWritable(context.runtime); Stream writeStream = myOpenFile.getWriteStream(); writeStream.fputc(c); if (myOpenFile.isSync()) myOpenFile.fflush(writeStream); } catch (IOException ex) { throw context.runtime.newIOErrorFromException(ex); } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (InvalidValueException ex) { throw context.runtime.newErrnoEINVALError(); } } else { maybeIO.callMethod(context, "write", RubyString.newStringNoCopy(context.runtime, new byte[] {(byte)c})); } return object; }
public static IRubyObject putc(ThreadContext context, IRubyObject maybeIO, IRubyObject object) { int c = RubyNumeric.num2chr(object); if (maybeIO instanceof RubyIO) { // FIXME we should probably still be dyncalling 'write' here RubyIO io = (RubyIO)maybeIO; try { OpenFile myOpenFile = io.getOpenFileChecked(); myOpenFile.checkWritable(context.runtime); Stream writeStream = myOpenFile.getWriteStream(); writeStream.fputc(c); if (myOpenFile.isSync()) myOpenFile.fflush(writeStream); } catch (IOException ex) { throw context.runtime.newIOErrorFromException(ex); } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (InvalidValueException ex) { throw context.runtime.newErrnoEINVALError(); } } else { maybeIO.callMethod(context, "write", RubyString.newStringNoCopy(context.runtime, new byte[] {(byte)c})); } return object; }
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); }