浏览代码

pref: Optimize code

drake 1 年之前
父节点
当前提交
6fc3f4bed5

+ 22 - 30
net/src/main/java/com/drake/net/body/BodyExtension.kt

@@ -35,8 +35,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
 fun RequestBody.toNetRequestBody(listeners: ConcurrentLinkedQueue<ProgressListener>? = null) = NetRequestBody(this, listeners)
 
 fun ResponseBody.toNetResponseBody(
-    listeners: ConcurrentLinkedQueue<ProgressListener>? = null,
-    complete: (() -> Unit)? = null
+    listeners: ConcurrentLinkedQueue<ProgressListener>? = null, complete: (() -> Unit)? = null
 ) = NetResponseBody(this, listeners, complete)
 
 /**
@@ -44,15 +43,10 @@ fun ResponseBody.toNetResponseBody(
  * @param byteCount 复制的字节长度, 允许超过实际长度, 如果-1则返回完整的字符串内容
  */
 fun RequestBody.peekBytes(byteCount: Long = 1024 * 1024): ByteString {
-    return when (this) {
-        is NetRequestBody -> peekBytes(byteCount)
-        else -> {
-            val buffer = Buffer()
-            writeTo(buffer)
-            val maxSize = if (byteCount < 0) buffer.size else minOf(buffer.size, byteCount)
-            buffer.readByteString(maxSize)
-        }
-    }
+    val buffer = Buffer()
+    writeTo(buffer)
+    val maxSize = if (byteCount < 0) buffer.size else minOf(buffer.size, byteCount)
+    return buffer.readByteString(maxSize)
 }
 
 /**
@@ -60,39 +54,37 @@ fun RequestBody.peekBytes(byteCount: Long = 1024 * 1024): ByteString {
  * @param byteCount 复制的字节长度, 允许超过实际长度, 如果-1则返回完整的字符串内容
  */
 fun ResponseBody.peekBytes(byteCount: Long = 1024 * 1024): ByteString {
-    return when (this) {
-        is NetResponseBody -> peekBytes(byteCount)
-        else -> {
-            val peeked = source().peek()
-            peeked.request(byteCount)
-            val maxSize = if (byteCount < 0) peeked.buffer.size else minOf(byteCount, peeked.buffer.size)
-            peeked.readByteString(maxSize)
-        }
-    }
+    val peeked = source().peek()
+    peeked.request(byteCount)
+    val maxSize = if (byteCount < 0) peeked.buffer.size else minOf(byteCount, peeked.buffer.size)
+    return peeked.readByteString(maxSize)
 }
 
 /**
- * 通过判断[okhttp3.Headers]里面的Content-Disposition是否存在filename属性来确定是否为文件类型[MultipartBody.Part]
+ * 获取Content-Disposition里面的filename属性值
+ * 可以此来判断是否为文件类型
  */
-fun MultipartBody.Part.isFile(): Boolean {
-    val contentDisposition = headers?.get("Content-Disposition") ?: return false
-    return ";\\s${"filename"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1) != null
+fun MultipartBody.Part.fileName(): String? {
+    val contentDisposition = headers?.get("Content-Disposition") ?: return null
+    val regex = ";\\s${"filename"}=\"(.+?)\"".toRegex()
+    val matchResult = regex.find(contentDisposition)
+    return matchResult?.groupValues?.getOrNull(1)
 }
 
 /**
- * 返回Content-Disposition里面的字段名称
+ * 获取Content-Disposition里面的字段名称
  */
 fun MultipartBody.Part.name(): String? {
     val contentDisposition = headers?.get("Content-Disposition") ?: return null
-    return ";\\s${"name"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1) ?: ""
+    val regex = ";\\s${"name"}=\"(.+?)\"".toRegex()
+    val matchResult = regex.find(contentDisposition)
+    return matchResult?.groupValues?.getOrNull(1)
 }
 
 /**
  * 将[MultipartBody.Part.body]作为字符串返回
- * 如果[MultipartBody.Part]是文件类型则返回的是文件名称, 确定文件类型请参考[MultipartBody.Part.isFile]
+ * 如果[MultipartBody.Part]有指定fileName那么视为文件类型将返回fileName值而不是文件内容
  */
 fun MultipartBody.Part.value(): String? {
-    val contentDisposition = headers?.get("Content-Disposition") ?: return null
-    return ";\\s${"filename"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1)
-        ?: body.peekBytes().utf8()
+    return fileName() ?: body.peekBytes().utf8()
 }

+ 0 - 11
net/src/main/java/com/drake/net/body/NetRequestBody.kt

@@ -71,17 +71,6 @@ class NetRequestBody(
         }
     }
 
-    /**
-     * 复制一段指定长度的字符串内容
-     * @param byteCount 复制的字节长度, 允许超过实际长度, 如果-1则返回完整的字符串内容
-     */
-    fun peekBytes(byteCount: Long = 1024 * 1024): ByteString {
-        val buffer = Buffer()
-        body.writeTo(buffer)
-        val maxSize = if (byteCount < 0) buffer.size else minOf(buffer.size, byteCount)
-        return buffer.readByteString(maxSize)
-    }
-
     private fun Sink.toProgress() = object : ForwardingSink(this) {
         private var writeByteCount = 0L
 

+ 0 - 11
net/src/main/java/com/drake/net/body/NetResponseBody.kt

@@ -54,17 +54,6 @@ class NetResponseBody(
         return bufferedSource
     }
 
-    /**
-     * 复制一段指定长度的字符串内容
-     * @param byteCount 复制的字节长度, 允许超过实际长度, 如果-1则返回完整的字符串内容
-     */
-    fun peekBytes(byteCount: Long = 1024 * 1024 * 4): ByteString {
-        val peeked = body.source().peek()
-        peeked.request(byteCount)
-        val maxSize = if (byteCount < 0) peeked.buffer.size else minOf(byteCount, peeked.buffer.size)
-        return peeked.readByteString(maxSize)
-    }
-
     private fun Source.toProgress() = object : ForwardingSource(this) {
         private var readByteCount: Long = 0
 

+ 8 - 2
net/src/main/java/com/drake/net/utils/FileUtils.kt

@@ -75,13 +75,19 @@ fun File.mediaType(): MediaType? {
  * @param contentType 如果为null则通过判断扩展名来生成MediaType
  */
 fun File.toRequestBody(contentType: MediaType? = null): RequestBody {
+    val fileMediaType = contentType ?: mediaType()
     return object : RequestBody() {
-        override fun contentType() = contentType ?: mediaType()
+
+        override fun contentType(): MediaType? {
+            return fileMediaType
+        }
 
         override fun contentLength() = length()
 
         override fun writeTo(sink: BufferedSink) {
-            source().use { source -> sink.writeAll(source) }
+            source().use { source ->
+                sink.writeAll(source)
+            }
         }
     }
 }