comparison 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
comparison
equal deleted inserted replaced
1401:ef1620aa99cb 1402:27efb1fcbcb5
1 package goodjava.webserver;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.Map;
6 import java.util.List;
7 import java.util.ArrayList;
8
9
10 final class Util {
11
12 static String urlEncode(String s) {
13 try {
14 return URLEncoder.encode(s,"UTF-8");
15 } catch(UnsupportedEncodingException e) {
16 throw new RuntimeException(e);
17 }
18 }
19
20 static void add(Map<String,Object> map,String name,Object value) {
21 Object current = map.get(name);
22 if( current == null ) {
23 map.put(name,value);
24 } else if( current instanceof List ) {
25 List list = (List)current;
26 list.add(value);
27 } else {
28 List list = new ArrayList();
29 list.add(current);
30 list.add(value);
31 map.put(name,list);
32 }
33 }
34
35 static String toString(byte[] a,String charset) throws UnsupportedEncodingException {
36 if( charset != null )
37 return new String(a,charset);
38 char[] ac = new char[a.length];
39 for( int i=0; i<a.length; i++ ) {
40 ac[i] = (char)a[i];
41 }
42 return new String(ac);
43 }
44
45 static byte[] toBytes(String s) {
46 char[] ac = s.toCharArray();
47 byte[] a = new byte[ac.length];
48 for( int i=0; i<ac.length; i++ ) {
49 a[i] = (byte)ac[i];
50 }
51 return a;
52 }
53
54 private Util() {} // never
55 }