Skip to content

Commit 96e9d21

Browse files
committed
More UTF-8 string fixes
1 parent f61a524 commit 96e9d21

File tree

17 files changed

+54
-34
lines changed

17 files changed

+54
-34
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ import Denotations.Denotation
1212
import typer.Typer
1313
import typer.ImportInfo._
1414
import Decorators._
15-
import io.{AbstractFile, PlainFile}
15+
import io.{AbstractFile, PlainFile, VirtualFile}
1616
import Phases.unfusedPhases
1717

18-
import scala.io.Codec
1918
import util._
2019
import reporting.Reporter
2120
import rewrites.Rewrites
22-
import java.io.{BufferedWriter, OutputStreamWriter}
2321

2422
import profile.Profiler
2523
import printing.XprintMode
2624
import parsing.Parsers.Parser
2725
import parsing.JavaParsers.JavaParser
2826
import typer.ImplicitRunInfo
29-
import collection.mutable
3027

31-
import dotty.tools.io.VirtualFile
28+
import java.io.{BufferedWriter, OutputStreamWriter}
29+
import java.nio.charset.StandardCharsets
3230

31+
import scala.collection.mutable
3332
import scala.util.control.NonFatal
33+
import scala.io.Codec
3434

3535
/** A compiler run. Exports various methods to compile source files */
3636
class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with ConstraintRunInfo {
@@ -274,7 +274,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
274274
val uuid = java.util.UUID.randomUUID().toString
275275
val ext = if (isJava) ".java" else ".scala"
276276
val virtualFile = new VirtualFile(s"compileFromString-$uuid.$ext")
277-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
277+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name)) // buffering is still advised by javadoc
278278
writer.write(source)
279279
writer.close()
280280
new SourceFile(virtualFile, Codec.UTF8)

compiler/src/dotty/tools/dotc/config/Properties.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.annotation.internal.sharable
66

77
import java.io.IOException
88
import java.util.jar.Attributes.{ Name => AttributeName }
9+
import java.nio.charset.StandardCharsets
910

