1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Contact/event/task upload: use streams directly without extra byte[] array

This commit is contained in:
Ricki Hirner 2016-05-23 15:06:21 +02:00
parent fd1f59d124
commit 3bde3758fc
4 changed files with 30 additions and 10 deletions

View File

@ -12,11 +12,18 @@ import android.support.annotation.NonNull;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import lombok.Cleanup;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
public class DavUtils {
@ -26,6 +33,21 @@ public class DavUtils {
return String.format("#%06X%02X", color, alpha);
}
public static RequestBody requestBody(final MediaType mediaType, @NonNull final ByteArrayOutputStream dataStream) throws IOException {
return new RequestBody() {
@Override
public MediaType contentType() {
return mediaType;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
@Cleanup OutputStream os = sink.outputStream();
dataStream.writeTo(os);
}
};
}
public static String lastSegmentOfUrl(@NonNull String url) {
// the list returned by HttpUrl.pathSegments() is unmodifiable, so we have to create a copy
List<String> segments = new LinkedList<>(HttpUrl.parse(url).pathSegments());

View File

@ -39,6 +39,7 @@ import at.bitfire.davdroid.AccountSettings;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.ArrayUtils;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.DavUtils;
import at.bitfire.davdroid.InvalidAccountException;
import at.bitfire.davdroid.R;
import at.bitfire.davdroid.resource.LocalCalendar;
@ -97,10 +98,7 @@ public class CalendarSyncManager extends SyncManager {
protected RequestBody prepareUpload(LocalResource resource) throws IOException, CalendarStorageException {
LocalEvent local = (LocalEvent)resource;
App.log.log(Level.FINE, "Preparing upload of event " + local.getFileName(), new Object[] { local.getEvent() });
return RequestBody.create(
DavCalendar.MIME_ICALENDAR,
local.getEvent().toStream().toByteArray()
);
return DavUtils.requestBody(DavCalendar.MIME_ICALENDAR, local.getEvent().toStream());
}
@Override

View File

@ -41,6 +41,7 @@ import at.bitfire.davdroid.AccountSettings;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.ArrayUtils;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.DavUtils;
import at.bitfire.davdroid.HttpClient;
import at.bitfire.davdroid.InvalidAccountException;
import at.bitfire.davdroid.R;
@ -131,9 +132,9 @@ public class ContactsSyncManager extends SyncManager {
protected RequestBody prepareUpload(LocalResource resource) throws IOException, ContactsStorageException {
LocalContact local = (LocalContact)resource;
App.log.log(Level.FINE, "Preparing upload of contact " + local.getFileName(), new Object[] { local.getContact() });
return RequestBody.create(
return DavUtils.requestBody(
hasVCard4 ? DavAddressBook.MIME_VCARD4 : DavAddressBook.MIME_VCARD3_UTF8,
local.getContact().toStream(hasVCard4 ? VCardVersion.V4_0 : VCardVersion.V3_0).toByteArray()
local.getContact().toStream(hasVCard4 ? VCardVersion.V4_0 : VCardVersion.V3_0)
);
}

View File

@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
@ -37,6 +38,7 @@ import at.bitfire.davdroid.AccountSettings;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.ArrayUtils;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.DavUtils;
import at.bitfire.davdroid.InvalidAccountException;
import at.bitfire.davdroid.R;
import at.bitfire.davdroid.resource.LocalResource;
@ -91,10 +93,7 @@ public class TasksSyncManager extends SyncManager {
protected RequestBody prepareUpload(LocalResource resource) throws IOException, CalendarStorageException {
LocalTask local = (LocalTask)resource;
App.log.log(Level.FINE, "Preparing upload of task " + local.getFileName(), new Object[] { local.getTask() });
return RequestBody.create(
DavCalendar.MIME_ICALENDAR,
local.getTask().toStream().toByteArray()
);
return DavUtils.requestBody(DavCalendar.MIME_ICALENDAR, local.getTask().toStream());
}
@Override