Package org.jruby

Examples of org.jruby.RubyIO


        }
       
        @JRubyMethod
        public static IRubyObject rbuf_fill(IRubyObject recv) {
            RubyString buf = (RubyString)recv.getInstanceVariables().getInstanceVariable("@rbuf");
            RubyIO io = (RubyIO)recv.getInstanceVariables().getInstanceVariable("@io");

            int timeout = RubyNumeric.fix2int(recv.getInstanceVariables().getInstanceVariable("@read_timeout")) * 1000;
            NativeImpl nim = (NativeImpl)recv.dataGetStruct();

            Selector selector = null;
            try {
                selector = Selector.open();
                nim.channel.configureBlocking(false);
                SelectionKey key = nim.channel.register(selector, SelectionKey.OP_READ);
                int n = selector.select(timeout);

                if(n > 0) {
                    IRubyObject readItems = io.read(new IRubyObject[]{recv.getRuntime().newFixnum(1024*16)});
                    return buf.concat(readItems);
                } else {
                    RubyClass exc = (RubyClass)(recv.getRuntime().getModule("Timeout").getConstant("Error"));
                    throw new RaiseException(RubyException.newException(recv.getRuntime(), exc, "execution expired"),false);
                }
View Full Code Here


    /**
     * returns non-nil if input available without blocking, false if EOF or not open/readable, otherwise nil.
     */
    @JRubyMethod(name = "ready?")
    public static IRubyObject ready(ThreadContext context, IRubyObject obj) {
        RubyIO io = (RubyIO)obj;
        try {
            OpenFile openFile = io.getOpenFile();
            ChannelDescriptor descriptor = openFile.getMainStream().getDescriptor();
            if (!descriptor.isOpen() || !openFile.getMainStream().getModes().isReadable() || openFile.getMainStream().feof()) {
                return context.getRuntime().getFalse();
            }

View Full Code Here

    /**
     * waits until input available or timed out and returns self, or nil when EOF reached.
     */
    @JRubyMethod
    public static IRubyObject io_wait(ThreadContext context, IRubyObject obj) {
        RubyIO io = (RubyIO)obj;
        try {
            OpenFile openFile = io.getOpenFile();
            ChannelDescriptor descriptor = openFile.getMainStream().getDescriptor();
            if (openFile.getMainStream().feof()) {
                return context.getRuntime().getNil();
            }
            openFile.getMainStream().waitUntilReady();
View Full Code Here

import org.jruby.util.io.PipeException;

public class IOJavaAddons {
    @JRubyMethod
    public static IRubyObject to_inputstream(ThreadContext context, IRubyObject self) {
        RubyIO io = (RubyIO)self;
        Ruby runtime = context.getRuntime();

        try {
            io.getOpenFile().checkReadable(context.getRuntime());
        } catch (PipeException pe) {
            throw runtime.newErrnoEPIPEError();
        } catch (IOException ex) {
            throw runtime.newIOErrorFromException(ex);
        } catch (BadDescriptorException ex) {
            throw runtime.newErrnoEBADFError();
        } catch (InvalidValueException e) {
            throw runtime.newErrnoEINVALError();
        }
       
        return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), io.getInStream());
    }
View Full Code Here

        return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), io.getInStream());
    }
   
    @JRubyMethod
    public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) {
        RubyIO io = (RubyIO)self;
        Ruby runtime = context.getRuntime();

        try {
            io.getOpenFile().checkWritable(context.getRuntime());
        } catch (PipeException pe) {
            throw runtime.newErrnoEPIPEError();
        } catch (IOException ex) {
            throw runtime.newIOErrorFromException(ex);
        } catch (BadDescriptorException ex) {
            throw runtime.newErrnoEBADFError();
        } catch (InvalidValueException e) {
            throw runtime.newErrnoEINVALError();
        }
       
        return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), io.getOutStream());
    }
View Full Code Here

        return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), io.getOutStream());
    }
   
    @JRubyMethod
    public static IRubyObject to_channel(ThreadContext context, IRubyObject self) {
        RubyIO io = (RubyIO)self;
       
        return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), io.getChannel());
    }
View Full Code Here

     * @return the value printed out on  stdout and stderr by
     **/
    protected String eval(String script) throws Exception {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        out = new PrintStream(result);
        RubyIO lStream = new RubyIO(runtime, out);
        runtime.getGlobalVariables().set("$stdout", lStream);
        runtime.getGlobalVariables().set("$>", lStream);
        runtime.getGlobalVariables().set("$stderr", lStream);
       
        runtime.runNormally(
View Full Code Here

     * @see #hookIntoRuntime(Ruby)
     */
    public void hookIntoRuntimeWithStreams(final Ruby runtime) {
        hookIntoRuntime(runtime);

        RubyIO in = new RubyIO(runtime, getInputStream());
        runtime.getGlobalVariables().set("$stdin", in);

        RubyIO out = new RubyIO(runtime, getOutputStream());
        runtime.getGlobalVariables().set("$stdout", out);
        runtime.getGlobalVariables().set("$stderr", out);
    }
View Full Code Here

    public void testPrintErrorWithStringBacktrace() throws Exception {
        testPrintErrorWithBacktrace("\"abc\"");
    }
   
    private void testPrintErrorWithBacktrace(String backtrace) throws Exception {
        RubyIO oldStderr = (RubyIO)runtime.getGlobalVariables().get("$stderr");
        try {
            ByteArrayOutputStream stderrOutput = new ByteArrayOutputStream();
            RubyIO newStderr = new RubyIO(runtime, stderrOutput);
            runtime.getGlobalVariables().set("$stderr", newStderr);
           
            try {
                eval("class MyError < StandardError ; def backtrace ; " + backtrace + " ; end ; end ; raise MyError.new ");
                fail("Expected MyError to be raised");
View Full Code Here

    /**
     * returns non-nil if input available without blocking, false if EOF or not open/readable, otherwise nil.
     */
    @JRubyMethod(name = "ready?")
    public static IRubyObject ready(ThreadContext context, IRubyObject obj) {
        RubyIO io = (RubyIO)obj;
        try {
            OpenFile openFile = io.getOpenFile();
            ChannelDescriptor descriptor = openFile.getMainStreamSafe().getDescriptor();
            if (!descriptor.isOpen() || !openFile.getMainStreamSafe().getModes().isReadable() || openFile.getMainStreamSafe().feof()) {
                return context.getRuntime().getFalse();
            }

View Full Code Here

TOP

Related Classes of org.jruby.RubyIO

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.