From b5e2279106cfd94cfeb0fbada42d79a53680a414 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 8 Jul 2022 01:03:14 -0700 Subject: [PATCH 1/2] Write-back is UTF-8 --- compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala index 9c85d2f5bd1d..71f023349cc6 100644 --- a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala +++ b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala @@ -8,6 +8,8 @@ import collection.mutable import scala.annotation.tailrec import dotty.tools.dotc.reporting.Reporter +import java.nio.charset.StandardCharsets.UTF_8 + /** Handles rewriting of Scala2 files to Dotty */ object Rewrites { private class PatchedFiles extends mutable.HashMap[SourceFile, Patches] @@ -56,7 +58,7 @@ object Rewrites { def writeBack(): Unit = { val chars = apply(source.underlying.content) - val bytes = new String(chars).getBytes + val bytes = new String(chars).getBytes(UTF_8) val out = source.file.output out.write(bytes) out.close() From 2085bfd3cc795f89897a75edc4ade51ede8e505f Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 8 Jul 2022 01:23:44 -0700 Subject: [PATCH 2/2] Write-back uses unbuffered writer Encoder has 8Kb buffer. --- compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala index 71f023349cc6..96e88e5c68ae 100644 --- a/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala +++ b/compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala @@ -8,6 +8,7 @@ import collection.mutable import scala.annotation.tailrec import dotty.tools.dotc.reporting.Reporter +import java.io.OutputStreamWriter import java.nio.charset.StandardCharsets.UTF_8 /** Handles rewriting of Scala2 files to Dotty */ @@ -56,13 +57,11 @@ object Rewrites { ds } - def writeBack(): Unit = { + def writeBack(): Unit = val chars = apply(source.underlying.content) - val bytes = new String(chars).getBytes(UTF_8) - val out = source.file.output - out.write(bytes) - out.close() - } + val osw = OutputStreamWriter(source.file.output, UTF_8) + try osw.write(chars, 0, chars.length) + finally osw.close() } /** If -rewrite is set, record a patch that replaces the range