Skip to content

Commit 925e887

Browse files
Add DataTools.readFile signature that accepts an encoding name
See ome/bioformats#4021
1 parent ee7fb4c commit 925e887

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/main/java/loci/common/DataTools.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ private DataTools() { }
7474
* @param id name of the file to read
7575
* this can be any name supported by Location,
7676
* not necessarily a file on disk
77+
* @param encoding the file encoding, e.g. "UTF-8"
7778
* @return the complete contents of the specified file
7879
* @throws IOException if the file cannot be read or is larger than 2GB
7980
* @see Location#getMappedId(String)
8081
*/
81-
public static String readFile(String id) throws IOException {
82+
public static String readFile(String id, String encoding) throws IOException {
8283
RandomAccessInputStream in = new RandomAccessInputStream(id);
8384
long idLen = in.length();
8485
if (idLen > Integer.MAX_VALUE) {
@@ -88,11 +89,25 @@ public static String readFile(String id) throws IOException {
8889
byte[] b = new byte[len];
8990
in.readFully(b);
9091
in.close();
91-
String data = new String(b, Constants.ENCODING);
92+
String data = new String(b, encoding);
9293
b = null;
9394
return data;
9495
}
9596

97+
/**
98+
* Reads the contents of the given file into a string.
99+
*
100+
* @param id name of the file to read
101+
* this can be any name supported by Location,
102+
* not necessarily a file on disk
103+
* @return the complete contents of the specified file
104+
* @throws IOException if the file cannot be read or is larger than 2GB
105+
* @see Location#getMappedId(String)
106+
*/
107+
public static String readFile(String id) throws IOException {
108+
return readFile(id, Constants.ENCODING);
109+
}
110+
96111
// -- Word decoding - bytes to primitive types --
97112

98113
/**

0 commit comments

Comments
 (0)