1 package dolda.jsvc.util;
8 public static final java.nio.charset.Charset utf8 = java.nio.charset.Charset.forName("UTF-8");
9 private static Map<Integer, String> stext = new HashMap<Integer, String>();
13 stext.put(300, "Multiple Choices");
14 stext.put(301, "Permanently Moved");
15 stext.put(302, "Temporarily Moved");
16 stext.put(303, "See Other");
17 stext.put(304, "Not Modified");
18 stext.put(400, "Bad Request");
19 stext.put(401, "Authentication Required");
20 stext.put(403, "Access Forbidden");
21 stext.put(404, "Resource Not Found");
22 stext.put(500, "Server Error");
25 public static String statustext(int status) {
27 if((text = stext.get(status)) != null)
29 return("Server Flimsiness");
32 public static String stripslashes(String p, boolean beg, boolean end) {
33 while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/'))
34 p = p.substring(0, p.length() - 1);
35 while(beg && (p.length() > 0) && (p.charAt(0) == '/'))
40 static byte[] readall(InputStream in) throws IOException {
41 byte[] buf = new byte[4096];
44 if(off == buf.length) {
45 byte[] n = new byte[buf.length * 2];
46 System.arraycopy(buf, 0, n, 0, buf.length);
49 int ret = in.read(buf, off, buf.length - off);
51 byte[] n = new byte[off];
52 System.arraycopy(buf, 0, n, 0, off);
59 public static void cpstream(InputStream in, OutputStream out) throws IOException {
60 byte[] buf = new byte[4096];
62 int ret = in.read(buf, 0, buf.length);
65 out.write(buf, 0, ret);
69 public static Responder stdroot(Responder root) {
71 ret = new Rehandler(ret);
72 ret = new ErrorHandler(ret);
76 public static int hex2int(char digit) {
77 if((digit >= '0') && (digit <= '9'))
79 if((digit >= 'a') && (digit <= 'f'))
80 return(digit - 'a' + 10);
81 if((digit >= 'A') && (digit <= 'F'))
82 return(digit - 'A' + 10);
83 throw(new NumberFormatException("Invalid hex digit " + digit));
86 public static char int2hex(int nibble, boolean upper) {
87 if((nibble >= 0) && (nibble <= 9))
88 return((char)('0' + nibble));
89 if((nibble >= 10) && (nibble <= 15))
90 return((char)((upper?'A':'a') + nibble - 10));
91 throw(new NumberFormatException("Invalid hex nibble " + nibble));
94 public static String htmlq(String in) {
95 StringBuilder buf = new StringBuilder();
96 for(int i = 0; i < in.length(); i++) {
97 char c = in.charAt(i);
107 return(buf.toString());
110 public static String urlq(String in) {
111 byte[] bytes = in.getBytes(utf8);
112 StringBuilder buf = new StringBuilder();
113 for(int i = 0; i < bytes.length; i++) {
115 if((b < 32) || (b == ' ') || (b == '&') || (b == '?') || (b == '/') || (b == '=') || (b == '#') || (b == '%') || (b == '+') || (b >= 128)) {
117 buf.append(int2hex((b & 0xf0) >> 4, true));
118 buf.append(int2hex(b & 0x0f, true));
123 return(buf.toString());