Skip to content

Commit

Permalink
Add readUrlAsText helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Dec 30, 2020
1 parent 24a136a commit 4c7424d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/kotlin/net/servicestack/gistcafe/Inspect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package net.servicestack.gistcafe
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.io.BufferedReader
import java.io.FileWriter
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.math.abs
Expand Down Expand Up @@ -204,6 +207,21 @@ class Inspect {
val json = gson.toJson(obj)
return gson.fromJson(json, object : TypeToken<Map<String, Any?>>() {}.type)
}

/**
* Helper to download the text contents of a URL, because Java needs it.
* @param url the URL to download
*/
@JvmStatic fun readUrlAsText(url: URL): String {
val sb = StringBuilder()
BufferedReader(InputStreamReader(url.openStream())).use { reader ->
var line: String?
while (reader.readLine().also { line = it } != null) {
sb.append(line)
}
}
return sb.toString()
}
}
}

Expand Down

0 comments on commit 4c7424d

Please sign in to comment.