Example #1
0
    @JRubyMethod
    public IRubyObject close(ThreadContext context, IRubyObject deregister) {
      Ruby runtime = context.getRuntime();
      this.closed = runtime.getTrue();

      if (deregister == runtime.getTrue()) {
        selector.callMethod(context, "deregister", io);
      }

      return context.nil;
    }
Example #2
0
 @JRubyMethod(name = {"eof", "eof?"})
 public IRubyObject eof(ThreadContext context) {
   checkReadable();
   Ruby runtime = context.runtime;
   if (ptr.pos < ptr.string.size()) return runtime.getFalse();
   return runtime.getTrue();
 }
 /**
  * Take the boolean value and convert it to its bytes.
  *
  * @param bool The Ruby boolean value.
  * @return The byte array.
  * @since 2.0.0
  */
 private static RubyString toBsonBoolean(final IRubyObject bool) {
   final Ruby runtime = bool.getRuntime();
   if (bool == runtime.getTrue()) {
     return RubyString.newString(runtime, TRUE_BYTES);
   } else {
     return RubyString.newString(runtime, FALSE_BYTES);
   }
 }
Example #4
0
    @JRubyMethod(name = {"writable?", "writeable?"})
    public IRubyObject writable(ThreadContext context) {
      Ruby runtime = context.getRuntime();
      int readyOps = this.key.readyOps();

      if ((readyOps & SelectionKey.OP_WRITE) != 0 || (readyOps & SelectionKey.OP_CONNECT) != 0) {
        return runtime.getTrue();
      } else {
        return runtime.getFalse();
      }
    }
Example #5
0
    @JRubyMethod(name = "readable?")
    public IRubyObject isReadable(ThreadContext context) {
      Ruby runtime = context.getRuntime();
      int readyOps = this.key.readyOps();

      if ((readyOps & SelectionKey.OP_READ) != 0 || (readyOps & SelectionKey.OP_ACCEPT) != 0) {
        return runtime.getTrue();
      } else {
        return runtime.getFalse();
      }
    }
Example #6
0
    @JRubyMethod(name = "registered?")
    public IRubyObject isRegistered(ThreadContext context, IRubyObject io) {
      Ruby runtime = context.getRuntime();
      Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();

      if (!(rawChannel instanceof SelectableChannel)) {
        throw runtime.newArgumentError("not a selectable IO object");
      }

      SelectableChannel channel = (SelectableChannel) rawChannel;
      SelectionKey key = channel.keyFor(this.selector);

      if (key == null) return context.nil;

      if (((Monitor) key.attachment()).isClosed(context) == runtime.getTrue()) {
        return runtime.getFalse();
      } else {
        return runtime.getTrue();
      }
    }
Example #7
0
 @JRubyMethod(name = "empty?")
 public IRubyObject isEmpty(ThreadContext context) {
   Ruby runtime = context.getRuntime();
   return this.selector.keys().isEmpty() ? runtime.getTrue() : runtime.getFalse();
 }
Example #8
0
 @JRubyMethod(name = "closed?")
 public IRubyObject isClosed(ThreadContext context) {
   Ruby runtime = context.getRuntime();
   return this.selector.isOpen() ? runtime.getFalse() : runtime.getTrue();
 }
Example #9
0
  private IRubyObject unmarshalObjectDirectly(int type, MarshalState state, boolean callProc)
      throws IOException {
    IRubyObject rubyObj = null;
    switch (type) {
      case 'I':
        MarshalState childState = new MarshalState(true);
        rubyObj = unmarshalObject(childState);
        if (childState.isIvarWaiting()) {
          defaultVariablesUnmarshal(rubyObj);
        }
        return rubyObj;
      case '0':
        rubyObj = runtime.getNil();
        break;
      case 'T':
        rubyObj = runtime.getTrue();
        break;
      case 'F':
        rubyObj = runtime.getFalse();
        break;
      case '"':
        rubyObj = RubyString.unmarshalFrom(this);
        break;
      case 'i':
        rubyObj = RubyFixnum.unmarshalFrom(this);
        break;
      case 'f':
        rubyObj = RubyFloat.unmarshalFrom(this);
        break;
      case '/':
        rubyObj = RubyRegexp.unmarshalFrom(this);
        break;
      case ':':
        rubyObj = RubySymbol.unmarshalFrom(this);
        break;
      case '[':
        rubyObj = RubyArray.unmarshalFrom(this);
        break;
      case '{':
        rubyObj = RubyHash.unmarshalFrom(this, false);
        break;
      case '}':
        // "hashdef" object, a hash with a default
        rubyObj = RubyHash.unmarshalFrom(this, true);
        break;
      case 'c':
        rubyObj = RubyClass.unmarshalFrom(this);
        break;
      case 'm':
        rubyObj = RubyModule.unmarshalFrom(this);
        break;
      case 'e':
        RubySymbol moduleName = (RubySymbol) unmarshalObject();
        RubyModule tp = null;
        try {
          tp = runtime.getClassFromPath(moduleName.asJavaString());
        } catch (RaiseException e) {
          if (runtime.getModule("NameError").isInstance(e.getException())) {
            throw runtime.newArgumentError("undefined class/module " + moduleName.asJavaString());
          }
          throw e;
        }

        rubyObj = unmarshalObject();

        tp.extend_object(rubyObj);
        tp.callMethod(runtime.getCurrentContext(), "extended", rubyObj);
        break;
      case 'l':
        rubyObj = RubyBignum.unmarshalFrom(this);
        break;
      case 'S':
        rubyObj = RubyStruct.unmarshalFrom(this);
        break;
      case 'o':
        rubyObj = defaultObjectUnmarshal();
        break;
      case 'u':
        rubyObj = userUnmarshal(state);
        break;
      case 'U':
        rubyObj = userNewUnmarshal();
        break;
      case 'C':
        rubyObj = uclassUnmarshall();
        break;
      default:
        throw getRuntime().newArgumentError("dump format error(" + (char) type + ")");
    }

    if (callProc) {
      return doCallProcForObj(rubyObj);
    }

    return rubyObj;
  }