Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
import org.jruby.util.io.ModeFlags;
import org.jruby.util.io.OpenFile;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;

import static org.jruby.RubyEnumerator.enumeratorize;
Expand Down Expand Up @@ -1270,6 +1273,23 @@ public IRubyObject write(ThreadContext context, IRubyObject[] args) {
return RubyFixnum.newFixnum(runtime, len);
}

private static final MethodHandle CAT_WITH_CODE_RANGE;

static {
MethodHandle cat;
try {
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "catWithCodeRange", MethodType.methodType(RubyString.class, RubyString.class));
} catch (NoSuchMethodException | IllegalAccessException ex) {
try {
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "cat19", MethodType.methodType(RubyString.class, RubyString.class));
} catch (NoSuchMethodException | IllegalAccessException ex2) {
throw new ExceptionInInitializerError(ex2);
}
}

CAT_WITH_CODE_RANGE = cat;
}

// MRI: strio_write
private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg) {
checkWritable();
Expand Down Expand Up @@ -1299,7 +1319,11 @@ private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg)
if (enc == EncodingUtils.ascii8bitEncoding(runtime) || encStr == EncodingUtils.ascii8bitEncoding(runtime)) {
EncodingUtils.encStrBufCat(runtime, ptr.string, strByteList, enc);
} else {
ptr.string.cat19(str);
try {
RubyString unused = (RubyString) CAT_WITH_CODE_RANGE.invokeExact(ptr.string, str);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
} else {
strioExtend(ptr.pos, len);
Expand Down