package nl.nn.adapterframework.util;

import java.io.*;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
/**
 * Miscellanous conversion functions.
 * @version Id
 */
public class Misc {
	public static final String version="$Id: Misc.java,v 1.3 2004-03-26 10:42:43 NNVZNL01#L180564 Exp $";
	
	public static String createSimpleUUID() {
		StringBuffer sb=new StringBuffer();
		sb.append(System.currentTimeMillis());
		sb.append('-');
		sb.append(Math.round(Math.random()*1000000));
		return sb.toString();
	}
	 /**
     * Creates a Universally Unique Identifier.
     *
     * Similar to javax.mail.internet.UniqueValue, this implementation
     * generates a unique value by random number, the current
     * time (in milliseconds), and this system's hostname generated by
     * <code>InternetAddress.getLocalAddress()</code>.
     * @return A unique identifier is returned.
     */
    static public String createUUID ()
    { 
        String user = System.getProperty("user.name");
        InetAddress inetAddress=null;
        String ipAddress = null;
        
        try {
            inetAddress = InetAddress.getLocalHost();
            ipAddress = inetAddress.getHostAddress();
        } 
        
        catch (UnknownHostException uhe) {
            ipAddress = "127.0.0.1";
        }
        

        StringBuffer s = new StringBuffer();

        //Unique string is <ipaddress>.<currentTime>.<username>.<hashcode>
        s.append(ipAddress).append('.').append(System.currentTimeMillis()).append('.').
                                        append(user).append('.').append(Math.round(Math.random()*1000000));
        
        return s.toString();
    }
/**
 * Please consider using resourceToString() instead of relying on files.
 */
public static String fileToString(String fileName) throws IOException {
    return fileToString(fileName, null, false);
}
/**
 * Please consider using resourceToString() instead of relying on files.
 */
public static String fileToString(String fileName, String endOfLineString)
    throws IOException {
    return fileToString(fileName, endOfLineString, false);
}
/**
  * Please consider using resourceToString() instead of relying on files.
 */
public static String fileToString(
    String fileName,
    String endOfLineString,
    boolean xmlEncode)
    throws IOException {

	return readerToString(new FileReader(fileName), endOfLineString, xmlEncode);
}
public static String readerToString(
    Reader reader, 
    String endOfLineString,
    boolean xmlEncode)
    throws IOException {

    BufferedReader in = new BufferedReader(reader);
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {

        if (line.length()==0 || line.getBytes()[0] != 26) { // skip end of file (ctrl-z)
            if (xmlEncode) {
                sb.append(XmlUtils.encodeChars(line));
            } else {
                sb.append(line);
            }
            if (endOfLineString != null)
                sb.append(endOfLineString);
        }
    }
    in.close();
    return sb.toString();

}
public static String resourceToString(URL resource) throws IOException {
    return resourceToString(resource, null, false);
}
public static String resourceToString(URL resource, String endOfLineString)
    throws IOException {
    return resourceToString(resource, endOfLineString, false);
}
public static String resourceToString(
    URL resource, 
    String endOfLineString,
    boolean xmlEncode)
    throws IOException {

	return readerToString(new InputStreamReader(resource.openStream()), endOfLineString, xmlEncode);
}
	public static void stringToFile(String string, String fileName)
       throws IOException
    {
       FileWriter fw = new FileWriter(fileName);
       fw.write(string); 
       fw.close();
    }
}

@@ -137,16 +137,44 @@
 eption;%0A
+import java.rmi.server.UID;%0A
 /**%0A * M
@@ -306,46 +306,47 @@
 v 1.
-3
+4
  2004-0
-3-26 10:42:43 NNVZNL01#L180564
+7-20 13:12:00 a1909356#db2admin
  Exp
@@ -390,16 +390,20 @@
 mpleUUID
+_old
 () %7B%0A%09%09S
@@ -570,16 +570,316 @@
 ng();%0A%09%7D
+%0A%0A%09/**%0A%09* Creates a Universally Unique Identifier, via the java.rmi.server.UID class.%0A%09*/%0A%09%09public static String createSimpleUUID() %7B%0A%09%09UID uid = new UID();%0A%0A%09%09// Replace semi colons by underscores, so IBIS will support it%0A%09%09String uidString = uid.toString().replace(':', '_');%0A%09%09return uidString;%0A%09%7D
 %0A%09 /**%0A 
@@ -2031,16 +2031,32 @@
 ;%0A    %7D%0A
+    %0A%0A    %0A    %0A
 /**%0A * P

Use java.rmi.server.UID for unique ID