From 40c65dbcea913447cf97a6fe900b291547c02de1 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 26 Nov 2018 11:48:11 +0000 Subject: [PATCH] Move deserialization code to a more natural location - Add DownloadAction.fromByteArray for symmetry with toByteArray - Make DownloadService call fromByteArray, for symmetry with calls to toByteArray PiperOrigin-RevId: 222801703 --- .../exoplayer2/offline/DownloadAction.java | 17 ++++++++++-- .../exoplayer2/offline/DownloadManager.java | 16 ------------ .../exoplayer2/offline/DownloadService.java | 2 +- .../offline/DownloadActionTest.java | 26 ++++++++++++++++--- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java index 46f5c78e55..ddbb6cdea9 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java @@ -19,6 +19,7 @@ import android.net.Uri; import android.support.annotation.Nullable; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -44,6 +45,18 @@ public final class DownloadAction { private static final int VERSION = 2; + /** + * Deserializes an action from the {@code data}. + * + * @param data The action data to deserialize. + * @return The deserialized action. + * @throws IOException If the data could not be deserialized. + */ + public static DownloadAction fromByteArray(byte[] data) throws IOException { + ByteArrayInputStream input = new ByteArrayInputStream(data); + return deserializeFromStream(input); + } + /** * Deserializes one action that was serialized with {@link #serializeToStream(OutputStream)} from * the {@code input}. @@ -52,8 +65,8 @@ public final class DownloadAction { * * @param input The stream from which to read. * @return The deserialized action. - * @throws IOException If there is an IO error reading from {@code input}, or if the action type - * isn't supported by any of the {@code deserializers}. + * @throws IOException If there is an IO error reading from {@code input}, or if the data could + * not be deserialized. */ public static DownloadAction deserializeFromStream(InputStream input) throws IOException { return readFromStream(new DataInputStream(input)); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 9437a410fc..4208521cc6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -31,7 +31,6 @@ import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.lang.annotation.Documented; @@ -187,21 +186,6 @@ public final class DownloadManager { } } - /** - * Deserializes an action from {@code actionData}, and calls {@link - * #handleAction(DownloadAction)}. - * - * @param actionData Serialized version of the action to be executed. - * @return The id of the newly created task. - * @throws IOException If an error occurs deserializing the action. - */ - public int handleAction(byte[] actionData) throws IOException { - Assertions.checkState(!released); - ByteArrayInputStream input = new ByteArrayInputStream(actionData); - DownloadAction action = DownloadAction.deserializeFromStream(input); - return handleAction(action); - } - /** * Handles the given action. A task is created and added to the task queue. If it's a remove * action then any download tasks for the same media are immediately canceled. diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java index e9492f6b87..cfca8ede79 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java @@ -259,7 +259,7 @@ public abstract class DownloadService extends Service { Log.e(TAG, "Ignoring ADD action with no action data"); } else { try { - downloadManager.handleAction(actionData); + downloadManager.handleAction(DownloadAction.fromByteArray(actionData)); } catch (IOException e) { Log.e(TAG, "Failed to handle ADD action", e); } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java index e2d4287d6b..dc34d7e1a3 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java @@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH; import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_HLS; import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_PROGRESSIVE; import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_SS; +import static com.google.android.exoplayer2.offline.DownloadAction.fromByteArray; import static com.google.common.truth.Truth.assertThat; import android.net.Uri; @@ -142,15 +143,28 @@ public class DownloadActionTest { } @Test - public void testSerializerWriteRead() throws Exception { - assertSerializationRoundTrip( + public void testStreamSerialization() throws Exception { + assertStreamSerializationRoundTrip( DownloadAction.createDownloadAction( TYPE_DASH, uri1, toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), "key123", data)); - assertSerializationRoundTrip( + assertStreamSerializationRoundTrip( + DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data)); + } + + @Test + public void testArraySerialization() throws Exception { + assertArraySerializationRoundTrip( + DownloadAction.createDownloadAction( + TYPE_DASH, + uri1, + toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), + "key123", + data)); + assertArraySerializationRoundTrip( DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data)); } @@ -259,7 +273,7 @@ public class DownloadActionTest { assertThat(action2).isEqualTo(action1); } - private static void assertSerializationRoundTrip(DownloadAction action) throws IOException { + private static void assertStreamSerializationRoundTrip(DownloadAction action) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(out); action.serializeToStream(output); @@ -267,6 +281,10 @@ public class DownloadActionTest { assertEqual(action, deserializeActionFromStream(out)); } + private static void assertArraySerializationRoundTrip(DownloadAction action) throws IOException { + assertEqual(action, fromByteArray(action.toByteArray())); + } + // TODO: Remove this method and add assets for legacy serialized actions. private static void assertLegacySerializationRoundTrip(DownloadAction action, int version) throws IOException {