/*
* $Id: MemoryLoader.java,v 1.4 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;
import anvil.codec.School;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
/**
* class PathLoader
*
* @author: Jani Lehtim�ki
*/
public class MemoryLoader implements Loader, School
{
protected CompilerPreferences _prefs;
protected HashMap _images = new HashMap();
public MemoryLoader(CompilerPreferences prefs)
{
_prefs = prefs;
}
public void destroy()
{
_images.clear();
}
public OutputStream createClassRoom(String name)
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
_images.put(name, output);
return output;
}
public ClassData load(String name)
{
if (_images.size() == 0) {
return null;
}
ByteArrayOutputStream output = (ByteArrayOutputStream)_images.get(name);
if (output != null) {
_images.remove(name);
String directory = _prefs.getClassPath();
if (_prefs.getStoreImages() && directory != null) {
FileOutputStream fileoutput = null;
try {
File file = new File(directory, name + ".class");
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
fileoutput = new FileOutputStream(file);
output.writeTo(fileoutput);
} catch (IOException e) {
} finally {
if (fileoutput != null) {
try {
fileoutput.close();
} catch (IOException e) {
}
}
fileoutput = null;
}
}
byte[] bytes = output.toByteArray();
try {
output.close();
} catch (IOException e) {
}
return new ClassData(name, bytes);
}
return null;
}
}