mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-05-03 15:11:42 +01:00
Add debug and testing apis to Spinner.
This commit is contained in:
committed by
Alex Hart
parent
5d207932c9
commit
8cd92a400c
@@ -28,6 +28,48 @@ sealed class PluginResult(val type: String) {
|
||||
val json: String
|
||||
) : PluginResult("json")
|
||||
|
||||
class TsvResult(
|
||||
private val columns: List<String>,
|
||||
private val rows: Iterator<List<String?>>
|
||||
) : PluginResult("tsv") {
|
||||
fun toInputStream(): InputStream = object : InputStream() {
|
||||
private var buffer: ByteArray = columns.joinToString("\t").toByteArray()
|
||||
private var pos: Int = 0
|
||||
private var done: Boolean = false
|
||||
|
||||
override fun read(): Int {
|
||||
if (!fill()) return -1
|
||||
return buffer[pos++].toInt() and 0xFF
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray, off: Int, len: Int): Int {
|
||||
if (!fill()) return -1
|
||||
val toRead = minOf(buffer.size - pos, len)
|
||||
System.arraycopy(buffer, pos, b, off, toRead)
|
||||
pos += toRead
|
||||
return toRead
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
(rows as? AutoCloseable)?.close()
|
||||
}
|
||||
|
||||
private fun fill(): Boolean {
|
||||
while (pos >= buffer.size) {
|
||||
if (done) return false
|
||||
if (rows.hasNext()) {
|
||||
buffer = ("\n" + rows.next().joinToString("\t") { it ?: "" }).toByteArray()
|
||||
pos = 0
|
||||
} else {
|
||||
done = true
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ErrorResult(
|
||||
val status: Response.Status = Response.Status.INTERNAL_ERROR,
|
||||
val message: String
|
||||
|
||||
@@ -286,6 +286,9 @@ internal class SpinnerServer(
|
||||
is PluginResult.JsonResult -> {
|
||||
return newFixedLengthResponse(Response.Status.OK, "application/json", pluginResult.json)
|
||||
}
|
||||
is PluginResult.TsvResult -> {
|
||||
return newChunkedResponse(Response.Status.OK, "text/tab-separated-values", pluginResult.toInputStream())
|
||||
}
|
||||
is PluginResult.RawFileResult -> {
|
||||
return newFixedLengthResponse(Response.Status.OK, pluginResult.mimeType, pluginResult.data, pluginResult.length)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user