IOJavaAddons.java

  1. package org.jruby.java.addons;

  2. import java.io.IOException;
  3. import org.jruby.Ruby;
  4. import org.jruby.RubyIO;
  5. import org.jruby.anno.JRubyMethod;
  6. import org.jruby.javasupport.JavaUtil;
  7. import org.jruby.runtime.ThreadContext;
  8. import org.jruby.runtime.builtin.IRubyObject;
  9. import org.jruby.util.IOChannel;
  10. import org.jruby.util.IOInputStream;
  11. import org.jruby.util.IOOutputStream;
  12. import org.jruby.util.io.BadDescriptorException;
  13. import org.jruby.util.io.InvalidValueException;

  14. public class IOJavaAddons {
  15.     // FIXME This whole thing could probably be implemented as a module and
  16.     // mixed into appropriate classes, especially if it uses either
  17.     // IOInput/OutputStream or is smart about the kind of IO-like object
  18.     // it's being used against.
  19.    
  20.     @JRubyMethod
  21.     public static IRubyObject to_inputstream(ThreadContext context, IRubyObject self) {
  22.         RubyIO io = (RubyIO)self;

  23.         io.getOpenFile().checkReadable(context);

  24.         return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getInStream());
  25.     }
  26.    
  27.     @JRubyMethod
  28.     public static IRubyObject to_outputstream(ThreadContext context, IRubyObject self) {
  29.         RubyIO io = (RubyIO)self;

  30.         io.getOpenFile().checkWritable(context);

  31.         return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getOutStream());
  32.     }

  33.     @JRubyMethod
  34.     public static IRubyObject to_channel(ThreadContext context, IRubyObject self) {
  35.         RubyIO io = (RubyIO)self;

  36.         return JavaUtil.convertJavaToUsableRubyObject(context.runtime, io.getChannel());
  37.     }

  38.     public static class AnyIO {
  39.         @JRubyMethod(name = "to_inputstream")
  40.         public static IRubyObject any_to_inputstream(ThreadContext context, IRubyObject self) {
  41.             // using IOInputStream may not be the most performance way, but it's easy.
  42.             return JavaUtil.convertJavaToUsableRubyObject(context.runtime, new IOInputStream(self));
  43.         }

  44.         @JRubyMethod(name = "to_outputstream")
  45.         public static IRubyObject any_to_outputstream(ThreadContext context, IRubyObject self) {
  46.             // using IOOutputStream may not be the most performance way, but it's easy.
  47.             return JavaUtil.convertJavaToUsableRubyObject(context.runtime, new IOOutputStream(self));
  48.         }

  49.         @JRubyMethod(name = "to_channel")
  50.         public static IRubyObject any_to_channel(ThreadContext context, IRubyObject self) {
  51.             // using IOChannel may not be the most performant way, but it's easy.
  52.             IOChannel channel;
  53.             if (self.respondsTo("read")) {
  54.                 if (self.respondsTo("write") || self.respondsTo("<<")) {
  55.                     channel = new IOChannel.IOReadableWritableByteChannel(self);
  56.                 } else {
  57.                     channel = new IOChannel.IOReadableByteChannel(self);
  58.                 }
  59.             } else {
  60.                 if (self.respondsTo("write") || self.respondsTo("<<")) {
  61.                     channel = new IOChannel.IOWritableByteChannel(self);
  62.                 } else {
  63.                     throw context.runtime.newTypeError(self.inspect().toString() + " does not respond to any of read, write, or <<");
  64.                 }
  65.             }
  66.             return JavaUtil.convertJavaToUsableRubyObject(context.runtime, channel);
  67.         }
  68.     }
  69. }