Package org.jruby

Examples of org.jruby.RubyIO


        return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream());
    }

    @JRubyMethod
    public static IRubyObject to_channel(ThreadContext context, IRubyObject self) {
        RubyIO io = (RubyIO)self;

        return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getChannel());
    }
View Full Code Here


        }
       
        @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;
            synchronized (nim.channel.blockingLock()) {
                boolean oldBlocking = nim.channel.isBlocking();

                try {
                    selector = SelectorFactory.openWithRetryFrom(recv.getRuntime(), SelectorProvider.provider());
                    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

    public static IRubyObject nread(ThreadContext context, IRubyObject _io) {
        Ruby runtime = context.runtime;
        OpenFile fptr;
        int len;
//        ioctl_arg n;
        RubyIO io = (RubyIO)_io;

        fptr = io.getOpenFileChecked();
        fptr.checkReadable(context);
        len = fptr.readPending();
        if (len > 0) return runtime.newFixnum(len);
        // TODO: better effort to get available bytes from our channel
//        if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0);
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 _io) {
        Ruby runtime = context.runtime;
        RubyIO io = (RubyIO)_io;
        OpenFile fptr;
//        ioctl_arg n;

        fptr = io.getOpenFileChecked();
        fptr.checkReadable(context);
        if (fptr.readPending() != 0) return runtime.getTrue();
        // TODO: better effort to get available bytes from our channel
//        if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qnil;
//        if (ioctl(fptr->fd, FIONREAD, &n)) return Qnil;
View Full Code Here

        return runtime.getFalse();
    }

    @JRubyMethod(optional = 1)
    public static IRubyObject wait_readable(ThreadContext context, IRubyObject _io, IRubyObject[] argv) {
        RubyIO io = (RubyIO)_io;
        Ruby runtime = context.runtime;
        OpenFile fptr;
        boolean i;
//        ioctl_arg n;
        IRubyObject timeout;
        long tv;
//        struct timeval timerec;
//        struct timeval *tv;

        fptr = io.getOpenFileChecked();
        fptr.checkReadable(context);

        switch (argv.length) {
            case 1:
                timeout = argv[0];
View Full Code Here

    /**
     * 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;
        boolean i;
        IRubyObject timeout;
        long tv;

        fptr = io.getOpenFileChecked();
        fptr.checkWritable(context);

        switch (argv.length) {
            case 1:
                timeout = argv[0];
View Full Code Here

        RubyArray writeAry = null;
        if (!write.isNil()) {
            writeAry = write.convertToArray();
            for (i = 0; i < writeAry.size(); i++) {
                RubyIO write_io = TypeConverter.ioGetIO(runtime, writeAry.eltOk(i)).GetWriteIO();
                fptr = write_io.getOpenFileChecked();
                fdSetWrite(context, fptr.fd(), writeAry.size());
            }
        }

        RubyArray exceptAry = null;
        if (!except.isNil()) {
            // This does not actually register anything because we do not have a way to select for error on JDK.
            // We make the calls for their side effects.
            exceptAry = except.convertToArray();
            for (i = 0; i < exceptAry.size(); i++) {
                RubyIO io = TypeConverter.ioGetIO(runtime, exceptAry.eltOk(i));
                RubyIO write_io = io.GetWriteIO();
                fptr = io.getOpenFileChecked();
                if (io != write_io) {
                    fptr = write_io.getOpenFileChecked();
                }
            }
        }

        int n = threadFdSelect(context);

        if (n == 0 && pendingReadFDs == null && n == 0 && unselectableReadFDs == null && unselectableWriteFDs == null) return context.nil; /* returns nil on timeout */

        res = RubyArray.newArray(runtime, 3);
        res.push(runtime.newArray(Math.min(n, maxReadReadySize())));
        res.push(runtime.newArray(Math.min(n, maxWriteReadySize())));
        // we never add anything for error since JDK does not provide a way to select for error
        res.push(runtime.newArray(0));

        if (readKeyList != null) {
            list = (RubyArray) res.eltOk(0);
            for (i = 0; i < readAry.size(); i++) {
                IRubyObject obj = readAry.eltOk(i);
                RubyIO io = TypeConverter.ioGetIO(runtime, obj);
                fptr = io.getOpenFileChecked();
                if (fdIsSet(readKeyList, fptr.fd(), READ_ACCEPT_OPS) || (pendingReadFDs != null && pendingReadFDs.contains(fptr.fd()))) {
                    list.push(obj);
                }
            }
        }
        if (unselectableReadFDs != null) {
            list = (RubyArray) res.eltOk(0);
            for (i = 0; i < readAry.size(); i++) {
                IRubyObject obj = readAry.eltOk(i);
                RubyIO io = TypeConverter.ioGetIO(runtime, obj);
                fptr = io.getOpenFileChecked();
                if (unselectableReadFDs.contains(fptr.fd())) {
                    list.push(obj);
                }
            }
        }

        if (writeKeyList != null) {
            list = (RubyArray) res.eltOk(1);
            for (i = 0; i < writeAry.size(); i++) {
                IRubyObject obj = writeAry.eltOk(i);
                RubyIO io = TypeConverter.ioGetIO(runtime, obj);
                RubyIO write_io = io.GetWriteIO();
                fptr = write_io.getOpenFileChecked();
                if (fdIsSet(writeKeyList, fptr.fd(), WRITE_CONNECT_OPS)) {
                    list.push(obj);
                }
            }
        }
        if (unselectableWriteFDs != null) {
            list = (RubyArray) res.eltOk(1);
            for (i = 0; i < writeAry.size(); i++) {
                IRubyObject obj = writeAry.eltOk(i);
                RubyIO io = TypeConverter.ioGetIO(runtime, obj);
                fptr = io.getOpenFileChecked();
                if (unselectableWriteFDs.contains(fptr.fd())) {
                    list.push(obj);
                }
            }
        }

        if (errorKeyList != null) {
            list = (RubyArray) res.eltOk(2);
            for (i = 0; i < exceptAry.size(); i++) {
                IRubyObject obj = exceptAry.eltOk(i);
                RubyIO io = TypeConverter.ioGetIO(runtime, obj);
                RubyIO write_io = io.GetWriteIO();
                fptr = io.getOpenFileChecked();
                if (errorKeyList.contains(fptr.fd())) {
                    list.push(obj);
                } else if (io != write_io) {
                    fptr = write_io.getOpenFileChecked();
                    if (errorKeyList.contains(fptr.fd())) {
                        list.push(obj);
                    }
                }
            }
View Full Code Here

                readArray = null;
            } else {
                readIOs = new RubyIO[readSize];
                Map<Character,Integer> attachment = new HashMap<Character,Integer>(1);
                for (int i = 0; i < readSize; i++) {
                    RubyIO ioObj = saveReadIO(i, context);
                    saveReadBlocking(ioObj, i);
                    saveBufferedRead(ioObj, i);                   
                    attachment.clear();
                    attachment.put('r', i);
                    trySelectRead(context, attachment, ioObj.getOpenFileChecked());
                }
            }
        }
    }
View Full Code Here

        }
    }

    private RubyIO saveReadIO(int i, ThreadContext context) {
        IRubyObject obj = readArray.eltOk(i);
        RubyIO ioObj = RubyIO.convertToIO(context, obj);
        readIOs[i] = ioObj;
        return ioObj;
    }
View Full Code Here

                writeArray = null;
            } else {
                writeIOs = new RubyIO[writeSize];
                Map<Character,Integer> attachment = new HashMap<Character,Integer>(1);
                for (int i = 0; i < writeSize; i++) {
                    RubyIO ioObj = saveWriteIO(i, context);
                    saveWriteBlocking(ioObj, i);
                    attachment.clear();                   
                    attachment.put('w', i);
                    trySelectWrite(context, attachment, ioObj.getOpenFileChecked());
                }
            }
        }
    }
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.