mirror of
https://github.com/androidx/media.git
synced 2025-05-06 07:00:19 +08:00
Use ReusableBufferedOutputStream for cache index file write operation
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=139889957
This commit is contained in:
parent
42fadfe083
commit
e84fa5835d
@ -20,9 +20,9 @@ import com.google.android.exoplayer2.C;
|
|||||||
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
|
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import com.google.android.exoplayer2.util.AtomicFile;
|
import com.google.android.exoplayer2.util.AtomicFile;
|
||||||
|
import com.google.android.exoplayer2.util.ReusableBufferedOutputStream;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -61,6 +61,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
private final Cipher cipher;
|
private final Cipher cipher;
|
||||||
private final SecretKeySpec secretKeySpec;
|
private final SecretKeySpec secretKeySpec;
|
||||||
private boolean changed;
|
private boolean changed;
|
||||||
|
private ReusableBufferedOutputStream bufferedOutputStream;
|
||||||
|
|
||||||
/** Creates a CachedContentIndex which works on the index file in the given cacheDir. */
|
/** Creates a CachedContentIndex which works on the index file in the given cacheDir. */
|
||||||
public CachedContentIndex(File cacheDir) {
|
public CachedContentIndex(File cacheDir) {
|
||||||
@ -256,8 +257,13 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
private void writeFile() throws CacheException {
|
private void writeFile() throws CacheException {
|
||||||
DataOutputStream output = null;
|
DataOutputStream output = null;
|
||||||
try {
|
try {
|
||||||
OutputStream outputStream = new BufferedOutputStream(atomicFile.startWrite());
|
OutputStream outputStream = atomicFile.startWrite();
|
||||||
output = new DataOutputStream(outputStream);
|
if (bufferedOutputStream == null) {
|
||||||
|
bufferedOutputStream = new ReusableBufferedOutputStream(outputStream);
|
||||||
|
} else {
|
||||||
|
bufferedOutputStream.reset(outputStream);
|
||||||
|
}
|
||||||
|
output = new DataOutputStream(bufferedOutputStream);
|
||||||
output.writeInt(VERSION);
|
output.writeInt(VERSION);
|
||||||
|
|
||||||
int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
|
int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
|
||||||
@ -274,7 +280,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
throw new IllegalStateException(e); // Should never happen.
|
throw new IllegalStateException(e); // Should never happen.
|
||||||
}
|
}
|
||||||
output.flush();
|
output.flush();
|
||||||
output = new DataOutputStream(new CipherOutputStream(outputStream, cipher));
|
output = new DataOutputStream(new CipherOutputStream(bufferedOutputStream, cipher));
|
||||||
}
|
}
|
||||||
|
|
||||||
output.writeInt(keyToContent.size());
|
output.writeInt(keyToContent.size());
|
||||||
|
@ -25,6 +25,8 @@ import java.io.OutputStream;
|
|||||||
*/
|
*/
|
||||||
public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
||||||
|
|
||||||
|
private boolean closed;
|
||||||
|
|
||||||
public ReusableBufferedOutputStream(OutputStream out) {
|
public ReusableBufferedOutputStream(OutputStream out) {
|
||||||
super(out);
|
super(out);
|
||||||
}
|
}
|
||||||
@ -35,13 +37,14 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
closed = true;
|
||||||
|
|
||||||
Throwable thrown = null;
|
Throwable thrown = null;
|
||||||
try {
|
try {
|
||||||
flush();
|
flush();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
thrown = e;
|
thrown = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
out.close();
|
out.close();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
@ -49,8 +52,6 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
|||||||
thrown = e;
|
thrown = e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out = null;
|
|
||||||
|
|
||||||
if (thrown != null) {
|
if (thrown != null) {
|
||||||
Util.sneakyThrow(thrown);
|
Util.sneakyThrow(thrown);
|
||||||
}
|
}
|
||||||
@ -64,7 +65,8 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
|
|||||||
* @throws IllegalStateException If the stream isn't closed.
|
* @throws IllegalStateException If the stream isn't closed.
|
||||||
*/
|
*/
|
||||||
public void reset(OutputStream out) {
|
public void reset(OutputStream out) {
|
||||||
Assertions.checkState(this.out == null);
|
Assertions.checkState(closed);
|
||||||
this.out = out;
|
this.out = out;
|
||||||
|
closed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user