com.nytimes.android.external.fs.FSWriter Maven / Gradle / Ivy
package com.nytimes.android.external.fs;
import com.nytimes.android.external.fs.filesystem.FileSystem;
import com.nytimes.android.external.store.base.DiskWrite;
import java.util.concurrent.Callable;
import javax.annotation.Nonnull;
import okio.BufferedSource;
import rx.Observable;
/**
* FSReader is used when persisting to file system
* PathResolver will be used in creating file system paths based on cache keys.
* Make sure to have keys containing same data resolve to same "path"
* @param key type
*/
public class FSWriter implements DiskWrite {
final FileSystem fileSystem;
final PathResolver pathResolver;
public FSWriter(FileSystem fileSystem, PathResolver pathResolver) {
this.fileSystem = fileSystem;
this.pathResolver = pathResolver;
}
@Nonnull
@Override
public Observable write(@Nonnull final T key, @Nonnull final BufferedSource data) {
return Observable.fromCallable(new Callable() {
@Nonnull
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public Boolean call() throws Exception {
fileSystem.write(pathResolver.resolve(key), data);
return true;
}
});
}
}