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 android.support.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@ -44,6 +45,18 @@ public final class DownloadAction {
|
|||||||
|
|
||||||
private static final int VERSION = 2;
|
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
|
* Deserializes one action that was serialized with {@link #serializeToStream(OutputStream)} from
|
||||||
* the {@code input}.
|
* the {@code input}.
|
||||||
@ -52,8 +65,8 @@ public final class DownloadAction {
|
|||||||
*
|
*
|
||||||
* @param input The stream from which to read.
|
* @param input The stream from which to read.
|
||||||
* @return The deserialized action.
|
* @return The deserialized action.
|
||||||
* @throws IOException If there is an IO error reading from {@code input}, or if the action type
|
* @throws IOException If there is an IO error reading from {@code input}, or if the data could
|
||||||
* isn't supported by any of the {@code deserializers}.
|
* not be deserialized.
|
||||||
*/
|
*/
|
||||||
public static DownloadAction deserializeFromStream(InputStream input) throws IOException {
|
public static DownloadAction deserializeFromStream(InputStream input) throws IOException {
|
||||||
return readFromStream(new DataInputStream(input));
|
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.Assertions;
|
||||||
import com.google.android.exoplayer2.util.Log;
|
import com.google.android.exoplayer2.util.Log;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.annotation.Documented;
|
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
|
* 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.
|
* 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");
|
Log.e(TAG, "Ignoring ADD action with no action data");
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
downloadManager.handleAction(actionData);
|
downloadManager.handleAction(DownloadAction.fromByteArray(actionData));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Failed to handle ADD action", 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_HLS;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_PROGRESSIVE;
|
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.TYPE_SS;
|
||||||
|
import static com.google.android.exoplayer2.offline.DownloadAction.fromByteArray;
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -142,15 +143,28 @@ public class DownloadActionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerWriteRead() throws Exception {
|
public void testStreamSerialization() throws Exception {
|
||||||
assertSerializationRoundTrip(
|
assertStreamSerializationRoundTrip(
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_DASH,
|
TYPE_DASH,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||||
"key123",
|
"key123",
|
||||||
data));
|
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));
|
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,7 +273,7 @@ public class DownloadActionTest {
|
|||||||
assertThat(action2).isEqualTo(action1);
|
assertThat(action2).isEqualTo(action1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertSerializationRoundTrip(DownloadAction action) throws IOException {
|
private static void assertStreamSerializationRoundTrip(DownloadAction action) throws IOException {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
DataOutputStream output = new DataOutputStream(out);
|
DataOutputStream output = new DataOutputStream(out);
|
||||||
action.serializeToStream(output);
|
action.serializeToStream(output);
|
||||||
@ -267,6 +281,10 @@ public class DownloadActionTest {
|
|||||||
assertEqual(action, deserializeActionFromStream(out));
|
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.
|
// TODO: Remove this method and add assets for legacy serialized actions.
|
||||||
private static void assertLegacySerializationRoundTrip(DownloadAction action, int version)
|
private static void assertLegacySerializationRoundTrip(DownloadAction action, int version)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user