Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9d899a9 authored by desperateCoder's avatar desperateCoder
Browse files

speed up Request cloning by using copy constructor rather than stream-based copying

parent 3e745d5a
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -45,6 +45,18 @@ public class NextcloudRequest implements Serializable {

    private NextcloudRequest() { }

    public NextcloudRequest(NextcloudRequest ncr) {
        this.method = ncr.method;
        this.requestBody = ncr.requestBody;
        this.url = ncr.url;
        this.token = ncr.token;
        this.packageName = ncr.packageName;
        this.accountName = ncr.accountName;
        this.followRedirects = ncr.followRedirects;
        header = new HashMap<>(ncr.header);
        parameter = new HashMap<>(ncr.parameter);
    }

    public static class Builder implements Serializable {

        private static final long serialVersionUID = 2121321432424242L; //assign a long value
@@ -55,6 +67,10 @@ public class NextcloudRequest implements Serializable {
            ncr = new NextcloudRequest();
        }

        public Builder(Builder cloneSource) {
            ncr = new NextcloudRequest(cloneSource.ncr);
        }

        public NextcloudRequest build() {
            return ncr;
        }
+22 −21
Original line number Diff line number Diff line
@@ -40,7 +40,12 @@ import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.HEAD;
import retrofit2.http.HTTP;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.OPTIONS;
import retrofit2.http.PATCH;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
@@ -100,7 +105,7 @@ public class NextcloudRetrofitServiceMethod<T> {
            throw new InvalidParameterException("Expected: " + parameterAnnotationsArray.length + " params - were: " + args.length);
        }

        NextcloudRequest.Builder rBuilder = cloneSerializable(requestBuilder);
        NextcloudRequest.Builder rBuilder = new NextcloudRequest.Builder(requestBuilder);


        Map<String, String> parameters = new HashMap<>();
@@ -111,7 +116,7 @@ public class NextcloudRetrofitServiceMethod<T> {
        // Build/parse dynamic parameters
        for(int i = 0; i < parameterAnnotationsArray.length; i++) {
            Annotation annotation = parameterAnnotationsArray[i][0];

            //TODO: add Part annotation
            if(annotation instanceof Query) {
                parameters.put(((Query)annotation).value(), String.valueOf(args[i]));
            } else if(annotation instanceof Body) {
@@ -185,6 +190,21 @@ public class NextcloudRetrofitServiceMethod<T> {
            parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
        } else if (annotation instanceof PUT) {
            parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
        } else if (annotation instanceof HEAD) {
            parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
        } else if (annotation instanceof HTTP) {
            HTTP http = (HTTP) annotation;
            parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
        } else if (annotation instanceof Multipart) {
            if (isFormEncoded) {
                throw methodError(method, "Only one encoding annotation is allowed.");
            }
            isMultipart = true;
        } else if (annotation instanceof FormUrlEncoded) {
            if (isMultipart) {
                throw methodError(method, "Only one encoding annotation is allowed.");
            }
            isFormEncoded = true;
        } else if (annotation instanceof Streaming) {
            Log.v(TAG, "streaming interface");
        } else if (annotation instanceof retrofit2.http.Headers) {
@@ -289,23 +309,4 @@ public class NextcloudRetrofitServiceMethod<T> {
                + "."
                + method.getName(), cause);
    }

    private static <T extends Serializable> T cloneSerializable(T o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject(o);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()) );
        T res  = null;
        try {
            res = (T) ois.readObject();
        } catch (ClassNotFoundException e) {
            // Can't happen as we just clone an object..
            Log.e(TAG, "ClassNotFoundException", e);
        }
        ois.close();

        return res;
    }
}