• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.nio.charset.Charset;
6 
7 import org.unicode.cldr.util.With.SimpleIterator;
8 
9 import com.ibm.icu.util.ICUUncheckedIOException;
10 
11 /**
12  * Class to get an Iterable for Strings from a File, returning one line at a time.
13  * @author ribnitz
14  *
15  */
16 public class StringIterables {
17     /**
18      * Simple API to iterate over file lines. Example:
19      * for (String s : FileUtilities.in(directory,name)) {
20      * ...
21      * }
22      *
23      * @author markdavis
24      *
25      */
in(Class<?> class1, String file)26     public static Iterable<String> in(Class<?> class1, String file) {
27         return With.in(new FileLines(FileReaders.openFile(class1, file, CldrUtility.UTF8)));
28     }
29 
30     /**
31      * Simple API to iterate over file lines. Example:
32      * for (String s : FileUtilities.in(directory,name)) {
33      * ...
34      * }
35      *
36      * @author markdavis
37      *
38      */
in(Class<?> class1, String file, Charset charset)39     public static Iterable<String> in(Class<?> class1, String file, Charset charset) {
40         return With.in(new FileLines(FileReaders.openFile(class1, file, charset)));
41     }
42 
43     /**
44      * Simple API to iterate over file lines. Example:
45      * for (String s : FileUtilities.in(directory,name)) {
46      * ...
47      * }
48      *
49      * @author markdavis
50      *
51      */
in(String directory, String file)52     public static Iterable<String> in(String directory, String file) {
53         return With.in(new FileLines(FileReaders.openFile(directory, file, CldrUtility.UTF8)));
54     }
55 
56     /**
57      * Simple API to iterate over file lines. Example:
58      * for (String s : FileUtilities.in(directory,name)) {
59      * ...
60      * }
61      *
62      * @author markdavis
63      *
64      */
in(BufferedReader reader)65     public static Iterable<String> in(BufferedReader reader) {
66         return With.in(new FileLines(reader));
67     }
68 
69     /**
70      * Simple API to iterate over file lines. Example:
71      * for (String s : FileUtilities.in(directory,name)) {
72      * ...
73      * }
74      *
75      * @author markdavis
76      *
77      */
in(String directory, String file, Charset charset)78     public static Iterable<String> in(String directory, String file, Charset charset) {
79         return With.in(new FileLines(FileReaders.openFile(directory, file, charset)));
80     }
81 
82     private static class FileLines implements SimpleIterator<String> {
83         private BufferedReader input;
84 
FileLines(BufferedReader input)85         public FileLines(BufferedReader input) {
86             this.input = input;
87         }
88 
89         @Override
next()90         public String next() {
91             try {
92                 String result = input.readLine();
93                 if (result == null) {
94                     input.close();
95                 }
96                 return result;
97             } catch (IOException e) {
98                 throw new ICUUncheckedIOException(e); // handle dang'd checked exception
99             }
100         }
101 
102     }
103 }