1011
/** Loads `library.properties` from the jar. */
1112
object Properties extends PropertiesTrait {
@@ -88,13 +89,13 @@ trait PropertiesTrait {
8889
/** This is the encoding to use reading in source files, overridden with -encoding
8990
* Note that it uses "prop" i.e. looks in the scala jar, not the system properties.
9091
*/
91-
def sourceEncoding: String = scalaPropOrElse("file.encoding", "UTF-8")
92+
def sourceEncoding: String = scalaPropOrElse("file.encoding", StandardCharsets.UTF_8.name)
9293
def sourceReader: String = scalaPropOrElse("source.reader", "scala.tools.nsc.io.SourceReader")
9394

9495
/** This is the default text encoding, overridden (unreliably) with
9596
* `JAVA_OPTS="-Dfile.encoding=Foo"`
9697
*/
97-
def encodingString: String = propOrElse("file.encoding", "UTF-8")
98+
def encodingString: String = propOrElse("file.encoding", StandardCharsets.UTF_8.name)
9899

99100
/** The default end of line character.
100101
*/

compiler/src/dotty/tools/dotc/core/tasty/CommentPickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dotty.tools.dotc.core.Contexts._
77
import dotty.tools.tasty.TastyBuffer
88
import TastyBuffer.{Addr, NoAddr}
99

10-
import java.nio.charset.Charset
10+
import java.nio.charset.StandardCharsets
1111

1212
class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Addr, docString: untpd.MemberDef => Option[Comment]):
1313
private val buf = new TastyBuffer(5000)
@@ -17,7 +17,7 @@ class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Addr, docStr
1717

1818
private def pickleComment(addr: Addr, comment: Comment): Unit =
1919
if addr != NoAddr then
20-
val bytes = comment.raw.getBytes(Charset.forName("UTF-8"))
20+
val bytes = comment.raw.getBytes(StandardCharsets.UTF_8)
2121
val length = bytes.length
2222
buf.writeAddr(addr)
2323
buf.writeNat(length)

compiler/src/dotty/tools/dotc/core/tasty/CommentUnpickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import util.HashMap
88
import dotty.tools.tasty.{TastyReader, TastyBuffer}
99
import TastyBuffer.Addr
1010

11-
import java.nio.charset.Charset
11+
import java.nio.charset.StandardCharsets
1212

1313
class CommentUnpickler(reader: TastyReader) {
1414
import reader._
@@ -21,7 +21,7 @@ class CommentUnpickler(reader: TastyReader) {
2121
if (length > 0) {
2222
val bytes = readBytes(length)
2323
val position = new Span(readLongInt())
24-
val rawComment = new String(bytes, Charset.forName("UTF-8"))
24+
val rawComment = new String(bytes, StandardCharsets.UTF_8)
2525
comments(addr) = Comment(position, rawComment)
2626
}
2727
}

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.dotc
22
package decompiler
33

44
import java.io.{OutputStream, PrintStream}
5+
import java.nio.charset.StandardCharsets
56

67
import scala.io.Codec
78

@@ -27,7 +28,7 @@ class DecompilationPrinter extends Phase {
2728
var ps: PrintStream = null
2829
try {
2930
os = File(outputDir.fileNamed("decompiled.scala").path)(Codec.UTF8).outputStream(append = true)
30-
ps = new PrintStream(os, /* autoFlush = */ false, "UTF-8")
31+
ps = new PrintStream(os, /* autoFlush = */ false, StandardCharsets.UTF_8.name)
3132
printToOutput(ps)
3233
}
3334
finally {

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.net.URI
66
import java.io._
77
import java.nio.file._
88
import java.nio.file.attribute.BasicFileAttributes
9+
import java.nio.charset.StandardCharsets
910
import java.util.zip._
1011

1112
import scala.collection._
@@ -298,7 +299,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
298299
private def toSource(uri: URI, sourceCode: String): SourceFile = {
299300
val path = Paths.get(uri)
300301
val virtualFile = new VirtualFile(path.getFileName.toString, path.toString)
301-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
302+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name))
302303
writer.write(sourceCode)
303304
writer.close()
304305
new SourceFile(virtualFile, Codec.UTF8)

compiler/src/dotty/tools/dotc/semanticdb/internal/SemanticdbInputStream.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dotty.tools.dotc.semanticdb.internal
33
import java.io.IOException
44
import java.io.InputStream
55
import java.util.Arrays
6+
import java.nio.charset.StandardCharsets
7+
68
import SemanticdbInputStream._
79

810
import scala.collection.mutable
@@ -434,15 +436,15 @@ class SemanticdbInputStream private (buffer: Array[Byte], input: InputStream) {
434436
def readString(): String = {
435437
val size: Int = readRawVarint32()
436438
if (size <= (bufferSize - bufferPos) && size > 0) {
437-
val result: String = new String(buffer, bufferPos, size, Internal.UTF_8)
439+
val result: String = new String(buffer, bufferPos, size, StandardCharsets.UTF_8)
438440
bufferPos += size
439441
return result
440442
}
441443
else if (size == 0) {
442444
return ""
443445
}
444446
else {
445-
return new String(readRawBytesSlowPath(size), Internal.UTF_8)
447+
return new String(readRawBytesSlowPath(size), StandardCharsets.UTF_8)
446448
}
447449
}
448450

compiler/src/dotty/tools/dotc/semanticdb/internal/SemanticdbOutputStream.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dotty.tools.dotc.semanticdb.internal
33
import java.io.IOException
44
import java.io.OutputStream
55
import java.nio.ByteBuffer
6+
import java.nio.charset.StandardCharsets
7+
68
import SemanticdbOutputStream._
79

810
object SemanticdbOutputStream {
@@ -144,7 +146,7 @@ object SemanticdbOutputStream {
144146
def computeBoolSizeNoTag(value: Boolean): Int = 1
145147

146148
def computeStringSizeNoTag(value: String): Int = {
147-
val bytes = value.getBytes(Internal.UTF_8)
149+
val bytes = value.getBytes(StandardCharsets.UTF_8)
148150
computeRawVarint32Size(bytes.length) + bytes.length
149151
}
150152

@@ -391,7 +393,7 @@ class SemanticdbOutputStream(output: OutputStream, buffer: Array[Byte]) {
391393
}
392394

393395
def writeStringNoTag(value: String): Unit = {
394-
val bytes = value.getBytes(Internal.UTF_8)
396+
val bytes = value.getBytes(StandardCharsets.UTF_8)
395397
writeRawVarint32(bytes.length)
396398
writeRawBytes(bytes)
397399
}
@@ -602,6 +604,5 @@ class SemanticdbOutputStream(output: OutputStream, buffer: Array[Byte]) {
602604
}
603605

604606
object Internal {
605-
val UTF_8 = java.nio.charset.Charset.forName("UTF-8")
606607
val EMPTY_BYTE_ARRAY: Array[Byte] = Array()
607608
}

compiler/src/dotty/tools/dotc/util/ShowPickled.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package dotty.tools.dotc
22
package util
33

44
import java.io.PrintStream
5+
import java.nio.charset.StandardCharsets
56
import java.lang.Long.toHexString
67
import java.lang.Float.intBitsToFloat
78
import java.lang.Double.longBitsToDouble
9+
810
import core.unpickleScala2.PickleBuffer
911
import core.Names._
1012

@@ -18,7 +20,7 @@ object ShowPickled {
1820
case _ => false
1921
}
2022
def readName: String =
21-
if (isName) new String(bytes, "UTF-8")
23+
if (isName) new String(bytes, StandardCharsets.UTF_8)
2224
else sys.error("%s is no name" format tagName)
2325
def nameIndex: Int =
2426
if (hasName) readNat(bytes, 0)

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotty.tools.repl
22

33
import java.io.{File => JFile, PrintStream}
4+
import java.nio.charset.StandardCharsets
45

56
import dotty.tools.dotc.ast.Trees._
67
import dotty.tools.dotc.ast.{tpd, untpd}
@@ -367,7 +368,7 @@ class ReplDriver(settings: Array[String],
367368
case Load(path) =>
368369
val file = new JFile(path)
369370
if (file.exists) {
370-
val contents = Using(scala.io.Source.fromFile(file, "UTF-8"))(_.mkString).get
371+
val contents = Using(scala.io.Source.fromFile(file, StandardCharsets.UTF_8.name))(_.mkString).get
371372
run(contents)
372373
}
373374
else {

compiler/test/dotty/tools/vulpix/RunnerOrchestration.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package vulpix
44

55
import java.io.{ File => JFile, InputStreamReader, BufferedReader, PrintStream }
66
import java.nio.file.Paths
7+
import java.nio.charset.StandardCharsets
78
import java.util.concurrent.atomic.AtomicBoolean
89
import java.util.concurrent.TimeoutException
910

@@ -117,7 +118,7 @@ trait RunnerOrchestration {
117118
val sb = new StringBuilder
118119

119120
if (childStdout eq null)
120-
childStdout = new BufferedReader(new InputStreamReader(process.getInputStream, "UTF-8"))
121+
childStdout = new BufferedReader(new InputStreamReader(process.getInputStream, StandardCharsets.UTF_8))
121122

122123
var childOutput: String = childStdout.readLine()
123124

doc-tool/src/dotty/tools/dottydoc/staticsite/Page.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package dotty.tools
22
package dottydoc
33
package staticsite
44

5+
import model.Package
56

67
import dotc.util.SourceFile
8+
import dotc.core.Contexts.{Context, ctx}
9+
import io.VirtualFile
10+
711
import com.vladsch.flexmark.html.HtmlRenderer
812
import com.vladsch.flexmark.parser.Parser
913
import com.vladsch.flexmark.ext.yaml.front.matter.AbstractYamlFrontMatterVisitor
14+
1015
import java.util.{ Map => JMap, List => JList }
1116
import java.io.{ OutputStreamWriter, BufferedWriter }
17+
import java.nio.charset.StandardCharsets
1218

13-
import io.VirtualFile
14-
import dotc.core.Contexts.{Context, ctx}
15-
import model.Package
1619
import scala.io.Codec
1720

1821
/** When the YAML front matter cannot be parsed, this exception is thrown */
@@ -82,7 +85,7 @@ trait Page {
8285

8386
protected def virtualFile(subSource: String): SourceFile = {
8487
val virtualFile = new VirtualFile(path, path)
85-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
88+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name))
8689
writer.write(subSource)
8790
writer.close()
8891

doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ case class Site(
124124
/** Create virtual file from string `sourceCode` */
125125
private def stringToSourceFile(name: String, path: String, sourceCode: String): SourceFile = {
126126
val virtualFile = new VirtualFile(name, path)
127-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
127+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name))
128128
writer.write(sourceCode)
129129
writer.close()
130130

@@ -413,7 +413,7 @@ case class Site(
413413
}
414414

415415
private def toSourceFile(f: JFile): SourceFile =
416-
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Using(Source.fromFile(f, "UTF-8"))(_.toArray).get)
416+
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Using(Source.fromFile(f, StandardCharsets.UTF_8.name))(_.toArray).get)
417417

418418
private def collectFiles(dir: JFile, includes: String => Boolean): Array[JFile] =
419419
dir

doc-tool/src/dotty/tools/dottydoc/staticsite/Yaml.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import java.util.HashMap;
88
import java.io.ByteArrayInputStream;
9+
import java.nio.charset.StandardCharsets;
910

1011
public class Yaml {
1112

1213
public static HashMap<String, Object> apply(String input)
1314
throws java.io.UnsupportedEncodingException, java.io.IOException {
14-
ByteArrayInputStream is = new ByteArrayInputStream(input.getBytes("UTF-8"));
15+
ByteArrayInputStream is = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
1516
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
1617

1718
TypeReference<HashMap<String, Object>> typeRef =

doc-tool/test/dotty/tools/dottydoc/DottyDocTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.Directory
2020
import org.junit.Assert.fail
2121

2222
import java.io.{ BufferedWriter, OutputStreamWriter }
23+
import java.nio.charset.StandardCharsets
2324

2425
trait DottyDocTest extends MessageRendering {
2526
dotty.tools.dotc.parsing.Scanners // initialize keywords
@@ -80,7 +81,7 @@ trait DottyDocTest extends MessageRendering {
8081

8182
private def sourceFileFromString(name: String, contents: String): SourceFile = {
8283
val virtualFile = new dotty.tools.io.VirtualFile(name)
83-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
84+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8))
8485
writer.write(contents)
8586
writer.close()
8687
new SourceFile(virtualFile, scala.io.Codec.UTF8)

doc-tool/test/dotty/tools/dottydoc/staticsite/SourceFileOps.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package dottydoc
33
package staticsite
44

55
import dotc.util.SourceFile
6-
import java.io.{ BufferedWriter, OutputStreamWriter }
76
import io.VirtualFile
8-
import scala.io.Codec
97

108
import model.Package
119

10+
import java.io.{ BufferedWriter, OutputStreamWriter }
11+
import java.nio.charset.StandardCharsets
12+
13+
import scala.io.Codec
14+
15+
1216
trait SourceFileOps {
1317
import scala.collection.JavaConverters._
1418
val siteRoot = new java.io.File("doc-tool/resources/")
@@ -19,7 +23,7 @@ trait SourceFileOps {
1923

2024
def stringToSource(path: String, sourceCode: String): SourceFile = {
2125
val virtualFile = new VirtualFile(path, path)
22-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
26+
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, StandardCharsets.UTF_8.name))
2327
writer.write(sourceCode)
2428
writer.close()
2529

language-server/test/dotty/tools/languageserver/util/server/TestServer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.io.PrintWriter
44
import java.io.File.{pathSeparator, separator}
55
import java.net.URI
66
import java.nio.file.{Files, Path}
7+
import java.nio.charset.StandardCharsets
78
import java.util
89

910
import dotty.tools.dotc.Main
@@ -138,7 +139,7 @@ class TestServer(testFolder: Path, projects: List[Project]) {
138139
val sourcesDir = sourceDirectory(project, wipe = true)
139140
val sources = project.sources.zipWithIndex.map { case (src, id) =>
140141
val path = sourcesDir.resolve(src.sourceName(id)).toAbsolutePath
141-
Files.write(path, src.text.getBytes("UTF-8"))
142+
Files.write(path, src.text.getBytes(StandardCharsets.UTF_8.name))
142143
path.toString
143144
}
144145

0 commit comments

Comments
 (0)