package foam.nanos.fs;

import foam.util.SafetyUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;

public abstract class AbstractStorage implements Storage {

  protected abstract FileSystem getFS();

  protected abstract Path getRootPath();

  protected abstract Path getPath(String name);

  protected String resourceDir_;

  protected AbstractStorage() {
    resourceDir_ = null;
  }

  protected AbstractStorage (String root) {
    resourceDir_ = root;
  }

  @Override
  public java.io.File get(String name) {
    return new java.io.File(resourceDir_, name).getAbsoluteFile();
  }

  @Override
  public byte[] getBytes(String name) {
    Path path = getPath(name);
    if ( path == null ) return null;

    try {
      return Files.readAllBytes(path);
    } catch (IOException e) {
      return null;
    }
  }

  @Override
  public OutputStream getOutputStream(String name) {
    Path path = getPath(name);
    if ( path == null ) return null;

    try {
      return Files.newOutputStream(path, CREATE, APPEND);
    } catch (IOException e) {
      return null;
    }
  }

  @Override
  public InputStream getInputStream(String name) {
    Path path = getPath(name);
    if ( path == null ) return null;

    try {
      return Files.newInputStream(path);
    } catch (IOException e) {
      return null;
    }
  }

  // TODO Return a list of files names as a List<String> instead of a DirectoryStream<Path>

  @Override
  public Set<String> getAvailableFiles(String name) {
    return getAvailableFiles(name, "");
  }

  @Override
  public Set<String> getAvailableFiles(String name, String glob) {
    Path path = getPath(name);
    Path root = getRootPath();
    if ( path == null || root == null) return null;

    Set<String> paths = new HashSet<>();

    try (DirectoryStream<Path> contents = java.nio.file.Files.newDirectoryStream(path, SafetyUtil.isEmpty(glob) ? "*" : glob)) {
      for ( Path p : contents ) {
        paths.add(root.relativize(p).toString());
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    return paths;
  }

}

@@ -1,8 +1,132 @@
+/**%0A * @license%0A * Copyright 2019 The FOAM Authors. All Rights Reserved.%0A * http://www.apache.org/licenses/LICENSE-2.0%0A */%0A%0A
 package 
@@ -170,17 +170,16 @@
 tyUtil;%0A
-%0A
 import j
@@ -429,17 +429,16 @@
 il.Set;%0A
-%0A
 import s
@@ -577,16 +577,18 @@
 tStorage
+%0A 
  impleme
@@ -598,17 +598,17 @@
  Storage
- 
+%0A
 %7B%0A%0A  pro

Add copyright header.