Best way to translate this java code into kotlin

前端 未结 3 534
后悔当初
后悔当初 2021-01-13 15:57
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream in = connection.getInputStream();
int bytesRead = 0;
         


        
相关标签:
3条回答
  • 2021-01-13 16:32

    Instead of translating the code literally, make use of Kotlin's stdlib which offers a number of useful extension functions. Here's one version

    val text = URL(urlSpec).openConnection().inputStream.bufferedReader().use { it.readText() }
    

    To answer the original question: You're right, assignments are not treated as expressions. Therefore you will need to separate the assignment and the comparison. Take a look at the implementation in the stdlib for an example:

    public fun Reader.copyTo(out: Writer, bufferSize: Int = DEFAULT_BUFFER_SIZE): Long {
        var charsCopied: Long = 0
        val buffer = CharArray(bufferSize)
        var chars = read(buffer)
        while (chars >= 0) {
            out.write(buffer, 0, chars)
            charsCopied += chars
            chars = read(buffer)
        }
        return charsCopied
    }
    

    Source: https://github.com/JetBrains/kotlin/blob/a66fc9043437d2e75f04feadcfc63c61b04bd196/libraries/stdlib/src/kotlin/io/ReadWrite.kt#L114

    0 讨论(0)
  • 2021-01-13 16:33

    You could use apply block to execute the assignment:

    val input= connection.getInputStream();
    var bytesRead = 0;
    val buffer = ByteArray(1024)
    while (input.read(buffer).apply { bytesRead = this } > 0) {
        out.write(buffer, 0, bytesRead);
    }
    
    0 讨论(0)
  • 2021-01-13 16:49

    You could use something like this

    This operation may be little heavy as a function is created each iteration.

    val url = URL("urlSpec")
    val connection = url.openConnection() as HttpURLConnection
    val `in` = connection.inputStream
    val buffer = ByteArray(1024)
    var bytesRead: Int? = null
    while ({ bytesRead = `in`.read(buffer); bytesRead }() != null) {
            out.write(buffer, 0, bytesRead!!)
    }
    out.close()
    
    0 讨论(0)
提交回复
热议问题