Skip to content

Commit

Permalink
Improvements using reviews
Browse files Browse the repository at this point in the history
Signed-off-by: UpeksheJay <[email protected]>
  • Loading branch information
UpeksheJay committed Sep 13, 2016
1 parent 4754d51 commit df1a8fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions ext/nio4r/org/nio4r/ByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ByteBuffer(final Ruby ruby, RubyClass rubyClass) {
public IRubyObject initialize(ThreadContext context, IRubyObject value, IRubyObject offset, IRubyObject length) {
Ruby ruby = context.getRuntime();
if (value == ruby.getNil())
throw new IllegalArgumentException();
throw new context.runtime.newTypeError("not a valid input");

if (value instanceof RubyString) {
if (offset != ruby.getNil() && length != ruby.getNil()) {
Expand All @@ -48,7 +48,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject value, IRubyObj
int allocationSize = RubyNumeric.num2int(value);
byteBuffer = java.nio.ByteBuffer.allocate(allocationSize);
} else {
throw new IllegalArgumentException();
throw new context.runtime.newRuntimeError("Invalid Arguiments Exception");
}
return this;
}
Expand Down Expand Up @@ -87,15 +87,14 @@ public IRubyObject readNext(ThreadContext context, IRubyObject count) {
if (c < 1)
throw new IllegalArgumentException();
if (c <= byteBuffer.remaining()) {
ArrayList<Byte> temp = new ArrayList<Byte>();

org.jruby.util.ByteList temp = new ByteList(c);
while (c > 0) {
temp.add(byteBuffer.get());
temp.append(byteBuffer.get());
c = c - 1;
}
return JavaUtil.convertJavaToRuby(context.getRuntime(), new String(toPrimitives(temp)));
return context.runtime.newString(temp);
}
return JavaUtil.convertJavaToRuby(context.getRuntime(), ""); //Empty String
return RubyString.newEmptyString(context.runtime); //Empty String
}

private byte[] toPrimitives(ArrayList<Byte> oBytes) {
Expand Down Expand Up @@ -151,9 +150,8 @@ private boolean isTheSameFile(File f, boolean read) {

@JRubyMethod(name = "remaining")
public IRubyObject remainingPositions(ThreadContext context) {
Ruby ruby = context.getRuntime();
int count = byteBuffer.remaining();
return RubyNumeric.int2fix(ruby, count);
return context.getRuntime().newFixnum(count);
}

@JRubyMethod(name = "remaining?")
Expand All @@ -167,7 +165,7 @@ public IRubyObject hasRemaining(ThreadContext context) {
@JRubyMethod(name = "offset?")
public IRubyObject getOffset(ThreadContext context) {
int offset = byteBuffer.arrayOffset();
return JavaUtil.convertJavaToRuby(context.getRuntime(), offset);
return context.getRuntime().newFixnum(offset);
}

/**
Expand All @@ -179,9 +177,12 @@ public IRubyObject getOffset(ThreadContext context) {
*/
@JRubyMethod(name = "equals?")
public IRubyObject equals(ThreadContext context, IRubyObject ob) {
boolean equal = this.byteBuffer.equals(((ByteBuffer) JavaUtil.convertRubyToJava(ob)).getBuffer());
Object o = JavaUtil.convertRubyToJava(ob);
if(!(o instanceof ByteBuffer)) return context.getRuntime().getFalse();

boolean equal = this.byteBuffer.equals(((ByteBuffer) ).getBuffer());
if (equal)
context.getRuntime().getTrue();
return context.getRuntime().getTrue();
return context.getRuntime().getFalse();
}

Expand Down Expand Up @@ -249,7 +250,7 @@ public IRubyObject compact(ThreadContext context) {
@JRubyMethod(name = "capacity")
public IRubyObject capacity(ThreadContext context) {
int cap = byteBuffer.capacity();
return JavaUtil.convertJavaToRuby(context.getRuntime(), cap);
return context.getRuntime().newFixnum(cap);
}

@JRubyMethod
Expand All @@ -269,7 +270,7 @@ public IRubyObject limit(ThreadContext context, IRubyObject newLimit) {
@JRubyMethod(name = "limit?")
public IRubyObject limit(ThreadContext context) {
int lmt = byteBuffer.limit();
return JavaUtil.convertJavaToRuby(context.getRuntime(), lmt);
return context.getRuntime().newFixnum(lmt);
}

@JRubyMethod(name = "to_s")
Expand Down
4 changes: 2 additions & 2 deletions lib/nio/bytebuffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module NIO
class ByteBuffer
def initialize(value, offset = nil, length = nil)
# value can be either STRING or INTEGER
fail "Illegal Argument Exception" if value.nil?
fail "not a valid input" if value.nil?
@position = 0
@mark = -1
if value.is_a? Integer
Expand All @@ -18,7 +18,7 @@ def initialize(value, offset = nil, length = nil)
@offset = offset
@position = offset
unless length.nil?
fail "Illegal Argument Exception" if offset + length >= value
fail "Invalid Arguiments Exception" if offset + length >= value
@limit = offset + length
end
end
Expand Down

0 comments on commit df1a8fc

Please sign in to comment.