00001
package com.quadcap.net.server;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.FileOutputStream;
00042
import java.io.IOException;
00043
import java.io.InputStream;
00044
00045
import com.quadcap.util.Debug;
00046
import com.quadcap.util.Util;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 public final class WorkerInputStream extends InputStream {
00060 final static int MAX = 4096;
00061 byte[]
buf =
new byte[
MAX+1];
00062 int pos = 0;
00063 int lim = 0;
00064 boolean eof =
false;
00065 InputStream
in;
00066
00067
00068 static boolean doTrace =
false;
00069 FileOutputStream
log;
00070
00071
00072
00073 public WorkerInputStream(FileOutputStream f) {
00074
00075
doTrace = (f != null);
00076
log = f;
00077
00078 }
00079
00080 public final void reset(InputStream in)
throws IOException {
00081
this.in =
in;
00082
this.eof =
false;
00083
this.pos = 0;
00084
this.lim = 0;
00085
00086
if (
doTrace) {
00087
log.write((
"RESET " + Thread.currentThread().getName() +
00088
"\r\n").getBytes());
00089 }
00090
00091 }
00092
00093 public final int read() throws IOException {
00094
if (
pos >=
lim && !
eof)
fill();
00095
int c =
eof ? -1 :
buf[
pos++];
00096
return c;
00097 }
00098
00099 public void unread(
int c) {
00100
buf[--
pos] = (byte)c;
00101 }
00102
00103
00104
00105
00106
00107 final boolean fill() throws IOException {
00108
boolean ret =
true;
00109
pos = 0;
00110
lim =
in.read(
buf, 0,
MAX);
00111
if (
lim < 0) {
00112
eof =
true;
00113
lim = 0;
00114 ret =
false;
00115 }
00116
00117
if (
doTrace)
log.write(
buf, 0,
lim);
00118
00119
return ret;
00120 }
00121
00122 public final int read(byte[] b,
int off,
int len)
throws IOException {
00123
int rlim = off + len;
00124
while (off < rlim && !
eof) {
00125
if (
pos >=
lim)
fill();
00126
int cnt =
lim -
pos;
00127
if (len < cnt) cnt = len;
00128
if (cnt > 0) {
00129 System.arraycopy(
buf, pos, b, off, cnt);
00130 pos += cnt;
00131 off += cnt;
00132 }
00133 }
00134
int cnt = off - (rlim - len);
00135
return cnt == 0 ? (
eof ? -1 : 0) : cnt;
00136 }
00137
00138 public final int read(byte[] b)
throws IOException {
00139
return read(b, 0, b.length);
00140 }
00141
00142 static byte[]
CRLF = { 0x0d, 0x0a, 0x0d, 0x0a };
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 public final int readHeaders(byte[] hbuf,
int[] offsets)
00153
throws IOException
00154 {
00155
int off = 0;
00156
int p =
pos;
00157
int l =
lim;
00158
int hcnt = 1;
00159
buf[lim] = (byte)
'\r';
00160
int s = 1;
00161
while (
true) {
00162
while (
buf[p++] !=
'\r')
continue;
00163
while (l - p < 4 - s) {
00164
if (p > l) {
00165 p--;
00166 s--;
00167 }
00168
while (p < l &&
buf[p++] ==
CRLF[s]) {
00169
if (++s == 2 && off + p - pos > 2) {
00170 offsets[hcnt++] = p - pos + off;
00171 }
00172 }
00173
int len = p - pos;
00174 System.arraycopy(buf, pos, hbuf, off, len);
00175
fill();
00176
if (
eof) {
00177 offsets[0] = hcnt - 1;
00178
return off + len;
00179 }
00180 p = 0;
00181 l = lim;
00182 buf[lim] = (byte)
'\r';
00183 off += len;
00184 }
00185
while (
buf[p++] ==
CRLF[s]) {
00186 ++s;
00187
final int len = p - pos;
00188
if (s == 2 && len+off > 2) {
00189 offsets[hcnt++] = len + off;
00190 }
else if (s == 4) {
00191 System.arraycopy(
buf, pos, hbuf, off, len);
00192 pos = p;
00193 offsets[0] = hcnt - 1;
00194
return off + len;
00195 }
00196 }
00197 s = 1;
00198 }
00199 }
00200
00201 final void show(String s,
int p) {
00202 System.out.println(
"------ " + s);
00203
for (
int i = 0; i <
lim; i++) {
00204
if (
buf[i] ==
'\r') System.out.print(
'D');
00205
else if (
buf[i] ==
'\n') System.out.print(
'A');
00206
else System.out.print((
char)
buf[i]);
00207 }
00208 System.out.println(
"");
00209
for (
int i = 0; i < p; i++) {
00210 System.out.print(
" ");
00211 }
00212 System.out.println(
"^");
00213 System.out.println(
"");
00214 }
00215
00216 public final void close() throws IOException {
00217
in.close();
00218
in = null;
00219
00220
if (
doTrace)
log.write(
"CLOSE\r\n".getBytes());
00221
00222 }
00223
00224 public String
toString() {
00225
return Util.strBytes(
buf, 0,
lim);
00226 }
00227
00228 }