Exemplo n.º 1
0
  @JRubyMethod(name = "initialize", rest = true)
  public IRubyObject _initialize(IRubyObject[] args, Block block) {
    store.setVerifyCallbackFunction(ossl_verify_cb);
    this.set_verify_callback(getRuntime().getNil());
    this.setInstanceVariable("@flags", RubyFixnum.zero(getRuntime()));
    this.setInstanceVariable("@purpose", RubyFixnum.zero(getRuntime()));
    this.setInstanceVariable("@trust", RubyFixnum.zero(getRuntime()));

    this.setInstanceVariable("@error", getRuntime().getNil());
    this.setInstanceVariable("@error_string", getRuntime().getNil());
    this.setInstanceVariable("@chain", getRuntime().getNil());
    this.setInstanceVariable("@time", getRuntime().getNil());
    return this;
  }
Exemplo n.º 2
0
  private IRubyObject shutdownInternal(ThreadContext context, int how)
      throws BadDescriptorException {
    Ruby runtime = context.runtime;
    Channel channel;

    switch (how) {
      case 0:
        channel = getOpenChannel();
        try {
          SocketType.forChannel(channel).shutdownInput(channel);

        } catch (IOException e) {
          throw runtime.newIOError(e.getMessage());
        }

        if (openFile.getPipeStream() != null) {
          openFile.setMainStream(openFile.getPipeStream());
          openFile.setPipeStream(null);
        }

        openFile.setMode(openFile.getMode() & ~OpenFile.READABLE);

        return RubyFixnum.zero(runtime);

      case 1:
        channel = getOpenChannel();
        try {
          SocketType.forChannel(channel).shutdownOutput(channel);

        } catch (IOException e) {
          throw runtime.newIOError(e.getMessage());
        }

        openFile.setPipeStream(null);
        openFile.setMode(openFile.getMode() & ~OpenFile.WRITABLE);

        return RubyFixnum.zero(runtime);

      case 2:
        shutdownInternal(context, 0);
        shutdownInternal(context, 1);

        return RubyFixnum.zero(runtime);

      default:
        throw runtime.newArgumentError("`how' should be either :SHUT_RD, :SHUT_WR, :SHUT_RDWR");
    }
  }
Exemplo n.º 3
0
  @JRubyMethod(required = 1, optional = 1)
  public IRubyObject seek(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;

    checkFrozen();
    checkFinalized();

    int offset = RubyNumeric.num2int(args[0]);
    IRubyObject whence = context.nil;

    if (args.length > 1 && !args[0].isNil()) whence = args[1];

    checkOpen();

    switch (whence.isNil() ? 0 : RubyNumeric.num2int(whence)) {
      case 0:
        break;
      case 1:
        offset += ptr.pos;
        break;
      case 2:
        offset += ptr.string.size();
        break;
      default:
        throw runtime.newErrnoEINVALError("invalid whence");
    }

    if (offset < 0) throw runtime.newErrnoEINVALError("invalid seek value");

    ptr.pos = offset;

    return RubyFixnum.zero(runtime);
  }
Exemplo n.º 4
0
  @JRubyMethod(required = 1, optional = 1)
  @Override
  public IRubyObject seek(IRubyObject[] args) {
    checkOpen();
    checkFinalized();
    long amount = RubyNumeric.num2long(args[0]);
    int whence = Stream.SEEK_SET;
    long newPosition = data.pos;

    if (args.length > 1 && !args[0].isNil()) whence = RubyNumeric.fix2int(args[1]);

    if (whence == Stream.SEEK_CUR) {
      newPosition += amount;
    } else if (whence == Stream.SEEK_END) {
      newPosition = data.internal.getByteList().length() + amount;
    } else if (whence == Stream.SEEK_SET) {
      newPosition = amount;
    } else {
      throw getRuntime().newErrnoEINVALError("invalid whence");
    }

    if (newPosition < 0) throw getRuntime().newErrnoEINVALError("invalid seek value");

    data.pos = newPosition;
    data.eof = false;

    return RubyFixnum.zero(getRuntime());
  }
Exemplo n.º 5
0
  @JRubyMethod(name = "rewind")
  public IRubyObject rewind(ThreadContext context) {
    checkInitialized();

    this.ptr.pos = 0;
    this.ptr.lineno = 0;
    return RubyFixnum.zero(context.runtime);
  }
Exemplo n.º 6
0
  @JRubyMethod(name = {"size", "length"})
  public IRubyObject size(ThreadContext context) {
    if (!isClosed()) {
      flush();
      return context.getRuntime().newFileStat(path, false).size();
    }

    return RubyFixnum.zero(context.getRuntime());
  }
Exemplo n.º 7
0
 private IRubyObject shutdownInternal(ThreadContext context, int how) {
   Channel socketChannel;
   switch (how) {
     case 0:
       socketChannel = openFile.getMainStream().getDescriptor().getChannel();
       try {
         if (socketChannel instanceof SocketChannel || socketChannel instanceof DatagramChannel) {
           asSocket().shutdownInput();
         } else if (socketChannel instanceof Shutdownable) {
           ((Shutdownable) socketChannel).shutdownInput();
         }
       } catch (IOException e) {
         throw context.getRuntime().newIOError(e.getMessage());
       }
       if (openFile.getPipeStream() != null) {
         openFile.setMainStream(openFile.getPipeStream());
         openFile.setPipeStream(null);
       }
       openFile.setMode(openFile.getMode() & ~OpenFile.READABLE);
       return RubyFixnum.zero(context.getRuntime());
     case 1:
       socketChannel = openFile.getMainStream().getDescriptor().getChannel();
       try {
         if (socketChannel instanceof SocketChannel || socketChannel instanceof DatagramChannel) {
           asSocket().shutdownOutput();
         } else if (socketChannel instanceof Shutdownable) {
           ((Shutdownable) socketChannel).shutdownOutput();
         }
       } catch (IOException e) {
         throw context.getRuntime().newIOError(e.getMessage());
       }
       openFile.setPipeStream(null);
       openFile.setMode(openFile.getMode() & ~OpenFile.WRITABLE);
       return RubyFixnum.zero(context.getRuntime());
     case 2:
       shutdownInternal(context, 0);
       shutdownInternal(context, 1);
       return RubyFixnum.zero(context.getRuntime());
     default:
       throw context.getRuntime().newArgumentError("`how' should be either 0, 1, 2");
   }
 }
Exemplo n.º 8
0
  @JRubyMethod(
      name = {"write"},
      required = 1)
  public IRubyObject write(ThreadContext context, IRubyObject arg) {
    checkWritable();

    Ruby runtime = context.runtime;

    RubyString str = arg.asString();
    int len, olen;
    Encoding enc, enc2;

    enc = ptr.string.getEncoding();
    enc2 = str.getEncoding();
    if (enc != enc2
        && enc != EncodingUtils.ascii8bitEncoding(runtime)
        // this is a hack because we don't seem to handle incoming ASCII-8BIT properly in transcoder
        && enc2 != ASCIIEncoding.INSTANCE) {
      str = runtime.newString(Transcoder.strConvEnc(context, str.getByteList(), enc2, enc));
    }
    len = str.size();
    if (len == 0) return RubyFixnum.zero(runtime);
    checkModifiable();
    olen = ptr.string.size();
    if ((ptr.flags & OpenFile.APPEND) != 0) {
      ptr.pos = olen;
    }
    if (ptr.pos == olen
        // this is a hack because we don't seem to handle incoming ASCII-8BIT properly in transcoder
        && enc2 != ASCIIEncoding.INSTANCE) {
      EncodingUtils.encStrBufCat(runtime, ptr.string, str.getByteList(), enc);
    } else {
      strioExtend(ptr.pos, len);
      ByteList ptrByteList = ptr.string.getByteList();
      System.arraycopy(
          str.getByteList().getUnsafeBytes(),
          str.getByteList().getBegin(),
          ptrByteList.getUnsafeBytes(),
          ptrByteList.begin + ptr.pos,
          len);
      ptr.string.infectBy(str);
    }
    ptr.string.infectBy(this);
    ptr.pos += len;
    return RubyFixnum.newFixnum(runtime, len);
  }
Exemplo n.º 9
0
  @JRubyMethod()
  public IRubyObject bind(ThreadContext context, IRubyObject addr, IRubyObject backlog) {
    final InetSocketAddress iaddr;

    if (addr instanceof Addrinfo) {
      Addrinfo addrInfo = (Addrinfo) addr;
      if (!addrInfo.ip_p(context).isTrue()) {
        throw context.runtime.newTypeError("not an INET or INET6 address: " + addrInfo);
      }
      iaddr = new InetSocketAddress(addrInfo.getInetAddress().getHostAddress(), addrInfo.getPort());
    } else {
      iaddr = Sockaddr.addressFromSockaddr_in(context, addr);
    }
    doBind(context, getChannel(), iaddr, RubyFixnum.fix2int(backlog));

    return RubyFixnum.zero(context.runtime);
  }
Exemplo n.º 10
0
  @Override
  protected IRubyObject fillCommon(ThreadContext context, int beg, long len, Block block) {
    if (!packed()) return super.fillCommon(context, beg, len, block);

    modifyCheck();

    // See [ruby-core:17483]
    if (len <= 0) return this;

    if (len > Integer.MAX_VALUE - beg) throw context.runtime.newArgumentError("argument too big");

    if (len > 1) {
      unpack();
      return super.fillCommon(context, beg, len, block);
    }

    value = block.yield(context, RubyFixnum.zero(context.runtime));

    return this;
  }
Exemplo n.º 11
0
 @JRubyMethod(name = {"fsync"})
 public IRubyObject strioZero(ThreadContext context) {
   return RubyFixnum.zero(context.runtime);
 }
Exemplo n.º 12
0
 @JRubyMethod(name = "listen", required = 1)
 public IRubyObject listen(ThreadContext context, IRubyObject backlog) {
   return RubyFixnum.zero(context.getRuntime());
 }
Exemplo n.º 13
0
 @JRubyMethod()
 public IRubyObject bind(ThreadContext context, IRubyObject addr) {
   return bind(context, addr, RubyFixnum.zero(context.runtime));
 }
Exemplo n.º 14
0
 @JRubyMethod(name = "rewind")
 @Override
 public IRubyObject rewind() {
   doRewind();
   return RubyFixnum.zero(getRuntime());
 }
Exemplo n.º 15
0
 @JRubyMethod(name = "fsync")
 @Override
 public IRubyObject fsync() {
   return RubyFixnum.zero(getRuntime());
 }