3
3
import java .io .File ;
4
4
import java .io .FileNotFoundException ;
5
5
import java .io .IOException ;
6
- import java .io .InputStream ;
7
6
import java .nio .charset .StandardCharsets ;
8
7
import java .nio .file .Files ;
9
8
import java .nio .file .Path ;
10
- import java .nio .file .Paths ;
11
9
12
10
import convex .core .data .ACell ;
13
11
import convex .core .data .Blob ;
20
18
public class FileUtils {
21
19
22
20
/**
23
- * Loads a file as a String. Handles `-` for STDIN
21
+ * Loads a UTF-8 file as a String. Handles `-` for STDIN, and leading `~` for user home directory
24
22
* @param fileName File to load
25
23
* @return String contents of file
26
24
* @throws IOException in case of IO failure
@@ -32,32 +30,15 @@ public static String loadFileAsString(String fileName) throws IOException {
32
30
byte [] bs = System .in .readAllBytes ();
33
31
result = new String (bs );
34
32
} else {
35
- Path path = Paths . get (fileName );
36
- if (!path . toFile (). exists ()) {
33
+ Path path = getPath (fileName );
34
+ if (!Files . exists (path )) {
37
35
throw new FileNotFoundException ("File does not exist: " + path );
38
36
}
39
37
result = Files .readString (path , StandardCharsets .UTF_8 );
40
38
}
41
39
return result ;
42
40
}
43
41
44
- /**
45
- * Loads a String from an input stream assumed to be UTF-8
46
- * @param inputStream Stream to load
47
- * @return String contents of stream
48
- * @throws IOException in case of IO failure
49
- */
50
- public static String loadFileAsString (InputStream inputStream ) throws IOException {
51
- int bufferSize = 1024 ;
52
- char [] buffer = new char [bufferSize ];
53
- StringBuilder out = new StringBuilder ();
54
- java .io .Reader rdr = new java .io .InputStreamReader (inputStream , StandardCharsets .UTF_8 );
55
- for (int numRead ; (numRead = rdr .read (buffer , 0 , buffer .length )) > 0 ; ) {
56
- out .append (buffer , 0 , numRead );
57
- }
58
- return out .toString ();
59
- }
60
-
61
42
public static Blob loadFileAsBlob (Path file ) throws IOException {
62
43
return Blob .wrap (Files .readAllBytes (file ));
63
44
}
@@ -104,10 +85,10 @@ public static File ensureFilePath(File file) throws IOException {
104
85
}
105
86
106
87
/**
107
- * Create a path if necessary to a File object. Interprets leading "~" as user home directory.
88
+ * Create a path of directories as necessary to hold a file object. Interprets leading "~" as user home directory.
108
89
*
109
- * @param file File object to see if the path part of the filename exists, if not then create it.
110
- * @return target File, as an absolute path , with parent directories created recursively if needed
90
+ * @param file File object to see if the directory of the filename exists, if not then create it.
91
+ * @return An absolute Path to the file , with parent directories created recursively as needed
111
92
* @throws IOException In case of IO Error
112
93
*/
113
94
public static Path ensureFilePath (Path file ) throws IOException {
@@ -134,20 +115,19 @@ public static File getFile(String path) {
134
115
135
116
/**
136
117
* Gets the absolute Pile for a given file name. Interprets leading "~" as user home directory.
137
- * @param path Path as a string
118
+ * @param pathName Path as a string
138
119
* @return Path instance representing the given absolute path
139
120
*/
140
- public static Path getPath (String path ) {
141
- if (path .startsWith ("~" )) {
142
- path =System .getProperty ("user.home" )+path .substring (1 );
143
- return Path .of (path );
121
+ public static Path getPath (String pathName ) {
122
+ if (pathName .startsWith ("~" )) {
123
+ pathName =System .getProperty ("user.home" )+pathName .substring (1 );
144
124
} else {
145
125
// ensure an absolute path
146
- if (!path .startsWith ("/" )) {
147
- path = "/" + path ;
126
+ if (!pathName .startsWith (File . separator )) {
127
+ pathName = File . separator + pathName ;
148
128
}
149
- return Path .of (path );
150
129
}
130
+ return new File (pathName ).toPath ();
151
131
}
152
132
153
133
0 commit comments