1 28 31 package org.jruby.util; 32 33 import java.io.IOException ; 34 import java.io.Reader ; 35 36 import org.jruby.RubyString; 37 import org.jruby.runtime.builtin.IRubyObject; 38 39 43 public class IOReader extends Reader { 44 private IRubyObject io; 45 46 public IOReader(final IRubyObject io) { 47 if(!io.respondsTo("read")) { 48 throw new IllegalArgumentException ("Object: " + io + " is not a legal argument to this wrapper, cause it doesn't respond to \"read\"."); 49 } 50 this.io = io; 51 } 52 53 public void close() throws IOException { 54 if(io.respondsTo("close")) { 55 io.callMethod(io.getRuntime().getCurrentContext(), "close"); 56 } 57 } 58 59 public int read(final char[] arr, final int off, final int len) { 60 final IRubyObject read = io.callMethod(io.getRuntime().getCurrentContext(),"read", io.getRuntime().newFixnum(len)); 61 if(read.isNil() || ((RubyString)read).getValue().length() == 0) { 62 return -1; 63 } else { 64 final RubyString str = (RubyString)read; 65 final CharSequence val = str.getValue(); 66 System.arraycopy(val.toString().toCharArray(),0,arr,off,val.length()); 67 return val.length(); 68 } 69 } 70 } 72 | Popular Tags |