Skip to content

Commit 5eff49b

Browse files
authored
Merge pull request #100 from melissalinkert/readfile-encoding
Add DataTools.readFile signature that accepts an encoding name
2 parents de268dd + 527f3c9 commit 5eff49b

2 files changed

Lines changed: 75 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
/**

src/test/java/loci/common/utests/DataToolsTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232

3333
package loci.common.utests;
3434

35+
import loci.common.ByteArrayHandle;
3536
import loci.common.DataTools;
37+
import loci.common.Location;
3638
import java.util.Locale;
3739

3840
import static org.testng.AssertJUnit.assertEquals;
41+
import static org.testng.AssertJUnit.assertFalse;
3942
import static org.testng.AssertJUnit.fail;
4043
import org.testng.annotations.DataProvider;
4144
import org.testng.annotations.Test;
@@ -236,4 +239,59 @@ public void testParseDouble(String locale) {
236239
public void testThreadSafety() {
237240
assertEquals(DataTools.parseDouble("1.0"), 1.0d);
238241
}
242+
243+
244+
@Test
245+
public void testReadFileDefaultEncoding() {
246+
String src = "µ€œẞ";
247+
String tmpFile = "utf-test.txt";
248+
try {
249+
byte[] bytes = src.getBytes("UTF-8");
250+
Location.mapFile(tmpFile, new ByteArrayHandle(bytes));
251+
String result = DataTools.readFile(tmpFile);
252+
assertEquals(src, result);
253+
}
254+
catch (Exception e) {
255+
fail(e.getMessage());
256+
}
257+
finally {
258+
Location.mapFile(tmpFile, null);
259+
}
260+
}
261+
262+
@Test
263+
public void testReadFileDifferentEncoding() {
264+
String src = "µ€œ";
265+
String tmpFile = "windows-1252-test.txt";
266+
try {
267+
byte[] bytes = src.getBytes("windows-1252");
268+
Location.mapFile(tmpFile, new ByteArrayHandle(bytes));
269+
String result = DataTools.readFile(tmpFile, "windows-1252");
270+
assertEquals(src, result);
271+
}
272+
catch (Exception e) {
273+
fail(e.getMessage());
274+
}
275+
finally {
276+
Location.mapFile(tmpFile, null);
277+
}
278+
}
279+
280+
@Test
281+
public void testReadFileWrongEncoding() {
282+
String src = "µ€œ";
283+
String tmpFile = "windows-1252-vs-utf8-test.txt";
284+
try {
285+
byte[] bytes = src.getBytes("UTF-8");
286+
Location.mapFile(tmpFile, new ByteArrayHandle(bytes));
287+
String result = DataTools.readFile(tmpFile, "windows-1252");
288+
assertFalse(src.equals(result));
289+
}
290+
catch (Exception e) {
291+
fail(e.getMessage());
292+
}
293+
finally {
294+
Location.mapFile(tmpFile, null);
295+
}
296+
}
239297
}

0 commit comments

Comments
 (0)