diff --git a/app/src/main/java/at/bitfire/davdroid/DavUtils.java b/app/src/main/java/at/bitfire/davdroid/DavUtils.java index 8664f467..a8d04bf0 100644 --- a/app/src/main/java/at/bitfire/davdroid/DavUtils.java +++ b/app/src/main/java/at/bitfire/davdroid/DavUtils.java @@ -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 segments = new LinkedList<>(HttpUrl.parse(url).pathSegments()); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java index 488ac52e..592d9b4b 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarSyncManager.java @@ -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 diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java index 06f2474c..4d055e9a 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncManager.java @@ -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) ); } diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncManager.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncManager.java index 518d365a..fc64748a 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncManager.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncManager.java @@ -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