package nl.nn.adapterframework.util;

import java.io.*;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.rmi.server.UID;
/**
 * Miscellanous conversion functions.
 * @version Id
 */
public class Misc {
	public static final String version = "$Id: Misc.java,v 1.6 2004-08-23 13:41:15 a1909356#db2admin Exp $";

	public static String createSimpleUUID_old() {
		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, via the java.rmi.server.UID class.
	*/
	public static String createSimpleUUID() {
		UID uid = new UID();

		// Replace semi colons by underscores, so IBIS will support it
		String uidString = uid.toString().replace(':', '_');
		return uidString;
	}
	/**
	* 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 {
		FileReader reader = new FileReader(fileName);
		try {
			return readerToString(reader, endOfLineString, xmlEncode);
		}
		finally {
			reader.close();
		}
	}

	public static String readerToString(Reader reader, String endOfLineString, boolean xmlEncode) throws IOException {
		StringBuffer sb = new StringBuffer();
		int curChar = -1;
		int prevChar = -1;
		while ((curChar = reader.read()) != -1) {
			if (prevChar == '\r' || curChar == '\n') {
				if (endOfLineString == null) {
					if (prevChar == '\r')
						sb.append((char) prevChar);
					if (curChar == '\n')
						sb.append((char) curChar);
				}
				else {
					sb.append(endOfLineString);
				}
			}
			if (curChar != '\r' && curChar != '\n') {
				sb.append((char) curChar);
			}
			prevChar = curChar;
		}
		return (xmlEncode) ? XmlUtils.encodeChars(sb.toString()) : sb.toString();
	}

	public static String streamToString(InputStream stream, String endOfLineString, boolean xmlEncode)
		throws IOException {
		return readerToString(new InputStreamReader(stream), endOfLineString, xmlEncode);
	}

	public static String resourceToString(URL resource, String endOfLineString, boolean xmlEncode) throws IOException {
		InputStream stream = resource.openStream();
		try {
			return streamToString(stream, endOfLineString, xmlEncode);
		}
		finally {
			stream.close();
		}
	}

	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 void stringToFile(String string, String fileName) throws IOException {
		FileWriter fw = new FileWriter(fileName);
		try {
			fw.write(string);
		}
		finally {
			fw.close();
		}
	}
}

@@ -304,17 +304,17 @@
 ava,v 1.
-6
+7
  2004-08
@@ -319,18 +319,18 @@
 08-2
-3 13:41:15
+4 05:54:58
  a19
@@ -2877,16 +2877,36 @@
 )) != -1
+ %7C%7C prevChar == '%5Cr'
 ) %7B%0A%09%09%09i
@@ -3200,24 +3200,41 @@
 Char != '%5Cn'
+ && curChar != -1
 ) %7B%0A%09%09%09%09sb.a

Correct filereading in case last character is \r