@JRubyMethod( name = {"new_out", "alloc_out", "__alloc_out"}, meta = true) public static Buffer allocateOutput( ThreadContext context, IRubyObject recv, IRubyObject sizeArg, IRubyObject countArg, IRubyObject clearArg) { return allocate(context, recv, sizeArg, RubyFixnum.fix2int(countArg), OUT); }
@JRubyMethod(name = "truncate", required = 1) public IRubyObject truncate(IRubyObject len) { checkWritable(); int l = RubyFixnum.fix2int(len); int plen = ptr.string.size(); if (l < 0) { throw getRuntime().newErrnoEINVALError("negative legnth"); } ptr.string.resize(l); ByteList buf = ptr.string.getByteList(); if (plen < l) { // zero the gap Arrays.fill(buf.getUnsafeBytes(), buf.getBegin() + plen, buf.getBegin() + l, (byte) 0); } return len; }
@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); }
@JRubyMethod(name = "truncate", required = 1) @Override public IRubyObject truncate(IRubyObject arg) { checkWritable(); int len = RubyFixnum.fix2int(arg); if (len < 0) { throw getRuntime().newErrnoEINVALError("negative legnth"); } data.internal.modify(); ByteList buf = data.internal.getByteList(); if (len < buf.length()) { Arrays.fill(buf.getUnsafeBytes(), len, buf.length(), (byte) 0); } buf.length(len); return arg; }
private void strioInit(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.runtime; RubyString string; IRubyObject mode; boolean trunc = false; switch (args.length) { case 2: mode = args[1]; if (mode instanceof RubyFixnum) { int flags = RubyFixnum.fix2int(mode); ptr.flags = ModeFlags.getOpenFileFlagsFor(flags); trunc = (flags & ModeFlags.TRUNC) != 0; } else { String m = args[1].convertToString().toString(); ptr.flags = OpenFile.ioModestrFmode(runtime, m); trunc = m.charAt(0) == 'w'; } string = args[0].convertToString(); if ((ptr.flags & OpenFile.WRITABLE) != 0 && string.isFrozen()) { throw runtime.newErrnoEACCESError("Permission denied"); } if (trunc) { string.resize(0); } break; case 1: string = args[0].convertToString(); ptr.flags = string.isFrozen() ? OpenFile.READABLE : OpenFile.READWRITE; break; case 0: string = RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding()); ptr.flags = OpenFile.READWRITE; break; default: throw runtime.newArgumentError(args.length, 2); } ptr.string = string; ptr.pos = 0; ptr.lineno = 0; // funky way of shifting readwrite flags into object flags flags |= (ptr.flags & OpenFile.READWRITE) * (STRIO_READABLE / OpenFile.READABLE); }
public static Integer integerOrNull(IRubyObject obj) { return (!obj.isNil()) ? RubyFixnum.fix2int(obj) : null; }
public static int intOrMinusOne(IRubyObject obj) { return (!obj.isNil()) ? RubyFixnum.fix2int(obj) : -1; }
private static final int getCount(IRubyObject countArg) { return countArg instanceof RubyFixnum ? RubyFixnum.fix2int(countArg) : 1; }
public static void buildAddrinfoList( ThreadContext context, IRubyObject[] args, AddrinfoCallback callback) { Ruby runtime = context.runtime; IRubyObject host = args[0]; IRubyObject port = args[1]; boolean emptyHost = host.isNil() || host.convertToString().isEmpty(); try { if (port instanceof RubyString) { port = getservbyname(context, new IRubyObject[] {port}); } IRubyObject family = args.length > 2 ? args[2] : context.nil; IRubyObject socktype = args.length > 3 ? args[3] : context.nil; // IRubyObject protocol = args[4]; IRubyObject flags = args.length > 5 ? args[5] : context.nil; IRubyObject reverseArg = args.length > 6 ? args[6] : context.nil; // The Ruby Socket.getaddrinfo function supports boolean/nil/Symbol values for the // reverse_lookup parameter. We need to massage all valid inputs to true/false/null. Boolean reverseLookup = null; if (reverseArg instanceof RubyBoolean) { reverseLookup = reverseArg.isTrue(); } else if (reverseArg instanceof RubySymbol) { String reverseString = reverseArg.toString(); if ("hostname".equals(reverseString)) { reverseLookup = true; } else if ("numeric".equals(reverseString)) { reverseLookup = false; } else { throw runtime.newArgumentError("invalid reverse_lookup flag: :" + reverseString); } } AddressFamily addressFamily = AF_INET; if (!family.isNil()) { addressFamily = addressFamilyFromArg(family); } boolean is_ipv6 = addressFamily == AddressFamily.AF_INET6; boolean sock_stream = true; boolean sock_dgram = true; Sock sock = SOCK_STREAM; if (!socktype.isNil()) { sockFromArg(socktype); if (sock == SOCK_STREAM) { sock_dgram = false; } else if (sock == SOCK_DGRAM) { sock_stream = false; } } // When Socket::AI_PASSIVE and host is nil, return 'any' address. InetAddress[] addrs = null; if (!flags.isNil() && RubyFixnum.fix2int(flags) > 0) { // The value of 1 is for Socket::AI_PASSIVE. int flag = RubyNumeric.fix2int(flags); if ((flag == 1) && emptyHost) { // use RFC 2732 style string to ensure that we get Inet6Address addrs = InetAddress.getAllByName(is_ipv6 ? "[::]" : "0.0.0.0"); } } if (addrs == null) { addrs = InetAddress.getAllByName( emptyHost ? (is_ipv6 ? "[::1]" : null) : host.convertToString().toString()); } for (int i = 0; i < addrs.length; i++) { int p = port.isNil() ? 0 : (int) port.convertToInteger().getLongValue(); callback.addrinfo(addrs[i], p, sock, reverseLookup); } } catch (UnknownHostException e) { throw sockerr(runtime, "getaddrinfo: name or service not known"); } }