net.sf.gluebooster.java.booster.basic.resources.MemoryResourceSystem Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.resources;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import net.sf.gluebooster.java.booster.essentials.container.ResourceSystem;
import net.sf.gluebooster.java.booster.essentials.utils.Check;
/**
* Only in memory storage, no storage on external systems like file systems.
*
* @author CBauer
*
*/
public class MemoryResourceSystem implements ResourceSystem{
/**
* Stores the resources in ByteArrayOutputStream.
*/
private Map storage = new HashMap();
public Map getStorage() {
return storage;
}
public void setStorage(Map storage) {
this.storage = storage;
}
/**
* Supports every id.
*/
@Override
public boolean isResourceIDSupported(Object resourceID) {
return true;
}
@Override
public OutputStream getOutputStream(Object resourceID) throws Exception {
ByteArrayOutputStream result = new ByteArrayOutputStream();
storage.put(resourceID.toString(), result);
return result;
}
@Override
public InputStream getInputStream(Object resourceID) throws Exception {
return new ByteArrayInputStream(storage.get(resourceID.toString()).toByteArray());
}
@Override
public URL getUrl(Object resourceID)throws Exception{
throw new UnsupportedOperationException();
}
@Override
public boolean isWritable() {
return true;
}
@Override
public Object createDirectory(Object parentResourceId, Object newResourceId)
throws Exception {
Check.notNull(newResourceId, "newResourceId");
String result = null;
if (parentResourceId != null)
result = parentResourceId.toString();
result += newResourceId;
return result;
}
@Override
public String toString() {
return "This MemoryResourceSystem contains the following files: " + storage.keySet();
}
}