Skip to content

Commit

Permalink
Compatible with java environment
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Oct 30, 2022
1 parent c0a9d79 commit 19d8b22
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
49 changes: 49 additions & 0 deletions rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rxhttp.wrapper.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Response;
import okhttp3.ResponseBody;
import rxhttp.Platform;
import rxhttp.wrapper.OkHttpCompat;
import rxhttp.wrapper.callback.IConverter;
import rxhttp.wrapper.entity.ParameterizedTypeImpl;

/**
* User: ljx
* Date: 2022/10/30
* Time: 14:44
*/
public class Converter {

public static <T> T convertTo(Response response, Type rawType, Type... types) throws IOException {
return convert(response, ParameterizedTypeImpl.get(rawType, types));
}

public static <T> T convertToParameterized(Response response, Type rawType, Type... actualTypes) throws IOException {
return convert(response, ParameterizedTypeImpl.getParameterized(rawType, actualTypes));
}

@SuppressWarnings("unchecked")
public static <T> T convert(Response response, Type type) throws IOException {
ResponseBody body = OkHttpCompat.throwIfFail(response);
LogUtil.log(response, null);
if (type == ResponseBody.class) {
try {
return (T) OkHttpCompat.buffer(body);
} finally {
body.close();
}
} else if (Platform.get().isAndroid() && type == Bitmap.class) {
return (T) BitmapFactory.decodeStream(body.byteStream());
} else {
boolean needDecodeResult = OkHttpCompat.needDecodeResult(response);
IConverter converter = OkHttpCompat.request(response).tag(IConverter.class);
return converter.convert(body, type, needDecodeResult);
}
}
}
46 changes: 9 additions & 37 deletions rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
@file:JvmName("Converter")

package rxhttp.wrapper.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import okhttp3.Response
import okhttp3.ResponseBody
import rxhttp.Platform
import rxhttp.wrapper.OkHttpCompat
import rxhttp.wrapper.callback.IConverter
import rxhttp.wrapper.entity.ParameterizedTypeImpl
import java.io.IOException
import java.lang.reflect.Type
import kotlin.reflect.KClass
Expand All @@ -19,40 +10,21 @@ import kotlin.reflect.KClass
* Date: 2020/8/15
* Time: 11:48
*/

@Throws(IOException::class)
fun <T> Response.convertTo(rawType: KClass<*>, vararg types: Type): T {
return convertTo(rawType.java, *types)
}
fun <T> Response.convertTo(rawType: KClass<*>, vararg types: Type): T =
convertTo(rawType.java, *types)

@Throws(IOException::class)
fun <T> Response.convertTo(rawType: Type, vararg types: Type): T {
return convert(ParameterizedTypeImpl.get(rawType, *types))
}
fun <T> Response.convertTo(rawType: Type, vararg types: Type): T =
Converter.convertTo(this, rawType, *types)

@Throws(IOException::class)
fun <T> Response.convertToParameterized(rawType: KClass<*>, vararg actualTypeArguments: Type): T {
return convertToParameterized(rawType.java, *actualTypeArguments)
}
fun <T> Response.convertToParameterized(rawType: KClass<*>, vararg actualTypes: Type): T =
convertToParameterized(rawType.java, *actualTypes)

@Throws(IOException::class)
fun <T> Response.convertToParameterized(rawType: Type, vararg actualTypeArguments: Type): T {
return convert(ParameterizedTypeImpl.getParameterized(rawType, *actualTypeArguments))
}
fun <T> Response.convertToParameterized(rawType: Type, vararg actualTypes: Type): T =
Converter.convertToParameterized(this, rawType, *actualTypes)

@Suppress("UNCHECKED_CAST")
@Throws(IOException::class)
fun <T> Response.convert(type: Type): T {
val body = OkHttpCompat.throwIfFail(this)
LogUtil.log(this, null)
return if (type === ResponseBody::class.java) {
body.use { OkHttpCompat.buffer(it) as T }
} else if (Platform.get().isAndroid && type === Bitmap::class.java) {
BitmapFactory.decodeStream(body.byteStream()) as T
} else {
val needDecodeResult = OkHttpCompat.needDecodeResult(this)
val converter = OkHttpCompat.request(this).tag(IConverter::class.java)
converter?.convert(body, type, needDecodeResult)
?: throw IllegalStateException("Converter Could not deserialize body as $type")
}
}
fun <T> Response.convert(type: Type): T = Converter.convert(this, type)

0 comments on commit 19d8b22

Please sign in to comment.