@@ -115,8 +115,7 @@ private final void checkDirIgnoreClosed() {
115
115
// update snapshot (if changed) :
116
116
if (snapshot == null || dir .exists () && dir .lastModified () > lastModified ) {
117
117
lastModified = dir .lastModified ();
118
- final List <String > contents = getContents (dir );
119
- snapshot = contents .toArray (new String [contents .size ()]);
118
+ snapshot = list (dir );
120
119
}
121
120
}
122
121
@@ -294,10 +293,12 @@ public RubyArray entries() {
294
293
return RubyArray .newArrayMayCopy (getRuntime (), JavaUtil .convertJavaArrayToRuby (getRuntime (), snapshot ));
295
294
}
296
295
296
+ @ Deprecated
297
297
public static RubyArray entries (IRubyObject recv , IRubyObject path ) {
298
298
return entries (recv .getRuntime ().getCurrentContext (), recv , path );
299
299
}
300
300
301
+ @ Deprecated
301
302
public static RubyArray entries (IRubyObject recv , IRubyObject path , IRubyObject arg , IRubyObject opts ) {
302
303
return entries (recv .getRuntime ().getCurrentContext (), recv , path , opts );
303
304
}
@@ -317,7 +318,7 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb
317
318
@ JRubyMethod (name = "entries" , meta = true )
318
319
public static RubyArray entries (ThreadContext context , IRubyObject recv , IRubyObject arg , IRubyObject opts ) {
319
320
Ruby runtime = context .runtime ;
320
- Encoding encoding = runtime . getDefaultInternalEncoding () ;
321
+ Encoding encoding = null ;
321
322
322
323
RubyString path = StringSupport .checkEmbeddedNulls (runtime , RubyFile .get_path (context , arg ));
323
324
@@ -327,6 +328,8 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb
327
328
encoding = runtime .getEncodingService ().getEncodingFromObject (encodingArg );
328
329
}
329
330
}
331
+ if (encoding == null ) encoding = runtime .getDefaultEncoding ();
332
+
330
333
return entriesCommon (context , path .asJavaString (), encoding , false );
331
334
}
332
335
@@ -526,31 +529,33 @@ public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRu
526
529
}
527
530
528
531
private static IRubyObject eachChildCommon (ThreadContext context , IRubyObject recv , RubyString path , RubyEncoding encoding , Block block ) {
532
+ final Ruby runtime = context .runtime ;
529
533
if (block .isGiven ()) {
530
- RubyDir dir = (RubyDir ) context . runtime .getDir ().newInstance (context , path , Block .NULL_BLOCK );
534
+ RubyDir dir = (RubyDir ) runtime .getDir ().newInstance (context , path , Block .NULL_BLOCK );
531
535
532
- dir .each_child (context , block );
536
+ dir .each_child (context , encoding == null ? runtime . getDefaultEncoding () : encoding . getEncoding (), block );
533
537
return context .nil ;
534
538
}
535
539
536
540
if (encoding == null ) {
537
- return enumeratorize (context . runtime , recv , "each_child" , path );
541
+ return enumeratorize (runtime , recv , "each_child" , path );
538
542
}
539
- return enumeratorize (context . runtime , recv , "each_child" , path , encoding );
543
+ return enumeratorize (runtime , recv , "each_child" , path , encoding );
540
544
}
541
545
542
546
private static IRubyObject foreachCommon (ThreadContext context , IRubyObject recv , RubyString path , RubyEncoding encoding , Block block ) {
547
+ final Ruby runtime = context .runtime ;
543
548
if (block .isGiven ()) {
544
- RubyDir dir = (RubyDir ) context . runtime .getDir ().newInstance (context , path , Block .NULL_BLOCK );
549
+ RubyDir dir = (RubyDir ) runtime .getDir ().newInstance (context , path , Block .NULL_BLOCK );
545
550
546
- dir .each (context , block );
551
+ dir .each (context , encoding == null ? runtime . getDefaultEncoding () : encoding . getEncoding (), block );
547
552
return context .nil ;
548
553
}
549
554
550
555
if (encoding == null ) {
551
- return enumeratorize (context . runtime , recv , "foreach" , path );
556
+ return enumeratorize (runtime , recv , "foreach" , path );
552
557
}
553
- return enumeratorize (context . runtime , recv , "foreach" , path , encoding );
558
+ return enumeratorize (runtime , recv , "foreach" , path , encoding );
554
559
}
555
560
556
561
/** Returns the current directory. */
@@ -682,6 +687,31 @@ public IRubyObject each(ThreadContext context, Encoding enc, Block block) {
682
687
return this ;
683
688
}
684
689
690
+ /**
691
+ * Executes the block once for each entry in the directory.
692
+ */
693
+ @ JRubyMethod (name = "each" )
694
+ public IRubyObject each (ThreadContext context , Block block ) {
695
+ return block .isGiven () ? each (context , context .runtime .getDefaultEncoding (), block ) : enumeratorize (context .runtime , this , "each" );
696
+ }
697
+
698
+ @ JRubyMethod (name = "each" )
699
+ public IRubyObject each (ThreadContext context , IRubyObject encoding , Block block ) {
700
+ if (!(encoding instanceof RubyEncoding )) throw context .runtime .newTypeError (encoding , context .runtime .getEncoding ());
701
+
702
+ return block .isGiven () ? each (context , ((RubyEncoding ) encoding ).getEncoding (), block ) : enumeratorize (context .runtime , this , "each" , encoding );
703
+ }
704
+
705
+ @ Deprecated
706
+ public IRubyObject each19 (ThreadContext context , Block block ) {
707
+ return each (context , block );
708
+ }
709
+
710
+ @ Deprecated
711
+ public IRubyObject each19 (ThreadContext context , IRubyObject encoding , Block block ) {
712
+ return each (context , encoding , block );
713
+ }
714
+
685
715
/**
686
716
* Executes the block once for each child in the directory
687
717
* (i.e. all the directory entries except for "." and "..").
@@ -691,39 +721,20 @@ public IRubyObject each_child(ThreadContext context, Encoding enc, Block block)
691
721
692
722
String [] contents = snapshot ;
693
723
for (pos = 0 ; pos < contents .length ; pos ++) {
694
- if (!( contents [ pos ]. equals ( "." ) || contents [pos ]. equals ( ".." ))) {
695
- block . yield ( context , RubyString . newString ( context . runtime , contents [pos ], enc ));
696
- }
724
+ if (StringSupport . contentEquals ( contents [pos ], '.' )) continue ; /* current dir */
725
+ if ( StringSupport . contentEquals ( contents [pos ], '.' , '.' )) continue ; /* parent dir */
726
+ block . yield ( context , RubyString . newString ( context . runtime , contents [ pos ], enc ));
697
727
}
698
728
699
729
return this ;
700
730
}
701
731
702
- /**
703
- * Executes the block once for each entry in the directory.
704
- */
705
- public IRubyObject each (ThreadContext context , Block block ) {
706
- return each (context , context .runtime .getDefaultInternalEncoding (), block );
707
- }
708
-
709
732
/**
710
733
* Executes the block once for each child in the directory
711
734
* (i.e. all the directory entries except for "." and "..").
712
735
*/
713
736
public IRubyObject each_child (ThreadContext context , Block block ) {
714
- return each_child (context , context .runtime .getDefaultInternalEncoding (), block );
715
- }
716
-
717
- @ JRubyMethod (name = "each" )
718
- public IRubyObject each19 (ThreadContext context , Block block ) {
719
- return block .isGiven () ? each (context , block ) : enumeratorize (context .runtime , this , "each" );
720
- }
721
-
722
- @ JRubyMethod (name = "each" )
723
- public IRubyObject each19 (ThreadContext context , IRubyObject encoding , Block block ) {
724
- if (!(encoding instanceof RubyEncoding )) throw context .runtime .newTypeError (encoding , context .runtime .getEncoding ());
725
-
726
- return block .isGiven () ? each (context , ((RubyEncoding )encoding ).getEncoding (), block ) : enumeratorize (context .runtime , this , "each" , encoding );
737
+ return each_child (context , context .runtime .getDefaultEncoding (), block );
727
738
}
728
739
729
740
@ Override
@@ -788,6 +799,7 @@ public String getPath() {
788
799
public IRubyObject read () {
789
800
checkDir ();
790
801
802
+ final String [] snapshot = this .snapshot ;
791
803
if (pos >= snapshot .length ) return getRuntime ().getNil ();
792
804
793
805
RubyString result = getRuntime ().newString (snapshot [pos ]);
@@ -926,9 +938,15 @@ private static String dirFromPath(final String path, final Ruby runtime) throws
926
938
return dir ;
927
939
}
928
940
941
+ private static String [] list (FileResource directory ) {
942
+ final String [] contents = directory .list ();
943
+ // If an IO exception occurs (something odd, but possible)
944
+ // A directory may return null.
945
+ return contents == null ? NO_FILES : contents ;
946
+ }
947
+
929
948
/**
930
- * Returns the contents of the specified <code>directory</code> as an
931
- * <code>ArrayList</code> containing the names of the files as Java Strings.
949
+ * @deprecated no longer used
932
950
*/
933
951
protected static List <String > getContents (FileResource directory ) {
934
952
final String [] contents = directory .list ();
@@ -946,8 +964,7 @@ protected static List<String> getContents(FileResource directory) {
946
964
}
947
965
948
966
/**
949
- * Returns the contents of the specified <code>directory</code> as an
950
- * <code>ArrayList</code> containing the names of the files as Ruby Strings.
967
+ * @deprecated no longer used
951
968
*/
952
969
protected static List <RubyString > getContents (FileResource directory , Ruby runtime ) {
953
970
final String [] contents = directory .list ();
0 commit comments