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