private IRubyObject iconv(IRubyObject str, int start, int end) { if (str.isNil()) { fromEncoding.reset(); toEncoding.reset(); return RubyString.newEmptyString(getRuntime()); } return _iconv(str.convertToString(), start, end); }
@JRubyMethod(name = "close") public IRubyObject close() { if (toEncoding == null && fromEncoding == null) { return getRuntime().getNil(); } toEncoding = null; fromEncoding = null; return RubyString.newEmptyString(getRuntime()); }
@JRubyMethod(optional = 2, visibility = PRIVATE) @Override public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) { Object modeArgument = null; Ruby runtime = getRuntime(); switch (args.length) { case 0: data.internal = runtime.is1_9() ? RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding()) : RubyString.newEmptyString(getRuntime()); modeArgument = "r+"; break; case 1: data.internal = args[0].convertToString(); modeArgument = data.internal.isFrozen() ? "r" : "r+"; break; case 2: data.internal = args[0].convertToString(); if (args[1] instanceof RubyFixnum) { modeArgument = RubyFixnum.fix2long(args[1]); } else { modeArgument = args[1].convertToString().toString(); } break; } initializeModes(modeArgument); if (data.modes.isWritable() && data.internal.isFrozen()) { throw getRuntime().newErrnoEACCESError("Permission denied"); } if (data.modes.isTruncate()) { data.internal.modifyCheck(); data.internal.empty(); } return this; }
private RubyString strioSubstr(Ruby runtime, int pos, int len) { RubyString str = ptr.string; ByteList strByteList = str.getByteList(); byte[] strBytes = strByteList.getUnsafeBytes(); Encoding enc = str.getEncoding(); int rlen = str.size() - pos; if (len > rlen) len = rlen; if (len < 0) len = 0; if (len == 0) return RubyString.newEmptyString(runtime); return RubyString.newStringShared(runtime, strBytes, strByteList.getBegin() + pos, len, enc); }
public final IRubyObject invoke(Ruby runtime, Function function, HeapInvocationBuffer args) { long address = invoker.invokeAddress(function, args); if (address == 0) { return runtime.getNil(); } int len = (int) IO.getStringLength(address); if (len == 0) { return RubyString.newEmptyString(runtime); } byte[] bytes = new byte[len]; IO.getByteArray(address, bytes, 0, len); RubyString s = RubyString.newStringShared(runtime, bytes); s.setTaint(true); return s; }
@JRubyMethod(name = "print", rest = true, compat = CompatVersion.RUBY1_9) @Override public IRubyObject print19(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.runtime; if (args.length != 0) { for (int i = 0, j = args.length; i < j; i++) { append(context, args[i]); } } else { IRubyObject arg = runtime.getGlobalVariables().get("$_"); append(context, arg.isNil() ? RubyString.newEmptyString(getRuntime()) : arg); } IRubyObject sep = runtime.getGlobalVariables().get("$\\"); if (!sep.isNil()) append(context, sep); return runtime.getNil(); }
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); }
@JRubyMethod(required = 1, visibility = Visibility.PRIVATE) public static IRubyObject initialize(IRubyObject recv, IRubyObject io) { try { if (io instanceof RubyIO) { RubyIO rubyIO = (RubyIO) io; OpenFile of = rubyIO.getOpenFile(); Stream stream = of.getMainStreamSafe(); if (stream instanceof ChannelStream) { ChannelStream cStream = (ChannelStream) stream; if (cStream.getDescriptor().getChannel() instanceof SelectableChannel) { SelectableChannel selChannel = (SelectableChannel) cStream.getDescriptor().getChannel(); ((RubyObject) recv) .extend( new IRubyObject[] { ((RubyModule) recv.getRuntime().getModule("Net").getConstant("BufferedIO")) .getConstant("NativeImplementation") }); SelectableChannel sc = (SelectableChannel) (selChannel); recv.dataWrapStruct(new NativeImpl(sc)); } } } recv.getInstanceVariables().setInstanceVariable("@io", io); recv.getInstanceVariables() .setInstanceVariable("@read_timeout", recv.getRuntime().newFixnum(60)); recv.getInstanceVariables().setInstanceVariable("@debug_output", recv.getRuntime().getNil()); recv.getInstanceVariables() .setInstanceVariable("@rbuf", RubyString.newEmptyString(recv.getRuntime())); return recv; } catch (BadDescriptorException e) { throw recv.getRuntime().newErrnoEBADFError(); } }
@JRubyMethod(name = "conv", required = 3, rest = true, meta = true) public static IRubyObject conv( ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) { return convertWithArgs(context, recv, args, "conv") .join(context, RubyString.newEmptyString(recv.getRuntime())); }
// FIXME: We are assuming that original string will be raw bytes. If -Ku is provided // this will not be true, but that is ok for now. Deal with that when someone needs it. private IRubyObject _iconv(RubyString str, int start, int length) { if (fromEncoding == null) { throw getRuntime().newArgumentError("closed iconv"); } ByteList bytes = str.getByteList(); // treat start and end as start...end for end >= 0, start..end for end < 0 if (start < 0) { start += bytes.length(); } if (start < 0 || start > bytes.length()) { // invalid ranges result in an empty string return RubyString.newEmptyString(getRuntime()); } if (length < 0 || length > bytes.length() - start) { length = bytes.length() - start; } ByteBuffer buf = ByteBuffer.wrap(bytes.getUnsafeBytes(), bytes.begin() + start, length); try { CharBuffer cbuf = fromEncoding.decode(buf); buf = toEncoding.encode(cbuf); } catch (MalformedInputException e) { throw getRuntime().newIllegalSequence(str.toString()); } catch (UnmappableCharacterException e) { throw getRuntime().newIllegalSequence(str.toString()); } catch (CharacterCodingException e) { throw getRuntime().newInvalidEncoding("invalid sequence"); } catch (IllegalStateException e) { throw getRuntime().newIllegalSequence(str.toString()); } byte[] arr = buf.array(); start = 0; String displayName = toEncoding.charset().displayName(); if (arr.length >= 2) { // minimum Byte Order Mark (BOM) length if (displayName.toLowerCase().startsWith("utf-16")) { if ((arr[0] == (byte) 0xfe && arr[1] == (byte) 0xff)) { if (count > 0) start = 2; endian = "BE"; } else if (arr[0] == (byte) 0xff && arr[1] == (byte) 0xfe) { if (count > 0) start = 2; endian = "LE"; } } else if (displayName.toLowerCase().startsWith("utf-32") && arr.length >= 4) { if (arr[0] == (byte) 0x00 && arr[1] == (byte) 0x00 && arr[2] == (byte) 0xfe && arr[3] == (byte) 0xff) { if (count > 0) start = 4; endian = "BE"; } else if (arr[0] == (byte) 0xff && arr[1] == (byte) 0xfe && arr[2] == (byte) 0x00 && arr[3] == (byte) 0x00) { if (count > 0) start = 4; endian = "LE"; } } } count++; if (displayName.equalsIgnoreCase("utf-16") || displayName.equalsIgnoreCase("utf-32")) { displayName += endian; } ByteList r = new ByteList(arr, start, buf.limit() - start); EncodingDB.Entry entry = EncodingDB.getEncodings().get(displayName.getBytes()); if (entry != null) { Encoding charset = entry.getEncoding(); r.setEncoding(charset); } return getRuntime().newString(r); }
@JRubyMethod(name = "readline", module = true, visibility = Visibility.PRIVATE) public static IRubyObject s_readline(IRubyObject recv) throws IOException { return s_readline( recv, RubyString.newEmptyString(recv.getRuntime()), recv.getRuntime().getFalse()); }