I’ve written a simple utility to insert a header on code files as my first attempt at Kotlin. One of the tasks is to detect the BOM at the top of a UTF-8 file and replace it at the top of a new file before I place my header in it. I’m finding that Kotlin, when run in the IDE has different behavior when working with BufferedReader than when run on the command line:
// create a BOM character...
val BOM = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(byteArrayOf(0xEF.toByte(), 0xBB.toByte(), 0xBF.toByte())))[0];
// .. later....
BufferedReader(FileReader(file)).use(
{
r->
BufferedWriter(OutputStreamWriter(FileOutputStream(tempFile), "UTF-8")).use(
{
w->
// look for the BOM in the file and write it into the new file first if it exists
r.mark(5);
var firstChar = r.read().toChar();
if(firstChar.equals(BOM)) // NOTE: Also tried firstChar == BOM
// Code continues...
When running from the IDE, this code works fine, the BOM character is successfully decoded from the bytes, detected in the file and it’s copied into the destination file first.
When running from java after making an executeable jar (I use Gradle with the Shadow Jar plugin for this) the test for the first character always fails as the character read from the stream doesn’t equal the BOM.
Is there something I have to do in my JAR to make the Kotlin run work the same as in the IDE?
JDK 1.8.131
Kotlin 1.1.2