32
32
33
33
package org .jruby ;
34
34
35
- import static org .jruby .RubyEnumerator .enumeratorize ;
36
-
35
+ import java .io .Closeable ;
37
36
import java .io .File ;
38
37
import java .io .FileInputStream ;
39
38
import java .io .IOException ;
39
+ import java .nio .file .FileSystems ;
40
+ import java .nio .file .Path ;
41
+ import java .nio .file .Watchable ;
40
42
import java .util .ArrayList ;
41
43
import java .util .Arrays ;
42
44
import java .util .Collections ;
43
45
import java .util .List ;
44
46
import java .util .regex .Pattern ;
45
47
46
48
import jnr .posix .FileStat ;
47
-
48
- import org .jruby .anno .JRubyMethod ;
49
- import org .jruby .anno .JRubyClass ;
50
-
51
49
import jnr .posix .util .Platform ;
52
50
53
51
import org .jcodings .Encoding ;
54
- import org .jcodings .specific .UTF8Encoding ;
52
+ import org .jruby .anno .JRubyMethod ;
53
+ import org .jruby .anno .JRubyClass ;
55
54
import org .jruby .exceptions .RaiseException ;
56
55
import org .jruby .javasupport .JavaUtil ;
57
56
import org .jruby .runtime .Block ;
62
61
import org .jruby .util .*;
63
62
import org .jruby .ast .util .ArgsUtil ;
64
63
64
+ import static org .jruby .RubyEnumerator .enumeratorize ;
65
+ import static org .jruby .RubyString .UTF8 ;
66
+
65
67
/**
66
- * . The Ruby built-in class Dir.
68
+ * The Ruby built-in class Dir.
67
69
*
68
70
* @author jvoegele
69
71
*/
70
72
@ JRubyClass (name = "Dir" , include = "Enumerable" )
71
- public class RubyDir extends RubyObject {
73
+ public class RubyDir extends RubyObject implements Closeable {
72
74
private RubyString path ; // What we passed to the constructor for method 'path'
73
75
protected FileResource dir ;
74
76
private long lastModified = Long .MIN_VALUE ;
75
77
private String [] snapshot ; // snapshot of contents of directory
76
78
private int pos ; // current position in directory
77
79
private boolean isOpen = true ;
78
80
79
- private final static Encoding UTF8 = UTF8Encoding .INSTANCE ;
80
-
81
81
private static final Pattern PROTOCOL_PATTERN = Pattern .compile ("^(uri|jar|file|classpath):([^:]*:)?//?.*" );
82
82
83
83
public RubyDir (Ruby runtime , RubyClass type ) {
@@ -650,13 +650,15 @@ public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyO
650
650
* Closes the directory stream.
651
651
*/
652
652
@ JRubyMethod (name = "close" )
653
- public IRubyObject close () {
653
+ public IRubyObject close (ThreadContext context ) {
654
+ close ();
655
+ return context .nil ;
656
+ }
657
+
658
+ public final void close () {
654
659
// Make sure any read()s after close fail.
655
660
checkDirIgnoreClosed ();
656
-
657
661
isOpen = false ;
658
-
659
- return getRuntime ().getNil ();
660
662
}
661
663
662
664
/**
@@ -769,6 +771,11 @@ public IRubyObject to_path(ThreadContext context) {
769
771
return path (context );
770
772
}
771
773
774
+ public String getPath () {
775
+ if (path == null ) return null ;
776
+ return path .asJavaString ();
777
+ }
778
+
772
779
/** Returns the next entry from this directory. */
773
780
@ JRubyMethod (name = "read" )
774
781
public IRubyObject read () {
@@ -992,17 +999,18 @@ public static RubyString getHomeDirectoryPath(ThreadContext context) {
992
999
return getHomeDirectoryPath (context , context .runtime .getENV ().op_aref (context , homeKey ));
993
1000
}
994
1001
1002
+ private static final ByteList user_home = new ByteList (new byte [] {'u' ,'s' ,'e' ,'r' ,'.' ,'h' ,'o' ,'m' ,'e' }, false );
1003
+
995
1004
static RubyString getHomeDirectoryPath (ThreadContext context , IRubyObject home ) {
996
1005
final Ruby runtime = context .runtime ;
997
1006
998
1007
if (home == null || home == context .nil ) {
999
1008
IRubyObject ENV_JAVA = runtime .getObject ().getConstant ("ENV_JAVA" );
1000
- home = ENV_JAVA .callMethod (context , "[]" , runtime .newString ("user.home" ));
1009
+ home = ENV_JAVA .callMethod (context , "[]" , RubyString .newString (runtime , user_home , UTF8 ));
1001
1010
}
1002
1011
1003
1012
if (home == null || home == context .nil ) {
1004
- RubyHash ENV = (RubyHash ) runtime .getObject ().getConstant ("ENV" );
1005
- home = ENV .op_aref (context , runtime .newString ("LOGDIR" ));
1013
+ home = context .runtime .getENV ().op_aref (context , runtime .newString ("LOGDIR" ));
1006
1014
}
1007
1015
1008
1016
if (home == null || home == context .nil ) {
@@ -1012,6 +1020,19 @@ static RubyString getHomeDirectoryPath(ThreadContext context, IRubyObject home)
1012
1020
return (RubyString ) home ;
1013
1021
}
1014
1022
1023
+ @ Override
1024
+ public <T > T toJava (Class <T > target ) {
1025
+ if (target == File .class ) {
1026
+ final String path = getPath ();
1027
+ return path == null ? null : (T ) new File (path );
1028
+ }
1029
+ if (target == Path .class || target == Watchable .class ) {
1030
+ final String path = getPath ();
1031
+ return path == null ? null : (T ) FileSystems .getDefault ().getPath (path );
1032
+ }
1033
+ return super .toJava (target );
1034
+ }
1035
+
1015
1036
@ Deprecated
1016
1037
public static RubyArray entries19 (ThreadContext context , IRubyObject recv , IRubyObject arg ) {
1017
1038
return entries (context , recv , arg );
0 commit comments