1 package dolda.jsvc.util;
8 public static final java.nio.charset.Charset utf8 = java.nio.charset.Charset.forName("UTF-8");
9 public static final java.nio.charset.Charset ascii = java.nio.charset.Charset.forName("US-ASCII");
10 private static Map<Integer, String> stext = new HashMap<Integer, String>();
14 stext.put(300, "Multiple Choices");
15 stext.put(301, "Permanently Moved");
16 stext.put(302, "Temporarily Moved");
17 stext.put(303, "See Other");
18 stext.put(304, "Not Modified");
19 stext.put(400, "Bad Request");
20 stext.put(401, "Authentication Required");
21 stext.put(403, "Access Forbidden");
22 stext.put(404, "Resource Not Found");
23 stext.put(500, "Server Error");
26 public static String statustext(int status) {
28 if((text = stext.get(status)) != null)
30 return("Server Flimsiness");
33 public static String stripslashes(String p, boolean beg, boolean end) {
34 while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/'))
35 p = p.substring(0, p.length() - 1);
36 while(beg && (p.length() > 0) && (p.charAt(0) == '/'))
41 static byte[] readall(InputStream in) throws IOException {
42 byte[] buf = new byte[4096];
45 if(off == buf.length) {
46 byte[] n = new byte[buf.length * 2];
47 System.arraycopy(buf, 0, n, 0, buf.length);
50 int ret = in.read(buf, off, buf.length - off);
52 byte[] n = new byte[off];
53 System.arraycopy(buf, 0, n, 0, off);
60 public static void cpstream(InputStream in, OutputStream out) throws IOException {
61 byte[] buf = new byte[4096];
63 int ret = in.read(buf, 0, buf.length);
66 out.write(buf, 0, ret);
70 public static Responder stdroot(Responder root) {
72 ret = new Rehandler(ret);
73 ret = new ErrorHandler(ret);
77 public static int hex2int(char digit) {
78 if((digit >= '0') && (digit <= '9'))
80 if((digit >= 'a') && (digit <= 'f'))
81 return(digit - 'a' + 10);
82 if((digit >= 'A') && (digit <= 'F'))
83 return(digit - 'A' + 10);
84 throw(new NumberFormatException("Invalid hex digit " + digit));
87 public static char int2hex(int nibble, boolean upper) {
88 if((nibble >= 0) && (nibble <= 9))
89 return((char)('0' + nibble));
90 if((nibble >= 10) && (nibble <= 15))
91 return((char)((upper?'A':'a') + nibble - 10));
92 throw(new NumberFormatException("Invalid hex nibble " + nibble));
95 public static String htmlq(String in) {
96 StringBuilder buf = new StringBuilder();
97 for(int i = 0; i < in.length(); i++) {
98 char c = in.charAt(i);
108 return(buf.toString());
111 public static String urlq(String in) {
112 byte[] bytes = in.getBytes(utf8);
113 StringBuilder buf = new StringBuilder();
114 for(int i = 0; i < bytes.length; i++) {
116 if((b < 32) || (b == ' ') || (b == '&') || (b == '?') || (b == '/') || (b == '=') || (b == '#') || (b == '%') || (b == '+') || (b >= 128)) {
118 buf.append(int2hex((b & 0xf0) >> 4, true));
119 buf.append(int2hex(b & 0x0f, true));
124 return(buf.toString());
127 public static boolean boolval(String val) {
128 val = val.trim().toLowerCase();
129 if(val.equals("1") || val.equals("on") || val.equals("true") || val.equals("yes") || val.equals("\u22a4"))
131 if(val.equals("0") || val.equals("off") || val.equals("false") || val.equals("no") || val.equals("\u22a5"))
133 throw(new IllegalArgumentException("value not recognized as boolean: " + val));
136 public static void eatws(PushbackReader in) throws IOException {
142 } while(Character.isWhitespace(c));