From 7ad91fd748b9c86e5b44bb2cc9467ecacbff7d2e Mon Sep 17 00:00:00 2001 From: Roberto Orgiu Date: Thu, 20 Dec 2018 10:43:44 +0100 Subject: [PATCH] Create InMemoryFileSystem for testing purposes --- build.gradle | 4 +- .../fs3/filesystem/InMemoryFileSystem.java | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 filesystem/src/main/java/com/nytimes/android/external/fs3/filesystem/InMemoryFileSystem.java diff --git a/build.gradle b/build.gradle index e93762f3..434347d8 100644 --- a/build.gradle +++ b/build.gradle @@ -12,10 +12,8 @@ buildscript { maven { url = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/' } - - jcenter() - google() + jcenter() } rootProject.ext.versions = [ diff --git a/filesystem/src/main/java/com/nytimes/android/external/fs3/filesystem/InMemoryFileSystem.java b/filesystem/src/main/java/com/nytimes/android/external/fs3/filesystem/InMemoryFileSystem.java new file mode 100644 index 00000000..fb16f35c --- /dev/null +++ b/filesystem/src/main/java/com/nytimes/android/external/fs3/filesystem/InMemoryFileSystem.java @@ -0,0 +1,73 @@ +package com.nytimes.android.external.fs3.filesystem; + +import com.nytimes.android.external.store3.base.RecordState; +import okio.BufferedSource; + +import javax.annotation.Nonnull; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + + +/** + * This {@link FileSystem} has been created for testing purposes only, especially on Android, when you might need + * to avoid storing all the information. + * + * It is not suitable for production. + */ +@Deprecated +public class InMemoryFileSystem implements FileSystem { + + private Map fs = new HashMap<>(); + + @Nonnull + @Override + public BufferedSource read(String path) throws FileNotFoundException { + if (fs.containsKey(path)) { + return fs.get(path); + } else { + throw new FileNotFoundException("File " + path + " was not found in memory"); + } + } + + @Override + public void write(String path, BufferedSource source) throws IOException { + fs.put(path, source); + } + + @Override + public void delete(String path) throws IOException { + fs.remove(path); + } + + @Override + public void deleteAll(String path) throws IOException { + fs.clear(); + } + + @Nonnull + @Override + public Collection list(String path) throws FileNotFoundException { + if (fs.isEmpty()) { + throw new FileNotFoundException("Memory is empty"); + } + return fs.keySet(); + } + + @Override + public boolean exists(String file) { + return fs.containsKey(file); + } + + @Override + public RecordState getRecordState(@Nonnull TimeUnit expirationUnit, long expirationDuration, @Nonnull String path) { + if (fs.containsKey(path)) { + return RecordState.FRESH; + } else { + return RecordState.MISSING; + } + } +}