|
| 1 | +/***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Version: EPL 2.0/GPL 2.0/LGPL 2.1 |
| 3 | + * |
| 4 | + * The contents of this file are subject to the Eclipse Public |
| 5 | + * License Version 2.0 (the "License"); you may not use this file |
| 6 | + * except in compliance with the License. You may obtain a copy of |
| 7 | + * the License at http://www.eclipse.org/legal/epl-v20.html |
| 8 | + * |
| 9 | + * Software distributed under the License is distributed on an "AS |
| 10 | + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 11 | + * implied. See the License for the specific language governing |
| 12 | + * rights and limitations under the License. |
| 13 | + * |
| 14 | + * Copyright (C) 2020 The JRuby Team |
| 15 | + * |
| 16 | + * Alternatively, the contents of this file may be used under the terms of |
| 17 | + * either of the GNU General Public License Version 2 or later (the "GPL"), |
| 18 | + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 19 | + * in which case the provisions of the GPL or the LGPL are applicable instead |
| 20 | + * of those above. If you wish to allow use of your version of this file only |
| 21 | + * under the terms of either the GPL or the LGPL, and not to allow others to |
| 22 | + * use your version of this file under the terms of the EPL, indicate your |
| 23 | + * decision by deleting the provisions above and replace them with the notice |
| 24 | + * and other provisions required by the GPL or the LGPL. If you do not delete |
| 25 | + * the provisions above, a recipient may use your version of this file under |
| 26 | + * the terms of any one of the EPL, the GPL or the LGPL. |
| 27 | + ***** END LICENSE BLOCK *****/ |
| 28 | + |
| 29 | +package org.jruby.javasupport.ext; |
| 30 | + |
| 31 | +import org.jruby.Ruby; |
| 32 | +import org.jruby.RubyClass; |
| 33 | +import org.jruby.RubyModule; |
| 34 | +import org.jruby.RubyString; |
| 35 | +import org.jruby.anno.JRubyMethod; |
| 36 | +import org.jruby.anno.JRubyModule; |
| 37 | +import org.jruby.internal.runtime.methods.JavaMethod; |
| 38 | +import org.jruby.runtime.ThreadContext; |
| 39 | +import org.jruby.runtime.builtin.IRubyObject; |
| 40 | +import org.jruby.util.RubyStringBuilder; |
| 41 | + |
| 42 | +import static org.jruby.javasupport.JavaUtil.unwrapIfJavaObject; |
| 43 | +import static org.jruby.runtime.Visibility.PUBLIC; |
| 44 | +import static org.jruby.util.Inspector.GT; |
| 45 | +import static org.jruby.util.Inspector.SPACE; |
| 46 | +import static org.jruby.util.Inspector.inspectPrefix; |
| 47 | + |
| 48 | +/** |
| 49 | + * <code>java.nio</code> package additions. |
| 50 | + * |
| 51 | + * @author kares |
| 52 | + */ |
| 53 | +public abstract class JavaNio { |
| 54 | + |
| 55 | + public static void define(final Ruby runtime) { |
| 56 | + JavaExtensions.put(runtime, java.nio.Buffer.class, (proxy) -> Buffer.define(runtime, (RubyClass) proxy)); |
| 57 | + // make sure it does not use CharSequence#inspect but rather a unified Buffer#inspect format: |
| 58 | + JavaExtensions.put(runtime, java.nio.CharBuffer.class, (proxy) -> proxy.addMethod("inspect", new InspectBuffer(proxy))); |
| 59 | + } |
| 60 | + |
| 61 | + @JRubyModule(name = "Java::JavaNio::Buffer") |
| 62 | + public static class Buffer { |
| 63 | + |
| 64 | + static RubyModule define(final Ruby runtime, final RubyClass proxy) { |
| 65 | + proxy.defineAnnotatedMethods(Buffer.class); |
| 66 | + proxy.addMethod("inspect", new InspectBuffer(proxy)); |
| 67 | + return proxy; |
| 68 | + } |
| 69 | + |
| 70 | + @JRubyMethod(name = {"length", "size"}) |
| 71 | + public static IRubyObject length(final ThreadContext context, final IRubyObject self) { |
| 72 | + java.nio.Buffer obj = self.toJava(java.nio.Buffer.class); |
| 73 | + return context.runtime.newFixnum(obj.remaining()); // limit - position |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + private static final class InspectBuffer extends JavaMethod.JavaMethodZero { |
| 79 | + |
| 80 | + InspectBuffer(RubyModule implClass) { |
| 81 | + super(implClass, PUBLIC, "inspect"); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public IRubyObject call(final ThreadContext context, final IRubyObject self, final RubyModule clazz, final java.lang.String name) { |
| 86 | + java.nio.Buffer obj = unwrapIfJavaObject(self); |
| 87 | + |
| 88 | + RubyString buf = inspectPrefix(context, self.getMetaClass(), System.identityHashCode(obj)); |
| 89 | + RubyStringBuilder.cat(context.runtime, buf, SPACE); |
| 90 | + buf.catString("position=" + obj.position() + ", limit=" + obj.limit() + ", capacity=" + obj.capacity() + ", readOnly=" + obj.isReadOnly()); |
| 91 | + RubyStringBuilder.cat(context.runtime, buf, GT); // > |
| 92 | + return buf; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments