1 31 package org.jruby; 32 33 import org.jruby.runtime.CallbackFactory; 34 import org.jruby.runtime.builtin.IRubyObject; 35 import org.jruby.util.JRubyFile; 36 37 public class RubyFileTest { 38 public static RubyModule createFileTestModule(Ruby runtime) { 39 RubyModule fileTestModule = runtime.defineModule("FileTest"); 40 CallbackFactory callbackFactory = runtime.callbackFactory(RubyFileTest.class); 41 42 fileTestModule.defineFastModuleFunction("file?", callbackFactory.getFastSingletonMethod("file_p", RubyKernel.IRUBY_OBJECT)); 43 fileTestModule.defineFastModuleFunction("directory?", callbackFactory.getFastSingletonMethod("directory_p", RubyKernel.IRUBY_OBJECT)); 44 fileTestModule.defineFastModuleFunction("exist?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT)); 45 fileTestModule.defineFastModuleFunction("exists?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT)); 46 fileTestModule.defineFastModuleFunction("readable?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT)); 47 fileTestModule.defineFastModuleFunction("readable_real?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT)); 48 fileTestModule.defineFastModuleFunction("size", callbackFactory.getFastSingletonMethod("size", RubyKernel.IRUBY_OBJECT)); 49 fileTestModule.defineFastModuleFunction("writable?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT)); 50 fileTestModule.defineFastModuleFunction("writable_real?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT)); 51 fileTestModule.defineFastModuleFunction("zero?", callbackFactory.getFastSingletonMethod("zero_p", RubyKernel.IRUBY_OBJECT)); 52 53 fileTestModule.defineFastMethod("file?", callbackFactory.getFastSingletonMethod("file_p", RubyKernel.IRUBY_OBJECT)); 54 fileTestModule.defineFastMethod("directory?", callbackFactory.getFastSingletonMethod("directory_p", RubyKernel.IRUBY_OBJECT)); 55 fileTestModule.defineFastMethod("exist?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT)); 56 fileTestModule.defineFastMethod("exists?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT)); 57 fileTestModule.defineFastMethod("readable?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT)); 58 fileTestModule.defineFastMethod("readable_real?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT)); 59 fileTestModule.defineFastMethod("size", callbackFactory.getFastSingletonMethod("size", RubyKernel.IRUBY_OBJECT)); 60 fileTestModule.defineFastMethod("writable?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT)); 61 fileTestModule.defineFastMethod("writable_real?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT)); 62 fileTestModule.defineFastMethod("zero?", callbackFactory.getFastSingletonMethod("zero_p", RubyKernel.IRUBY_OBJECT)); 63 64 return fileTestModule; 65 } 66 67 public static RubyBoolean directory_p(IRubyObject recv, IRubyObject filename) { 68 return recv.getRuntime().newBoolean(newFile(filename).isDirectory()); 69 } 70 71 public static IRubyObject exist_p(IRubyObject recv, IRubyObject filename) { 72 return recv.getRuntime().newBoolean(newFile(filename).exists()); 73 } 74 75 public static RubyBoolean readable_p(IRubyObject recv, IRubyObject filename) { 78 return filename.getRuntime().newBoolean(newFile(filename).canRead()); 79 } 80 81 public static IRubyObject size(IRubyObject recv, IRubyObject filename) { 82 JRubyFile file = newFile(filename); 83 84 if (!file.exists()) { 85 throw recv.getRuntime().newErrnoENOENTError("No such file: " + filename); 86 } 87 return filename.getRuntime().newFixnum(file.length()); 88 } 89 90 public static RubyBoolean writable_p(IRubyObject recv, IRubyObject filename) { 93 return filename.getRuntime().newBoolean(newFile(filename).canWrite()); 94 } 95 96 public static RubyBoolean zero_p(IRubyObject recv, IRubyObject filename) { 97 JRubyFile file = newFile(filename); 98 99 return filename.getRuntime().newBoolean(file.exists() && file.length() == 0L); 100 } 101 102 public static RubyBoolean file_p(IRubyObject recv, IRubyObject filename) { 103 JRubyFile file = newFile(filename); 104 105 return filename.getRuntime().newBoolean(file.isFile()); 106 } 107 108 private static JRubyFile newFile(IRubyObject path) { 109 return JRubyFile.create(path.getRuntime().getCurrentDirectory(), path.convertToString().toString()); 110 } 111 } 112 | Popular Tags |