Skip to content

Commit 4c7424d

Browse files
committed
Add readUrlAsText helper
1 parent 24a136a commit 4c7424d

File tree

1 file changed

+18
-0
lines changed
  • src/main/kotlin/net/servicestack/gistcafe

1 file changed

+18
-0
lines changed

src/main/kotlin/net/servicestack/gistcafe/Inspect.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package net.servicestack.gistcafe
33
import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.google.gson.reflect.TypeToken
6+
import java.io.BufferedReader
67
import java.io.FileWriter
8+
import java.io.InputStreamReader
79
import java.lang.StringBuilder
10+
import java.net.URL
811
import java.nio.file.Files
912
import java.nio.file.Paths
1013
import kotlin.math.abs
@@ -204,6 +207,21 @@ class Inspect {
204207
val json = gson.toJson(obj)
205208
return gson.fromJson(json, object : TypeToken<Map<String, Any?>>() {}.type)
206209
}
210+
211+
/**
212+
* Helper to download the text contents of a URL, because Java needs it.
213+
* @param url the URL to download
214+
*/
215+
@JvmStatic fun readUrlAsText(url: URL): String {
216+
val sb = StringBuilder()
217+
BufferedReader(InputStreamReader(url.openStream())).use { reader ->
218+
var line: String?
219+
while (reader.readLine().also { line = it } != null) {
220+
sb.append(line)
221+
}
222+
}
223+
return sb.toString()
224+
}
207225
}
208226
}
209227

0 commit comments

Comments
 (0)