Remove ActionFile serialization, which is no longer used
PiperOrigin-RevId: 242465135
This commit is contained in:
parent
e7d717b96c
commit
9a03e73b7a
@ -19,7 +19,6 @@ import com.google.android.exoplayer2.offline.DownloadAction.UnsupportedActionExc
|
|||||||
import com.google.android.exoplayer2.util.AtomicFile;
|
import com.google.android.exoplayer2.util.AtomicFile;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -30,8 +29,7 @@ import java.util.ArrayList;
|
|||||||
*/
|
*/
|
||||||
public final class ActionFile {
|
public final class ActionFile {
|
||||||
|
|
||||||
private static final String TAG = "ActionFile";
|
private static final int VERSION = 0;
|
||||||
/* package */ static final int VERSION = 0;
|
|
||||||
|
|
||||||
private final AtomicFile atomicFile;
|
private final AtomicFile atomicFile;
|
||||||
|
|
||||||
@ -75,29 +73,6 @@ public final class ActionFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores {@link DownloadAction}s to file.
|
|
||||||
*
|
|
||||||
* @param downloadActions DownloadActions to store to file.
|
|
||||||
* @throws IOException If there is an error during storing.
|
|
||||||
*/
|
|
||||||
public void store(DownloadAction... downloadActions) throws IOException {
|
|
||||||
DataOutputStream output = null;
|
|
||||||
try {
|
|
||||||
output = new DataOutputStream(atomicFile.startWrite());
|
|
||||||
output.writeInt(VERSION);
|
|
||||||
output.writeInt(downloadActions.length);
|
|
||||||
for (DownloadAction action : downloadActions) {
|
|
||||||
action.serializeToStream(output);
|
|
||||||
}
|
|
||||||
atomicFile.endWrite(output);
|
|
||||||
// Avoid calling close twice.
|
|
||||||
output = null;
|
|
||||||
} finally {
|
|
||||||
Util.closeQuietly(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns whether the file or its backup exists. */
|
/** Returns whether the file or its backup exists. */
|
||||||
public boolean exists() {
|
public boolean exists() {
|
||||||
return atomicFile.exists();
|
return atomicFile.exists();
|
||||||
|
Binary file not shown.
Binary file not shown.
BIN
library/core/src/test/assets/offline/action_file_one_action.exi
Normal file
BIN
library/core/src/test/assets/offline/action_file_one_action.exi
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/offline/action_file_two_actions.exi
Normal file
BIN
library/core/src/test/assets/offline/action_file_two_actions.exi
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -22,7 +22,6 @@ import androidx.test.core.app.ApplicationProvider;
|
|||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -38,22 +37,16 @@ import org.junit.runner.RunWith;
|
|||||||
public class ActionFileTest {
|
public class ActionFileTest {
|
||||||
|
|
||||||
private File tempFile;
|
private File tempFile;
|
||||||
private DownloadAction action1;
|
private DownloadAction expectedAction1;
|
||||||
private DownloadAction action2;
|
private DownloadAction expectedAction2;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
tempFile = Util.createTempFile(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
tempFile = Util.createTempFile(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
||||||
action1 =
|
expectedAction1 =
|
||||||
buildAction(
|
buildExpectedAction(Uri.parse("http://test1.uri"), TestUtil.buildTestData(16));
|
||||||
DownloadAction.TYPE_PROGRESSIVE,
|
expectedAction2 =
|
||||||
Uri.parse("http://test1.uri"),
|
buildExpectedAction(Uri.parse("http://test2.uri"), TestUtil.buildTestData(32));
|
||||||
TestUtil.buildTestData(16));
|
|
||||||
action2 =
|
|
||||||
buildAction(
|
|
||||||
DownloadAction.TYPE_PROGRESSIVE,
|
|
||||||
Uri.parse("http://test2.uri"),
|
|
||||||
TestUtil.buildTestData(32));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -63,8 +56,9 @@ public class ActionFileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadNoDataThrowsIOException() throws Exception {
|
public void testLoadNoDataThrowsIOException() throws Exception {
|
||||||
|
ActionFile actionFile = getActionFile("offline/action_file_no_data.exi");
|
||||||
try {
|
try {
|
||||||
loadActions(new Object[] {});
|
actionFile.load();
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Expected exception.
|
// Expected exception.
|
||||||
@ -73,8 +67,9 @@ public class ActionFileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadIncompleteHeaderThrowsIOException() throws Exception {
|
public void testLoadIncompleteHeaderThrowsIOException() throws Exception {
|
||||||
|
ActionFile actionFile = getActionFile("offline/action_file_incomplete_header.exi");
|
||||||
try {
|
try {
|
||||||
loadActions(new Object[] {ActionFile.VERSION});
|
actionFile.load();
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Expected exception.
|
// Expected exception.
|
||||||
@ -82,95 +77,59 @@ public class ActionFileTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadCompleteHeaderZeroAction() throws Exception {
|
public void testLoadZeroActions() throws Exception {
|
||||||
DownloadAction[] actions = loadActions(new Object[] {ActionFile.VERSION, 0});
|
ActionFile actionFile = getActionFile("offline/action_file_zero_actions.exi");
|
||||||
|
DownloadAction[] actions = actionFile.load();
|
||||||
assertThat(actions).isNotNull();
|
assertThat(actions).isNotNull();
|
||||||
assertThat(actions).hasLength(0);
|
assertThat(actions).hasLength(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadAction() throws Exception {
|
public void testLoadOneAction() throws Exception {
|
||||||
DownloadAction[] actions =
|
ActionFile actionFile = getActionFile("offline/action_file_one_action.exi");
|
||||||
loadActions(
|
DownloadAction[] actions = actionFile.load();
|
||||||
new Object[] {
|
|
||||||
ActionFile.VERSION,
|
|
||||||
1, // Action count
|
|
||||||
action1
|
|
||||||
});
|
|
||||||
assertThat(actions).isNotNull();
|
|
||||||
assertThat(actions).hasLength(1);
|
assertThat(actions).hasLength(1);
|
||||||
assertThat(actions[0]).isEqualTo(action1);
|
assertThat(actions[0]).isEqualTo(expectedAction1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadActions() throws Exception {
|
public void testLoadTwoActions() throws Exception {
|
||||||
DownloadAction[] actions =
|
ActionFile actionFile = getActionFile("offline/action_file_two_actions.exi");
|
||||||
loadActions(
|
DownloadAction[] actions = actionFile.load();
|
||||||
new Object[] {
|
|
||||||
ActionFile.VERSION,
|
|
||||||
2, // Action count
|
|
||||||
action1,
|
|
||||||
action2,
|
|
||||||
});
|
|
||||||
assertThat(actions).isNotNull();
|
|
||||||
assertThat(actions).hasLength(2);
|
assertThat(actions).hasLength(2);
|
||||||
assertThat(actions[0]).isEqualTo(action1);
|
assertThat(actions[0]).isEqualTo(expectedAction1);
|
||||||
assertThat(actions[1]).isEqualTo(action2);
|
assertThat(actions[1]).isEqualTo(expectedAction2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadNotSupportedVersion() throws Exception {
|
public void testLoadUnsupportedVersion() throws Exception {
|
||||||
|
ActionFile actionFile = getActionFile("offline/action_file_unsupported_version.exi");
|
||||||
try {
|
try {
|
||||||
loadActions(
|
actionFile.load();
|
||||||
new Object[] {
|
|
||||||
ActionFile.VERSION + 1,
|
|
||||||
1, // Action count
|
|
||||||
action1,
|
|
||||||
});
|
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Expected exception.
|
// Expected exception.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private ActionFile getActionFile(String fileName) throws IOException {
|
||||||
public void testStoreAndLoadNoActions() throws Exception {
|
// Copy the test data from the asset to where the ActionFile expects it to be.
|
||||||
doTestSerializationRoundTrip();
|
byte[] actionFileBytes =
|
||||||
}
|
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), fileName);
|
||||||
|
try (FileOutputStream output = new FileOutputStream(tempFile)) {
|
||||||
@Test
|
output.write(actionFileBytes);
|
||||||
public void testStoreAndLoadActions() throws Exception {
|
|
||||||
doTestSerializationRoundTrip(action1, action2);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void doTestSerializationRoundTrip(DownloadAction... actions) throws IOException {
|
|
||||||
ActionFile actionFile = new ActionFile(tempFile);
|
|
||||||
actionFile.store(actions);
|
|
||||||
assertThat(actionFile.load()).isEqualTo(actions);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Remove this method and add assets for invalid and legacy serialized action files.
|
|
||||||
private DownloadAction[] loadActions(Object[] values) throws IOException {
|
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
|
|
||||||
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
|
|
||||||
try {
|
|
||||||
for (Object value : values) {
|
|
||||||
if (value instanceof Integer) {
|
|
||||||
dataOutputStream.writeInt((Integer) value);
|
|
||||||
} else if (value instanceof DownloadAction) {
|
|
||||||
((DownloadAction) value).serializeToStream(dataOutputStream);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
dataOutputStream.close();
|
|
||||||
}
|
}
|
||||||
return new ActionFile(tempFile).load();
|
// Load the action file.
|
||||||
|
return new ActionFile(tempFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DownloadAction buildAction(String type, Uri uri, byte[] data) {
|
private static DownloadAction buildExpectedAction(Uri uri, byte[] data) {
|
||||||
return DownloadAction.createDownloadAction(
|
return DownloadAction.createDownloadAction(
|
||||||
"id", type, uri, /* keys= */ Collections.emptyList(), /* customCacheKey= */ null, data);
|
/* id= */ uri.toString(),
|
||||||
|
DownloadAction.TYPE_PROGRESSIVE,
|
||||||
|
uri,
|
||||||
|
/* keys= */ Collections.emptyList(),
|
||||||
|
/* customCacheKey= */ null,
|
||||||
|
data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,10 @@ import android.net.Uri;
|
|||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
|
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
|
||||||
|
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -110,39 +112,45 @@ public class DownloadIndexUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void upgradeActionFile_createsDownloadStates() throws IOException {
|
public void upgradeActionFile_createsDownloadStates() throws IOException {
|
||||||
ActionFile actionFile = new ActionFile(tempFile);
|
// Copy the test asset to a file.
|
||||||
StreamKey streamKey1 =
|
byte[] actionFileBytes =
|
||||||
|
TestUtil.getByteArray(
|
||||||
|
ApplicationProvider.getApplicationContext(),
|
||||||
|
"offline/action_file_for_download_index_upgrade.exi");
|
||||||
|
try (FileOutputStream output = new FileOutputStream(tempFile)) {
|
||||||
|
output.write(actionFileBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamKey expectedStreamKey1 =
|
||||||
new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5);
|
new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5);
|
||||||
StreamKey streamKey2 =
|
StreamKey expectedStreamKey2 =
|
||||||
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
|
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
|
||||||
DownloadAction action1 =
|
DownloadAction expectedAction1 =
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
"id1",
|
"key123",
|
||||||
TYPE_DASH,
|
TYPE_DASH,
|
||||||
Uri.parse("https://www.test.com/download1"),
|
Uri.parse("https://www.test.com/download1"),
|
||||||
asList(streamKey1),
|
asList(expectedStreamKey1),
|
||||||
/* customCacheKey= */ "key123",
|
/* customCacheKey= */ "key123",
|
||||||
new byte[] {1, 2, 3, 4});
|
new byte[] {1, 2, 3, 4});
|
||||||
DownloadAction action2 =
|
DownloadAction expectedAction2 =
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
"id2",
|
"key234",
|
||||||
TYPE_DASH,
|
TYPE_DASH,
|
||||||
Uri.parse("https://www.test.com/download2"),
|
Uri.parse("https://www.test.com/download2"),
|
||||||
asList(streamKey2),
|
asList(expectedStreamKey2),
|
||||||
/* customCacheKey= */ "key234",
|
/* customCacheKey= */ "key234",
|
||||||
new byte[] {5, 4, 3, 2, 1});
|
new byte[] {5, 4, 3, 2, 1});
|
||||||
actionFile.store(action1, action2);
|
|
||||||
|
|
||||||
|
ActionFile actionFile = new ActionFile(tempFile);
|
||||||
DownloadIndexUtil.upgradeActionFile(actionFile, downloadIndex, /* downloadIdProvider= */ null);
|
DownloadIndexUtil.upgradeActionFile(actionFile, downloadIndex, /* downloadIdProvider= */ null);
|
||||||
|
assertDownloadIndexContainsAction(expectedAction1, DownloadState.STATE_QUEUED);
|
||||||
assertDownloadIndexContainsAction(action1, DownloadState.STATE_QUEUED);
|
assertDownloadIndexContainsAction(expectedAction2, DownloadState.STATE_QUEUED);
|
||||||
assertDownloadIndexContainsAction(action2, DownloadState.STATE_QUEUED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertDownloadIndexContainsAction(DownloadAction action, int state)
|
private void assertDownloadIndexContainsAction(DownloadAction action, int state)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
DownloadState downloadState = downloadIndex.getDownloadState(action.id);
|
DownloadState downloadState = downloadIndex.getDownloadState(action.id);
|
||||||
assertThat(downloadState).isNotNull();
|
|
||||||
assertThat(downloadState.type).isEqualTo(action.type);
|
assertThat(downloadState.type).isEqualTo(action.type);
|
||||||
assertThat(downloadState.cacheKey).isEqualTo(action.customCacheKey);
|
assertThat(downloadState.cacheKey).isEqualTo(action.customCacheKey);
|
||||||
assertThat(downloadState.customMetadata).isEqualTo(action.data);
|
assertThat(downloadState.customMetadata).isEqualTo(action.data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user