diff src/goodjava/webserver/Util.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/webserver/Util.java@643cf1c37723
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/webserver/Util.java	Tue Sep 17 01:35:01 2019 -0400
@@ -0,0 +1,55 @@
+package goodjava.webserver;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+
+
+final class Util {
+
+	static String urlEncode(String s) {
+		try {
+			return URLEncoder.encode(s,"UTF-8");
+		} catch(UnsupportedEncodingException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	static void add(Map<String,Object> map,String name,Object value) {
+		Object current = map.get(name);
+		if( current == null ) {
+			map.put(name,value);
+		} else if( current instanceof List ) {
+			List list = (List)current;
+			list.add(value);
+		} else {
+			List list = new ArrayList();
+			list.add(current);
+			list.add(value);
+			map.put(name,list);
+		}
+	}
+
+	static String toString(byte[] a,String charset) throws UnsupportedEncodingException {
+		if( charset != null )
+			return new String(a,charset);
+		char[] ac = new char[a.length];
+		for( int i=0; i<a.length; i++ ) {
+			ac[i] = (char)a[i];
+		}
+		return new String(ac);
+	}
+
+	static byte[] toBytes(String s) {
+		char[] ac = s.toCharArray();
+		byte[] a = new byte[ac.length];
+		for( int i=0; i<ac.length; i++ ) {
+			a[i] = (byte)ac[i];
+		}
+		return a;
+	}
+
+	private Util() {}  // never
+}