Search in sources :

Example 1 with ReusableBufferedOutputStream

use of com.google.android.exoplayer2.util.ReusableBufferedOutputStream in project ExoPlayer by google.

the class CachedContentIndex method writeFile.

private void writeFile() throws CacheException {
    DataOutputStream output = null;
    try {
        OutputStream outputStream = atomicFile.startWrite();
        if (bufferedOutputStream == null) {
            bufferedOutputStream = new ReusableBufferedOutputStream(outputStream);
        } else {
            bufferedOutputStream.reset(outputStream);
        }
        output = new DataOutputStream(bufferedOutputStream);
        output.writeInt(VERSION);
        int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
        output.writeInt(flags);
        if (cipher != null) {
            byte[] initializationVector = new byte[16];
            new Random().nextBytes(initializationVector);
            output.write(initializationVector);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
            try {
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
                // Should never happen.
                throw new IllegalStateException(e);
            }
            output.flush();
            output = new DataOutputStream(new CipherOutputStream(bufferedOutputStream, cipher));
        }
        output.writeInt(keyToContent.size());
        int hashCode = 0;
        for (CachedContent cachedContent : keyToContent.values()) {
            cachedContent.writeToStream(output);
            hashCode += cachedContent.headerHashCode();
        }
        output.writeInt(hashCode);
        atomicFile.endWrite(output);
        // Avoid calling close twice. Duplicate CipherOutputStream.close calls did
        // not used to be no-ops: https://android-review.googlesource.com/#/c/272799/
        output = null;
    } catch (IOException e) {
        throw new CacheException(e);
    } finally {
        Util.closeQuietly(output);
    }
}
Also used : ReusableBufferedOutputStream(com.google.android.exoplayer2.util.ReusableBufferedOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CacheException(com.google.android.exoplayer2.upstream.cache.Cache.CacheException) DataOutputStream(java.io.DataOutputStream) ReusableBufferedOutputStream(com.google.android.exoplayer2.util.ReusableBufferedOutputStream) DataOutputStream(java.io.DataOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Random(java.util.Random) IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 2 with ReusableBufferedOutputStream

use of com.google.android.exoplayer2.util.ReusableBufferedOutputStream in project ExoPlayer by google.

the class CacheDataSink method openNextOutputStream.

private void openNextOutputStream() throws IOException {
    long maxLength = dataSpec.length == C.LENGTH_UNSET ? maxCacheFileSize : Math.min(dataSpec.length - dataSpecBytesWritten, maxCacheFileSize);
    file = cache.startFile(dataSpec.key, dataSpec.absoluteStreamPosition + dataSpecBytesWritten, maxLength);
    underlyingFileOutputStream = new FileOutputStream(file);
    if (bufferSize > 0) {
        if (bufferedOutputStream == null) {
            bufferedOutputStream = new ReusableBufferedOutputStream(underlyingFileOutputStream, bufferSize);
        } else {
            bufferedOutputStream.reset(underlyingFileOutputStream);
        }
        outputStream = bufferedOutputStream;
    } else {
        outputStream = underlyingFileOutputStream;
    }
    outputStreamBytesWritten = 0;
}
Also used : ReusableBufferedOutputStream(com.google.android.exoplayer2.util.ReusableBufferedOutputStream) FileOutputStream(java.io.FileOutputStream)

Aggregations

ReusableBufferedOutputStream (com.google.android.exoplayer2.util.ReusableBufferedOutputStream)2 CacheException (com.google.android.exoplayer2.upstream.cache.Cache.CacheException)1 DataOutputStream (java.io.DataOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 Random (java.util.Random)1 CipherOutputStream (javax.crypto.CipherOutputStream)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1