pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/a7fe39cb8e635cd58f0e994d54ef30f8ac1d3d07

/> [ji] adjust Java nio Buffer's inspect · jruby/jruby@a7fe39c · GitHub
Skip to content

Commit a7fe39c

Browse files
committed
[ji] adjust Java nio Buffer's inspect
mostly to have consistent output with all Buffer sub-types since CharBuffer implements CharSequence -> prints content (we do not want that for a buffer) also added Buffer#length/size for convenience
1 parent 81b34d1 commit a7fe39c

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

core/src/main/java/org/jruby/javasupport/Java.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public void load(Ruby runtime, boolean wrap) {
116116
org.jruby.javasupport.ext.JavaUtil.define(runtime);
117117
org.jruby.javasupport.ext.JavaUtilRegex.define(runtime);
118118
org.jruby.javasupport.ext.JavaIo.define(runtime);
119+
org.jruby.javasupport.ext.JavaNio.define(runtime);
119120
org.jruby.javasupport.ext.JavaNet.define(runtime);
120121
org.jruby.javasupport.ext.JavaMath.define(runtime);
121122
org.jruby.javasupport.ext.JavaTime.define(runtime);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

spec/java_integration/types/inspect_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@
3030
expect(str.inspect).to eq '#<Java::JavaLang::StringBuilder: "bar">'
3131
end
3232
end
33+
34+
describe "java.nio.CharSequence" do # implements CharSequence
35+
it "inspects as other Buffer impls" do
36+
buf = java.nio.CharBuffer.allocate(12)
37+
buf.append('a'); buf.put('b')
38+
expect(buf.inspect).to match /#<Java::JavaNio::HeapCharBuffer:.*? position=2, limit=12, capacity=12, readOnly=false>/
39+
end
40+
end
41+
42+
describe "java.nio.ByteBuffer" do
43+
it "inspects as other Buffer impls" do
44+
buf = java.nio.ByteBuffer.allocateDirect(8)
45+
buf.put(1)
46+
expect(buf.inspect).to match /#<Java::JavaNio::DirectByteBuffer:.*? position=1, limit=8, capacity=8, readOnly=false>/
47+
end
48+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy