Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Create InMemoryFileSystem for testing purposes #373

Open
wants to merge 1 commit into
base: feature/rx2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ buildscript {
maven {
url = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
}

jcenter()

google()
jcenter()
}

rootProject.ext.versions = [
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, BufferedSource> 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<String> 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;
}
}
}