|
10 | 10 | import java.io.ByteArrayOutputStream;
|
11 | 11 | import java.io.IOException;
|
12 | 12 | import java.io.InputStream;
|
| 13 | +import java.lang.invoke.MethodHandle; |
| 14 | +import java.lang.invoke.MethodHandles; |
| 15 | +import java.lang.invoke.MethodType; |
13 | 16 | import java.lang.reflect.InvocationTargetException;
|
14 | 17 | import java.lang.reflect.Method;
|
15 | 18 |
|
|
21 | 24 | import org.jruby.anno.JRubyMethod;
|
22 | 25 | import org.jruby.javasupport.JavaEmbedUtils;
|
23 | 26 | import org.jruby.runtime.Block;
|
| 27 | +import org.jruby.runtime.Helpers; |
24 | 28 | import org.jruby.runtime.ObjectAllocator;
|
25 | 29 | import org.jruby.runtime.ThreadContext;
|
26 | 30 | import org.jruby.runtime.builtin.IRubyObject;
|
|
39 | 43 | */
|
40 | 44 | @SuppressWarnings("serial")
|
41 | 45 | public class Input extends RubyObject {
|
| 46 | + private static final MethodHandle CONCAT_WITH_CODERANGE; |
| 47 | + |
| 48 | + static { |
| 49 | + // set up coderange-aware concat that works with the new catWithCodeRange as well as earlier JRuby without it. |
| 50 | + // TODO: remove and replace with direct call once 9.3 is fully unsupported |
| 51 | + MethodHandle catWithCR = null; |
| 52 | + MethodHandles.Lookup lookup = MethodHandles.publicLookup(); |
| 53 | + try { |
| 54 | + catWithCR = lookup.findVirtual(RubyString.class, "catWithCodeRange", MethodType.methodType(int.class, ByteList.class, int.class)); |
| 55 | + } catch (NoSuchMethodException | IllegalAccessException e) { |
| 56 | + try { |
| 57 | + catWithCR = lookup.findVirtual(RubyString.class, "cat19", MethodType.methodType(int.class, ByteList.class, int.class)); |
| 58 | + } catch (Exception t) { |
| 59 | + Helpers.throwException(t); |
| 60 | + } |
| 61 | + } |
| 62 | + CONCAT_WITH_CODERANGE = catWithCR; |
| 63 | + } |
42 | 64 |
|
43 | 65 | static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
44 | 66 | public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
@@ -144,7 +166,12 @@ public IRubyObject read(final ThreadContext context, final IRubyObject[] args) {
|
144 | 166 | if ( bytes != null ) {
|
145 | 167 | if ( buffer != null ) {
|
146 | 168 | buffer.clear();
|
147 |
| - buffer.catWithCodeRange(new ByteList(bytes, false), StringSupport.CR_UNKNOWN); |
| 169 | + try { |
| 170 | + int _ = (int) CONCAT_WITH_CODERANGE.invokeExact(new ByteList(bytes, false), StringSupport.CR_UNKNOWN); |
| 171 | + } catch (Throwable t) { |
| 172 | + Helpers.throwException(t); |
| 173 | + } |
| 174 | + |
148 | 175 | return buffer;
|
149 | 176 | }
|
150 | 177 | return context.runtime.newString(new ByteList(bytes, false));
|
|
0 commit comments