• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.InputStream;
8 import java.io.InputStreamReader;
9 import java.net.URL;
10 import java.nio.charset.Charset;
11 
12 public class FileReaders {
13     //
14     // public static SemiFileReader fillMapFromSemi(Class classLocation, String fileName, SemiFileReader handler) {
15     // return handler.process(classLocation, fileName);
16     // }
openFile(Class<?> class1, String file)17     public static BufferedReader openFile(Class<?> class1, String file) {
18         return openFile(class1, file, CldrUtility.UTF8);
19     }
20 
openFile(Class<?> class1, String file, Charset charset)21     public static BufferedReader openFile(Class<?> class1, String file, Charset charset) {
22         // URL path = null;
23         // String externalForm = null;
24         try {
25             // //System.out.println("Reading:\t" + file1.getCanonicalPath());
26             // path = class1.getResource(file);
27             // externalForm = path.toExternalForm();
28             // if (externalForm.startsWith("file:")) {
29             // externalForm = externalForm.substring(5);
30             // }
31             // File file1 = new File(externalForm);
32             // boolean x = file1.canRead();
33             // final InputStream resourceAsStream = new FileInputStream(file1);
34             final InputStream resourceAsStream = class1.getResourceAsStream(file);
35             // String foo = class1.getResource(".").toString();
36             if (charset == null) {
37                 charset = CldrUtility.UTF8;
38             }
39             InputStreamReader reader = new InputStreamReader(resourceAsStream, charset);
40             BufferedReader bufferedReader = new BufferedReader(reader, 1024 * 64);
41             return bufferedReader;
42         } catch (Exception e) {
43             String className = class1 == null ? null : class1.getCanonicalName();
44             String canonicalName = null;
45             try {
46                 String relativeFileName = FileReaders.getRelativeFileName(class1, "../util/");
47                 canonicalName = new File(relativeFileName).getCanonicalPath();
48             } catch (Exception e1) {
49                 throw new IllegalArgumentException("Couldn't open file: " + file + "; relative to class: "
50                     + className, e);
51             }
52             throw new IllegalArgumentException("Couldn't open file " + file + "; in path " + canonicalName + "; relative to class: "
53                 + className, e);
54         }
55     }
56 
openFile(String directory, String file, Charset charset)57     public static BufferedReader openFile(String directory, String file, Charset charset) {
58         try {
59             return new BufferedReader(new InputStreamReader(new FileInputStream(new File(directory, file)), charset));
60         } catch (FileNotFoundException e) {
61             throw new IllegalArgumentException(e); // handle dang'd checked exception
62         }
63     }
64 
openFile(String directory, String file)65     public static BufferedReader openFile(String directory, String file) {
66         return openFile(directory, file, CldrUtility.UTF8);
67     }
68 
getRelativeFileName(Class<?> class1, String filename)69     public static String getRelativeFileName(Class<?> class1, String filename) {
70         URL resource = class1.getResource(filename);
71         String resourceString = resource.toString();
72         if (resourceString.startsWith("file:")) {
73             return resourceString.substring(5);
74         } else if (resourceString.startsWith("jar:file:")) {
75             return resourceString.substring(9);
76         } else {
77             throw new IllegalArgumentException("File not found: " + resourceString);
78         }
79     }
80 }