Best way to translate this java code into kotlin

前端 未结 3 535
后悔当初
后悔当初 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: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);
    }
    

提交回复
热议问题