package org.jboss.aerogear.unifiedpush.service.impl.file;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import javax.ejb.Stateless;

import org.jboss.aerogear.unifiedpush.service.file.FileManager;

/**
 * A basic, non-synchronous and blocking implementation of FileManager.
 */
@Stateless
public class FileManagerImpl implements FileManager {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void save(Path path, byte[] data) {
		try {
			File directory = path.getParent().toFile();
			if (!directory.exists()) {
				directory.mkdirs();
			}
			Files.write(path, data);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public byte[] read(Path path) {
		try {
			return Files.readAllBytes(path);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<File> list(Path path, FileFilter filter) {
		File directory = path.toFile();
		if (!directory.exists()) {
			throw new RuntimeException(path + " does not exist");
		}
		
		if (!directory.isDirectory()) {
			throw new RuntimeException(path + " is not a directory");
		}
		
		return Arrays.asList(directory.listFiles(filter));
	}

}
@@ -644,16 +644,34 @@
 ) %7B%0A%09%09%09%09
+boolean created = 
 director
@@ -682,16 +682,140 @@
 dirs();%0A
+%09%09%09%09if (!created) %7B%0A%09%09%09%09%09throw new RuntimeException(%22could not create new directory %22 + directory.getAbsolutePath());%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09

Throw exception if unable to create directory while saving