public synchronized void flushToDisk() {
        BufferedOutputStream outstream = null;
        try {
            outstream = new BufferedOutputStream(new FileOutputStream(userdataFile));
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Unable to create an outputstream for userdata.xml");
            return;
        }

        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setIndent("     "));
        try {
            outputter.output(document, outstream);
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Unable to output to userdata.xml");
        }

        try {
            outstream.flush();
            outstream.close();

            // Make sure that userdataFile has the right permissions ( not for Windows )
            Process p = Runtime.getRuntime().exec("chown " + plUser + " " + userdataFile);
            try {
                p.waitFor();
            } catch (Exception ex) {

            }
            if ( p != null )
                NativeCalls.releaseProcess(p);


        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Unable to close userdata.xml after writing");
        }
    }