1 package com.quadcap.net.server; 2 3 40 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 45 import com.quadcap.util.Debug; 46 import com.quadcap.util.Util; 47 48 59 public final class WorkerInputStream extends InputStream { 60 final static int MAX = 4096; 61 byte[] buf = new byte[MAX+1]; 62 int pos = 0; 63 int lim = 0; 64 boolean eof = false; 65 InputStream in; 66 67 static boolean doTrace = false; 69 FileOutputStream log; 70 72 73 public WorkerInputStream(FileOutputStream f) { 74 doTrace = (f != null); 76 log = f; 77 } 79 80 public final void reset(InputStream in) throws IOException { 81 this.in = in; 82 this.eof = false; 83 this.pos = 0; 84 this.lim = 0; 85 if (doTrace) { 87 log.write(("RESET " + Thread.currentThread().getName() + 88 "\r\n").getBytes()); 89 } 90 } 92 93 public final int read() throws IOException { 94 if (pos >= lim && !eof) fill(); 95 int c = eof ? -1 : buf[pos++]; 96 return c; 97 } 98 99 public void unread(int c) { 100 buf[--pos] = (byte)c; 101 } 102 103 107 final boolean fill() throws IOException { 108 boolean ret = true; 109 pos = 0; 110 lim = in.read(buf, 0, MAX); 111 if (lim < 0) { 112 eof = true; 113 lim = 0; 114 ret = false; 115 } 116 if (doTrace) log.write(buf, 0, lim); 118 return ret; 120 } 121 122 public final int read(byte[] b, int off, int len) throws IOException { 123 int rlim = off + len; 124 while (off < rlim && !eof) { 125 if (pos >= lim) fill(); 126 int cnt = lim - pos; 127 if (len < cnt) cnt = len; 128 if (cnt > 0) { 129 System.arraycopy(buf, pos, b, off, cnt); 130 pos += cnt; 131 off += cnt; 132 } 133 } 134 int cnt = off - (rlim - len); 135 return cnt == 0 ? (eof ? -1 : 0) : cnt; 136 } 137 138 public final int read(byte[] b) throws IOException { 139 return read(b, 0, b.length); 140 } 141 142 static byte[] CRLF = { 0x0d, 0x0a, 0x0d, 0x0a }; 143 144 152 public final int readHeaders(byte[] hbuf, int[] offsets) 153 throws IOException 154 { 155 int off = 0; int p = pos; 157 int l = lim; 158 int hcnt = 1; 159 buf[lim] = (byte)'\r'; 160 int s = 1; 161 while (true) { 162 while (buf[p++] != '\r') continue; 163 while (l - p < 4 - s) { 164 if (p > l) { 165 p--; 166 s--; 167 } 168 while (p < l && buf[p++] == CRLF[s]) { 169 if (++s == 2 && off + p - pos > 2) { 170 offsets[hcnt++] = p - pos + off; 171 } 172 } 173 int len = p - pos; 174 System.arraycopy(buf, pos, hbuf, off, len); 175 fill(); 176 if (eof) { 177 offsets[0] = hcnt - 1; 178 return off + len; 179 } 180 p = 0; 181 l = lim; 182 buf[lim] = (byte)'\r'; 183 off += len; 184 } 185 while (buf[p++] == CRLF[s]) { 186 ++s; 187 final int len = p - pos; 188 if (s == 2 && len+off > 2) { 189 offsets[hcnt++] = len + off; 190 } else if (s == 4) { 191 System.arraycopy(buf, pos, hbuf, off, len); 192 pos = p; 193 offsets[0] = hcnt - 1; 194 return off + len; 195 } 196 } 197 s = 1; 198 } 199 } 200 201 final void show(String s, int p) { 202 System.out.println("------ " + s); 203 for (int i = 0; i < lim; i++) { 204 if (buf[i] == '\r') System.out.print('D'); 205 else if (buf[i] == '\n') System.out.print('A'); 206 else System.out.print((char)buf[i]); 207 } 208 System.out.println(""); 209 for (int i = 0; i < p; i++) { 210 System.out.print(" "); 211 } 212 System.out.println("^"); 213 System.out.println(""); 214 } 215 216 public final void close() throws IOException { 217 in.close(); 218 in = null; 219 if (doTrace) log.write("CLOSE\r\n".getBytes()); 221 } 223 224 public String toString() { 225 return Util.strBytes(buf, 0, lim); 226 } 227 228 } 229 | Popular Tags |