Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.jruby.java.addons;
 
 import org.jruby.Ruby;
public class IOJavaAddons {
    // FIXME This whole thing could probably be implemented as a module and
    // mixed into appropriate classes, especially if it uses either
    // IOInput/OutputStream or is smart about the kind of IO-like object
    // it's being used against.
    
    @JRubyMethod
    public static IRubyObject to_inputstream(ThreadContext contextIRubyObject self) {
        RubyIO io = (RubyIO)self;
        Ruby runtime = context.runtime;
        try {
            io.getOpenFile().checkReadable(context.runtime);
        } catch (IOException ex) {
            throw runtime.newIOErrorFromException(ex);
        } catch (BadDescriptorException ex) {
            throw runtime.newErrnoEBADFError();
        } catch (InvalidValueException e) {
            throw runtime.newErrnoEINVALError();
        }
        return JavaUtil.convertJavaToUsableRubyObject(context.runtimeio.getInStream());
    }
    
    @JRubyMethod
    public static IRubyObject to_outputstream(ThreadContext contextIRubyObject self) {
        RubyIO io = (RubyIO)self;
        Ruby runtime = context.runtime;
        try {
            io.getOpenFile().checkWritable(context.runtime);
        } catch (IOException ex) {
            throw runtime.newIOErrorFromException(ex);
        } catch (BadDescriptorException ex) {
            throw runtime.newErrnoEBADFError();
        } catch (InvalidValueException e) {
            throw runtime.newErrnoEINVALError();
        }
        return JavaUtil.convertJavaToUsableRubyObject(context.runtimeio.getOutStream());
    }
    @JRubyMethod
    public static IRubyObject to_channel(ThreadContext contextIRubyObject self) {
        RubyIO io = (RubyIO)self;
        return JavaUtil.convertJavaToUsableRubyObject(context.runtimeio.getChannel());
    }
    public static class AnyIO {
        @JRubyMethod(name = "to_inputstream")
        public static IRubyObject any_to_inputstream(ThreadContext contextIRubyObject self) {
            // using IOInputStream may not be the most performance way, but it's easy.
            return JavaUtil.convertJavaToUsableRubyObject(context.runtimenew IOInputStream(self));
        }
        @JRubyMethod(name = "to_outputstream")
        public static IRubyObject any_to_outputstream(ThreadContext contextIRubyObject self) {
            // using IOOutputStream may not be the most performance way, but it's easy.
            return JavaUtil.convertJavaToUsableRubyObject(context.runtimenew IOOutputStream(self));
        }
        @JRubyMethod(name = "to_channel")
        public static IRubyObject any_to_channel(ThreadContext contextIRubyObject self) {
            // using IOChannel may not be the most performant way, but it's easy.
            IOChannel channel;
            if (self.respondsTo("read")) {
                if (self.respondsTo("write") || self.respondsTo("<<")) {
                    channel = new IOChannel.IOReadableWritableByteChannel(self);
                } else {
                    channel = new IOChannel.IOReadableByteChannel(self);
                }
            } else {
                if (self.respondsTo("write") || self.respondsTo("<<")) {
                    channel = new IOChannel.IOWritableByteChannel(self);
                } else {
                    throw context.runtime.newTypeError(self.inspect().toString() + " does not respond to any of read, write, or <<");
                }
            }
            return JavaUtil.convertJavaToUsableRubyObject(context.runtimechannel);
        }
    }
New to GrepCode? Check out our FAQ X