6 import java.nio.channels.*;
9 public static final java.nio.charset.Charset UTF8 = java.nio.charset.Charset.forName("UTF-8");
10 public static final java.nio.charset.Charset LATIN1 = java.nio.charset.Charset.forName("ISO-8859-1");
11 public static final java.nio.charset.Charset ASCII = java.nio.charset.Charset.forName("US-ASCII");
13 public static int read(ReadableByteChannel ch) throws IOException {
14 ByteBuffer buf = ByteBuffer.allocate(1);
16 int rv = ch.read(buf);
20 return(buf.get(0) & 0xff);
22 throw(new AssertionError());
26 public static ByteBuffer readall(ReadableByteChannel ch, ByteBuffer dst) throws IOException {
27 while(dst.remaining() > 0)
32 public static void writeall(WritableByteChannel ch, ByteBuffer src) throws IOException {
33 while(src.remaining() > 0)
37 public static void transfer(WritableByteChannel dst, ReadableByteChannel src) throws IOException {
38 ByteBuffer buf = ByteBuffer.allocate(65536);
44 while(buf.remaining() > 0)
49 public static String htmlquote(CharSequence text) {
50 StringBuilder buf = new StringBuilder();
51 for(int i = 0; i < text.length(); i++) {
52 char c = text.charAt(i);
54 case '&': buf.append("&"); break;
55 case '<': buf.append("<"); break;
56 case '>': buf.append(">"); break;
57 case '"': buf.append("""); break;
58 default: buf.append(c); break;
61 return(buf.toString());
64 public static Map<Object, Object> simpleerror(int code, CharSequence title, CharSequence msg) {
65 StringBuilder buf = new StringBuilder();
66 buf.append("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\n");
67 buf.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
68 buf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">\n");
69 buf.append("<head>\n");
70 buf.append("<title>" + title + "</title>\n");
71 buf.append("</head>\n");
72 buf.append("<body>\n");
73 buf.append("<h1>" + title + "</h1>\n");
74 buf.append("<p>" + htmlquote(msg) + "</p>\n");
75 buf.append("</body>\n");
76 buf.append("</html>\n");
77 ByteBuffer out = ASCII.encode(CharBuffer.wrap(buf));
78 Map<Object, Object> resp = new HashMap<>();
79 resp.put("http.status", code + " " + title);
80 resp.put("http.Content-Type", "text/html; charset=us-ascii");
81 resp.put("http.Content-Length", Integer.toString(out.remaining()));
82 resp.put("jagi.output", out);