1 package org.unicode.cldr.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.UncheckedIOException; 11 import java.net.URL; 12 import java.nio.charset.Charset; 13 14 import org.unicode.cldr.util.With.SimpleIterator; 15 16 public class FileReaders { 17 // 18 // public static SemiFileReader fillMapFromSemi(Class classLocation, String fileName, SemiFileReader handler) { 19 // return handler.process(classLocation, fileName); 20 // } openFile(Class<?> class1, String file)21 public static BufferedReader openFile(Class<?> class1, String file) { 22 return openFile(class1, file, CldrUtility.UTF8); 23 } 24 openFile(Class<?> class1, String file, Charset charset)25 public static BufferedReader openFile(Class<?> class1, String file, Charset charset) { 26 // URL path = null; 27 // String externalForm = null; 28 try { 29 // //System.out.println("Reading:\t" + file1.getCanonicalPath()); 30 // path = class1.getResource(file); 31 // externalForm = path.toExternalForm(); 32 // if (externalForm.startsWith("file:")) { 33 // externalForm = externalForm.substring(5); 34 // } 35 // File file1 = new File(externalForm); 36 // boolean x = file1.canRead(); 37 // final InputStream resourceAsStream = new FileInputStream(file1); 38 final InputStream resourceAsStream = class1.getResourceAsStream(file); 39 // String foo = class1.getResource(".").toString(); 40 if (charset == null) { 41 charset = CldrUtility.UTF8; 42 } 43 InputStreamReader reader = new InputStreamReader(resourceAsStream, charset); 44 BufferedReader bufferedReader = new BufferedReader(reader, 1024 * 64); 45 return bufferedReader; 46 } catch (Exception e) { 47 String className = class1 == null ? null : class1.getCanonicalName(); 48 String normalizedPath = null; 49 try { 50 String relativeFileName = FileReaders.getRelativeFileName(class1, "../util/"); 51 normalizedPath = PathUtilities.getNormalizedPathString(relativeFileName); 52 } catch (Exception e1) { 53 throw new IllegalArgumentException("Couldn't open file: " + file + "; relative to class: " 54 + className, e); 55 } 56 throw new IllegalArgumentException("Couldn't open file " + file + "; in path " + normalizedPath + "; relative to class: " 57 + className, e); 58 } 59 } 60 openFile(String directory, String file, Charset charset)61 public static BufferedReader openFile(String directory, String file, Charset charset) { 62 try { 63 return new BufferedReader(new InputStreamReader(new FileInputStream(new File(directory, file)), charset)); 64 } catch (FileNotFoundException e) { 65 throw new IllegalArgumentException(e); // handle dang'd checked exception 66 } 67 } 68 openFile(String directory, String file)69 public static BufferedReader openFile(String directory, String file) { 70 return openFile(directory, file, CldrUtility.UTF8); 71 } 72 getRelativeFileName(Class<?> class1, String filename)73 public static String getRelativeFileName(Class<?> class1, String filename) { 74 URL resource = class1.getResource(filename); 75 String resourceString = resource.toString(); 76 if (resourceString.startsWith("file:")) { 77 return resourceString.substring(5); 78 } else if (resourceString.startsWith("jar:file:")) { 79 return resourceString.substring(9); 80 } else { 81 throw new IllegalArgumentException("File not found: " + resourceString); 82 } 83 } 84 85 86 public static class ReadLineSimpleIterator implements SimpleIterator<String> { 87 final BufferedReader bufferedReader; 88 ReadLineSimpleIterator(BufferedReader bufferedReader)89 public ReadLineSimpleIterator(BufferedReader bufferedReader) { 90 super(); 91 this.bufferedReader = bufferedReader; 92 } 93 94 @Override next()95 public String next() { 96 try { 97 return bufferedReader.readLine(); 98 } catch (IOException e) { 99 throw new UncheckedIOException(e); 100 } 101 } 102 } 103 } 104