Correctly handling Exception: java.nio.file.FileSystemException: No space left on device.

By default methods File.makeDir() and File.makeDirs() can return 'false' if file aleady exists or can not be created. Such silent ignore of the situation propagates misbehavior to the caller: CacheDataSink#173 : new FileOutputStream(file). And then it throws not correct exception type 'FileNotFoundException'. While correct exception should be 'no space left on the device'.

This can be fixed only with 'Files.createDirectories()' method that throws correct exception type.
parent e139a465
......@@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.upstream.cache;
import android.os.Build;
import android.os.ConditionVariable;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
......@@ -26,6 +27,7 @@ import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -410,14 +412,30 @@ public final class SimpleCache implements Cache {
Assertions.checkState(cachedContent.isFullyLocked(position, length));
if (!cacheDir.exists()) {
// For some reason the cache directory doesn't exist. Make a best effort to create it.
cacheDir.mkdirs();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
Files.createDirectories(cacheDir.toPath());
} catch (IOException e) {
throw new CacheException(e);
}
} else {
cacheDir.mkdirs();
}
removeStaleSpans();
}
evictor.onStartFile(this, key, position, length);
// Randomly distribute files into subdirectories with a uniform distribution.
File fileDir = new File(cacheDir, Integer.toString(random.nextInt(SUBDIRECTORY_COUNT)));
if (!fileDir.exists()) {
fileDir.mkdir();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
Files.createDirectories(fileDir.toPath());
} catch (IOException e) {
throw new CacheException(e);
}
} else {
fileDir.mkdir();
}
}
long lastTouchTimestamp = System.currentTimeMillis();
return SimpleCacheSpan.getCacheFile(fileDir, cachedContent.id, position, lastTouchTimestamp);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment