簡體   English   中英

Kotlin 字符串最大長度? (帶有長字符串的 Kotlin 文件未編譯)

[英]Kotlin String max length? (Kotlin file with a long String is not compiling)

根據這個答案Java 最多可以容納 2^31 - 1 個字符。 我正在嘗試進行基准測試和其他東西,所以我嘗試創建大量字符串並將其寫入如下文件:

import java.io.*

fun main() {
    val out = File("ouput.txt").apply { createNewFile() }.printWriter()
    sequence {
        var x = 0
        while (true) {
            yield("${++x} ${++x} ${++x} ${++x} ${++x}")
        }
    }.take(5000).forEach { out.println(it) }
    out.close()
}

然后output.txt文件包含如下內容:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
// ... 5000 lines

然后我將文件的所有內容復制到一個字符串中,以便對某些函數進行一些基准測試,所以它看起來是這樣的:

import kotlin.system.*

fun main() {
    val inputs = """
        1 2 3 4 5
        6 7 8 9 10
        11 12 13 14 15
        16 17 18 19 20
        21 22 23 24 25
        // ... 5000 lines
        24986 24987 24988 24989 24990
        24991 24992 24993 24994 24995
        24996 24997 24998 24999 25000

    """.trimIndent()
    measureNanoTime {
        inputs.reader().forEachLine { line ->
            val (a, b, c, d, e) = line.split(" ").map(String::toInt)
        }
    }.div(5000).let(::println)
}

文件/字符串的總字符數為 138894

字符串最多可以容納 2147483647

但是 Kotlin 代碼無法編譯(最后一個文件)它會引發編譯錯誤:

e: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong bytecode generated
// more lines
The root cause java.lang.IllegalArgumentException was thrown at: org.jetbrains.org.objectweb.asm.ByteVector.putUTF8(ByteVector.java:246)
    at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.kt:92)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:971)
    ... 43 more
Caused by: java.lang.IllegalArgumentException: UTF8 string too large

這是堆棧跟蹤異常的總日志: https://gist.github.com/Animeshz/1a18a7e99b0c0b913027b7fb36940c07

java class 文件中有限制,字符串常量的長度必須適合 16 位,即。 65535 字節(不是字符)是源代碼中字符串的最大長度。

class 文件格式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM