Quadcap Embeddable Server

com/quadcap/net/server/WorkerInputStream.java

Go to the documentation of this file.
00001 package com.quadcap.net.server; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * An efficient worker input stream, which supports recycling. 00050 * Additionally, this class performs all necessary buffering and 00051 * some other higher level functions on the socket input stream, so 00052 * no extra 'buffered' are needed. 00053 * 00054 * <p>In a violoation of layering, but to achieve maximum performance, 00055 * this class knows how to "parse" HTTP headers.</p> 00056 * 00057 * @author Stan Bailes 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 //#ifdef DEBUG 00068 static boolean doTrace = false; 00069 FileOutputStream log; 00070 //#endif 00071 00072 00073 public WorkerInputStream(FileOutputStream f) { 00074 //#ifdef DEBUG 00075 doTrace = (f != null); 00076 log = f; 00077 //#endif 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 //#ifdef DEBUG 00086 if (doTrace) { 00087 log.write(("RESET " + Thread.currentThread().getName() + 00088 "\r\n").getBytes()); 00089 } 00090 //#endif 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 * Fill the buffer. Return true unless we're at end of file and have 00105 * returned zero bytes in this fill 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 //#ifdef DEBUG 00117 if (doTrace) log.write(buf, 0, lim); 00118 //#endif 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 * Read HTTP readers from the input stream. 00146 * @param hbuf We place the entire header octet sequence into hbuf, 00147 * terminated with CRLF 00148 * @param offsets We place the byte offset of the start of each 00149 * header into the offsets array 00150 * @return the number of headers found 00151 */ 00152 public final int readHeaders(byte[] hbuf, int[] offsets) 00153 throws IOException 00154 { 00155 int off = 0; // offset into hbuf 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 //#ifdef DEBUG 00220 if (doTrace) log.write("CLOSE\r\n".getBytes()); 00221 //#endif 00222 } 00223 00224 public String toString() { 00225 return Util.strBytes(buf, 0, lim); 00226 } 00227 00228 }