
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.io.*;
import java.util.*;

/******************************************************************************
* An applet useful for interacting with the Java applet persistence store.
* It allows you to set and get keyed strings in the applet persistence memory
* using JavaScript.
* This Applet doesn't display anything, so usually you want to hide it by
* specifying a height and width of "1".
* @author Edward A. Pier eap@milyway.gsfc.nasa.gov
* @version 1.0 2004-08-25
******************************************************************************/
public class Sniff extends JApplet{

String key;

/***************************************************************************
* Called after the applet loads. All it does is set the applet background 
* to white to better hide the applet.
***************************************************************************/
public void init() {

    getContentPane().setBackground(Color.white);

} // end of init method


/*****************************************************************************
* Set a key-value pair
*****************************************************************************/
public void set(String key, String value) {

    if(key==null) return;

    try {
        /***********************************
        * write the string to a data array *
        ***********************************/
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(data);
        out.writeObject(value);
        out.close();

        /*************************************************
        * deliver the byte array to the persistent store *
        *************************************************/
        AppletContext context = getAppletContext();
        InputStream in = new ByteArrayInputStream(data.toByteArray());
        context.setStream(key, in);

    } catch(Throwable e) {}


} // end of set method


/*****************************************************************************
* Fetch the value corresponding to a key. Returns null if there is no value
* for this key or if the value can't be accessed.
*****************************************************************************/
public String get(String key) {

    AppletContext context = getAppletContext();
    InputStream in =context.getStream(key);
    if(in == null) return null;

    try {
        ObjectInputStream objects = new ObjectInputStream(in);
        Object thing = objects.readObject();
        return thing.toString();
    } catch(Throwable e) {
        return null;
    }

} // end of get method

/****************************************************************************
* Delete a value from the store. Does nothing if the key was not previously set,
* or if the store can't be accessed.
****************************************************************************/
public void erase(String key) { 

    AppletContext context = getAppletContext();
    try { context.setStream(key,null); }
    catch(Throwable e) {}
}

/****************************************************************************
* not allowed for unsigned applets, so there's not much point in having this.
****************************************************************************/
// public String[] keys() {
// 
//     AppletContext context = getAppletContext();
// 
//     /****************************
//     * load the keys into a list *
//     ****************************/
//     java.util.List list = new ArrayList();
//     for(Iterator it = context.getStreamKeys(); it.hasNext(); ) {
//         list.add(it.next());
//     }
// 
//     /*********************************
//     * convert the list into an array *
//     *********************************/
//     String[] array = new String[list.size()];
//     for(int i=0; i< array.length; ++i) {
//         array[i] = (String)list.get(i);
//     }
// 
//     return array;
// 
// } // end of keys method


/****************************************************************************
* Just returns the string "Sniff". For some but not all browsers this
* can be used to check if the applet has loaded.
****************************************************************************/
public String toString() { return "Sniff"; }

} // end of Sniff class
