Skip to content

Commit 93dd12f

Browse files
committed
File / IO utility tweaks
1 parent 35cb906 commit 93dd12f

File tree

4 files changed

+21
-50
lines changed

4 files changed

+21
-50
lines changed

convex-core/src/main/java/convex/core/util/ConfigUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static ACell readConfig(URI uri) throws IOException {
2424

2525

2626
public static ACell readConfig(InputStream resource) throws IOException {
27-
String config=FileUtils.loadFileAsString(resource);
27+
String config=Utils.readString(resource);
2828
ACell result=JSONUtils.parseJSON5(config);
2929
return result;
3030
}

convex-core/src/main/java/convex/core/util/FileUtils.java

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import java.io.File;
44
import java.io.FileNotFoundException;
55
import java.io.IOException;
6-
import java.io.InputStream;
76
import java.nio.charset.StandardCharsets;
87
import java.nio.file.Files;
98
import java.nio.file.Path;
10-
import java.nio.file.Paths;
119

1210
import convex.core.data.ACell;
1311
import convex.core.data.Blob;
@@ -20,7 +18,7 @@
2018
public class FileUtils {
2119

2220
/**
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
2422
* @param fileName File to load
2523
* @return String contents of file
2624
* @throws IOException in case of IO failure
@@ -32,32 +30,15 @@ public static String loadFileAsString(String fileName) throws IOException {
3230
byte[] bs = System.in.readAllBytes();
3331
result = new String(bs);
3432
} else {
35-
Path path = Paths.get(fileName);
36-
if (!path.toFile().exists()) {
33+
Path path = getPath(fileName);
34+
if (!Files.exists(path)) {
3735
throw new FileNotFoundException("File does not exist: " + path);
3836
}
3937
result = Files.readString(path, StandardCharsets.UTF_8);
4038
}
4139
return result;
4240
}
4341

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-
6142
public static Blob loadFileAsBlob(Path file) throws IOException {
6243
return Blob.wrap(Files.readAllBytes(file));
6344
}
@@ -104,10 +85,10 @@ public static File ensureFilePath(File file) throws IOException {
10485
}
10586

10687
/**
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.
10889
*
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
11192
* @throws IOException In case of IO Error
11293
*/
11394
public static Path ensureFilePath(Path file) throws IOException {
@@ -134,20 +115,19 @@ public static File getFile(String path) {
134115

135116
/**
136117
* 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
138119
* @return Path instance representing the given absolute path
139120
*/
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);
144124
} else {
145125
// ensure an absolute path
146-
if (!path.startsWith("/")) {
147-
path="/"+path;
126+
if (!pathName.startsWith(File.separator)) {
127+
pathName=File.separator+pathName;
148128
}
149-
return Path.of(path);
150129
}
130+
return new File(pathName).toPath();
151131
}
152132

153133

convex-core/src/main/java/convex/core/util/Utils.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,11 @@ public static InputStream getResourceAsStream(String path) throws IOException {
741741
*
742742
* @param inputStream Stream of data to read as UTF-8 string
743743
* @return String content of stream, or null on failure
744+
* @throws IOException
744745
*/
745-
public static String readString(InputStream inputStream) {
746-
try {
747-
ByteArrayOutputStream result = new ByteArrayOutputStream();
748-
byte[] buffer = new byte[1024];
749-
for (int length; (length = inputStream.read(buffer)) != -1; ) {
750-
result.write(buffer, 0, length);
751-
}
752-
// StandardCharsets.UTF_8.name() > JDK 7
753-
return result.toString("UTF-8");
754-
} catch (IOException t) {
755-
return null;
756-
}
746+
public static String readString(InputStream inputStream) throws IOException {
747+
byte[] bytes = inputStream.readAllBytes();
748+
return new String(bytes, StandardCharsets.UTF_8);
757749
}
758750

759751
/**
@@ -850,10 +842,12 @@ public static void setBits(byte[] bs, int numBits, int shift, int bits) {
850842
* @return Blob containing bytes read from buffer
851843
*/
852844
public static AArrayBlob readBufferData(ByteBuffer bb) {
845+
int savedPos=bb.position();
853846
bb.position(0);
854847
int len = bb.remaining();
855848
byte[] bytes = new byte[len];
856849
bb.get(bytes);
850+
bb.position(savedPos);
857851
return Blob.wrap(bytes);
858852
}
859853

convex-core/src/test/java/convex/core/util/FileUtilsTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,8 @@ public void cleanup() throws IOException {
6767
} finally {
6868
Files.delete(TEXT);
6969
}
70-
71-
72-
7370
} finally {
74-
Files.delete(DIR);
71+
Files.deleteIfExists(DIR);
7572
}
7673
}
7774
}

0 commit comments

Comments
 (0)