URL url = new URL(urlSpec); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); InputStream in = connection.getInputStream(); int bytesRead = 0;
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); }