mirror of
https://github.com/androidx/media.git
synced 2025-05-09 16:40:55 +08:00
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
This commit is contained in:
parent
5284ad2e41
commit
40c65dbcea
@ -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));
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user