Commit e97ab872 authored by Ricki Hirner's avatar Ricki Hirner

Minor optimizations

parent e8d42d93
Pipeline #9100199 passed with stage
in 4 minutes and 6 seconds
......@@ -8,7 +8,6 @@
package at.bitfire.dav4android
import android.text.TextUtils
import at.bitfire.dav4android.exception.*
import at.bitfire.dav4android.property.GetContentType
import at.bitfire.dav4android.property.GetETag
......@@ -135,7 +134,7 @@ open class DavResource @JvmOverloads constructor(
checkStatus(response!!, false)
val eTag = response.header("ETag")
if (TextUtils.isEmpty(eTag))
if (eTag.isNullOrEmpty())
properties -= GetETag.NAME
else
properties[GetETag.NAME] = GetETag(eTag)
......@@ -184,7 +183,7 @@ open class DavResource @JvmOverloads constructor(
checkStatus(response!!, true)
val eTag = response.header("ETag")
if (TextUtils.isEmpty(eTag))
if (eTag.isNullOrEmpty())
properties -= GetETag.NAME
else
properties[GetETag.NAME] = GetETag(eTag)
......
......@@ -8,7 +8,6 @@
package at.bitfire.dav4android
import android.text.TextUtils
import java.util.*
import java.util.Collections.unmodifiableMap
......@@ -107,7 +106,7 @@ class PropertyCollection {
val s = LinkedList<String>()
for ((name, value) in getMap())
s.add("$name: $value")
return "[" + TextUtils.join(", ", s) + "]"
return "[${s.joinToString(", ")}]"
}
}
......@@ -11,9 +11,7 @@ package at.bitfire.dav4android.exception
import at.bitfire.dav4android.Constants
import okhttp3.Response
import okio.Buffer
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.Serializable
import java.io.*
open class HttpException: Exception, Serializable {
......@@ -34,6 +32,9 @@ open class HttpException: Exception, Serializable {
response = null
}
/**
* Brings [response] into an readable format. Reads and closes the [response] body.
*/
constructor(response: Response): super("${response.code()} ${response.message()}") {
status = response.code()
......@@ -55,8 +56,7 @@ open class HttpException: Exception, Serializable {
val buffer = Buffer()
it.writeTo(buffer)
val baos = ByteArrayOutputStream()
while (!buffer.exhausted())
appendByte(baos, buffer.readByte())
formatByteStream(buffer.inputStream(), baos)
formatted.append("\n").append(baos.toString())
} catch (e: IOException) {
Constants.log.warning("Couldn't read request body")
......@@ -75,8 +75,7 @@ open class HttpException: Exception, Serializable {
response.body()?.use {
try {
val baos = ByteArrayOutputStream()
for (b in it.bytes())
appendByte(baos, b)
formatByteStream(it.byteStream(), baos)
formatted.append("\n").append(baos.toString())
} catch(e: IOException) {
Constants.log.warning("Couldn't read response body")
......@@ -85,12 +84,17 @@ open class HttpException: Exception, Serializable {
this.response = formatted.toString()
}
private fun appendByte(stream: ByteArrayOutputStream, b: Byte) {
private fun formatByteStream(input: InputStream, output: ByteArrayOutputStream) {
OutputStreamWriter(output).use { writer ->
while (input.available() != 0) {
val b = input.read()
when (b) {
'\r'.toByte() -> stream.write("[CR]".toByteArray())
'\n'.toByte() -> stream.write("[LF]\n".toByteArray())
in 0x20..0x7E -> stream.write(b.toInt()) // printable ASCII
else -> stream.write("[${String.format("%02x", b)}]".toByteArray())
'\r'.toInt() -> writer.append("[CR]")
'\n'.toInt() -> writer.append("[LF]\n")
in 0x20..0x7E -> writer.write(b) // printable ASCII
else -> writer.append("[${String.format("%02x", b)}]")
}
}
}
}
......
......@@ -8,7 +8,6 @@
package at.bitfire.dav4android.property
import android.text.TextUtils
import at.bitfire.dav4android.Property
import at.bitfire.dav4android.PropertyFactory
import at.bitfire.dav4android.XmlUtils
......@@ -19,7 +18,7 @@ abstract class HrefListProperty: Property {
val hrefs = LinkedList<String>()
override fun toString() = "href=[" + TextUtils.join(", ", hrefs) + "]"
override fun toString() = "href=[" + hrefs.joinToString(", ") + "]"
abstract class Factory: PropertyFactory {
......
......@@ -8,7 +8,6 @@
package at.bitfire.dav4android.property
import android.text.TextUtils
import at.bitfire.dav4android.Constants
import at.bitfire.dav4android.Property
import at.bitfire.dav4android.PropertyFactory
......@@ -31,7 +30,7 @@ class ResourceType: Property {
val types = mutableSetOf<Property.Name>()
override fun toString() = "[" + TextUtils.join(", ", types) + "]"
override fun toString() = "[${types.joinToString { ", " }}]"
class Factory: PropertyFactory {
......
......@@ -8,7 +8,6 @@
package at.bitfire.dav4android.property
import android.text.TextUtils
import at.bitfire.dav4android.Constants
import at.bitfire.dav4android.Property
import at.bitfire.dav4android.PropertyFactory
......@@ -27,7 +26,7 @@ class SupportedAddressData: Property {
val types = mutableSetOf<MediaType>()
fun hasVCard4() = types.any { "text/vcard; version=4.0".equals(it.toString(), true) }
override fun toString() = "[" + TextUtils.join(", ", types) + "]"
override fun toString() = "[${types.joinToString(", ")}]"
class Factory: PropertyFactory {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment