Skip to content

Commit 6bce106

Browse files
Martijn HoekstraMartijn Hoekstra
Martijn Hoekstra
authored and
Martijn Hoekstra
committed
force UTF-8 for slurp and write
1 parent e104be1 commit 6bce106

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

test/test/CompilerTest.scala

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,36 @@ abstract class CompilerTest {
449449
/** Recursively copy over source files and directories, excluding extensions
450450
* that aren't in extensionsToCopy. */
451451
private def recCopyFiles(sourceFile: Path, dest: Path): Unit = {
452-
processFileDir(sourceFile, { sf =>
452+
def copyfile(file: SFile, bytewise: Boolean): Unit = {
453+
if (bytewise) {
454+
val in = file.inputStream()
455+
val out = SFile(dest).outputStream()
456+
val buffer = new Array[Byte](1024)
457+
def loop(available: Int):Unit = {
458+
if (available < 0) {()}
459+
else {
460+
out.write(buffer, 0, available)
461+
val read = in.read(buffer)
462+
loop(read)
463+
}
464+
}
465+
loop(0)
466+
in.close()
467+
out.close()
468+
} else {
469+
try {
470+
SFile(dest)(scala.io.Codec.UTF8).writeAll((s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $file !!!!! */").replace("\\", "/"), file.slurp("UTF-8"))
471+
} catch {
472+
case unmappable: java.nio.charset.MalformedInputException =>
473+
copyfile(file, true) //there are bytes that can't be mapped with UTF-8. Bail and just do a straight byte-wise copy without the warning header.
474+
}
475+
}
476+
}
477+
478+
processFileDir(sourceFile, { sf =>
453479
if (extensionsToCopy.contains(sf.extension)) {
454480
dest.parent.jfile.mkdirs
455-
dest.toFile.writeAll(s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $sf !!!!! */", sf.slurp)
481+
copyfile(sf, false)
456482
} else {
457483
log(s"WARNING: ignoring $sf")
458484
}

0 commit comments

Comments
 (0)