00001
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 package org.openmobileis.embedded.webserver;
00031
00032 import java.io.BufferedInputStream;
00033 import java.io.IOException;
00034 import java.io.InputStream;
00035
00036 import javax.servlet.ServletInputStream;
00037
00038 public class ServeInputStream extends ServletInputStream {
00039
00040 private BufferedInputStream in;
00041 private int chunksize = 0;
00042 private boolean chunking = false;
00043 private boolean returnedAsReader, returnedAsStream;
00044
00045
00048 public ServeInputStream(InputStream in) {
00049 this.in = new BufferedInputStream(in);
00050 }
00051
00052
00056 public void chunking(boolean chunking) {
00057 this.chunking = chunking;
00058 }
00059
00060
00066
00067 public String readLine() throws IOException {
00068 StringBuffer buf = new StringBuffer(1024);
00069
00070 int c;
00071 boolean cr = false;
00072 boolean lf = false;
00073
00074 LineLoop : while ((c = chunking ? read() : in.read()) != -1) {
00075 switch (c) {
00076 case 10 :
00077 lf = true;
00078 break LineLoop;
00079
00080 case 13 :
00081 cr = true;
00082 if (!chunking)
00083 in.mark(2);
00084 break;
00085
00086 default :
00087 if (cr) {
00088
00089
00090 in.reset();
00091 break LineLoop;
00092 } else
00093 buf.append((char) c);
00094 break;
00095 }
00096 }
00097
00098 if (c == -1 && buf.length() == 0)
00099 return null;
00100
00101
00102 return buf.toString();
00103 }
00104
00105
00106 public int read() throws IOException {
00107 if (chunking) {
00108 int b = -1;
00109 if (chunksize <= 0 && getChunkSize() <= 0)
00110 return -1;
00111 b = in.read();
00112 chunksize = (b < 0) ? -1 : (chunksize - 1);
00113 return b;
00114 }
00115
00116 return in.read();
00117 }
00118
00119
00120 public int read(byte b[]) throws IOException {
00121 return read(b, 0, b.length);
00122 }
00123
00124
00125 public int read(byte b[], int off, int len) throws IOException {
00126 if (chunking) {
00127 if (chunksize <= 0 && getChunkSize() <= 0)
00128 return -1;
00129 if (len > chunksize)
00130 len = chunksize;
00131 len = in.read(b, off, len);
00132 chunksize = (len < 0) ? -1 : (chunksize - len);
00133 } else
00134 len = in.read(b, off, len);
00135
00136
00137 return len;
00138 }
00139
00140
00141 public long skip(long len) throws IOException {
00142 if (chunking) {
00143 if (chunksize <= 0 && getChunkSize() <= 0)
00144 return -1;
00145 if (len > chunksize)
00146 len = chunksize;
00147 len = in.skip(len);
00148 chunksize = (len < 0) ? -1 : (chunksize - (int) len);
00149 } else
00150 len = in.skip(len);
00151 return len;
00152 }
00153
00154
00158 public int available() throws IOException {
00159 if (chunking) {
00160 int len = in.available();
00161 if (len <= chunksize)
00162 return len;
00163 return chunksize;
00164 }
00165
00166 return in.available();
00167 }
00168
00169
00170 public void close() throws IOException {
00171 in.close();
00172 chunksize = -1;
00173 }
00174
00175
00179 public boolean markSupported() {
00180 return false;
00181 }
00182
00183
00186 public void reset() {
00187 }
00188
00189
00193 public void mark(int readlimit) {
00194 }
00195
00196
00197 private int getChunkSize() throws IOException {
00198 if (chunksize < 0)
00199 return -1;
00200
00201 chunksize = -1;
00202
00203
00204 chunking = false;
00205 String line = readLine();
00206 while (line != null && line.length() == 0)
00207 line = readLine();
00208 chunking = true;
00209
00210
00211 if (line == null)
00212 return -1;
00213
00214
00215 int i = line.indexOf(';');
00216 if (i > 0)
00217 line = line.substring(0, i).trim();
00218 chunksize = Integer.parseInt(line, 16);
00219
00220
00221 if (chunksize == 0) {
00222 chunksize = -1;
00223
00224 chunking = false;
00225 }
00226 return chunksize;
00227 }
00228
00229 boolean isReturnedAsStream() {
00230 return returnedAsStream;
00231 }
00232
00233 void setReturnedAsStream(boolean _on) {
00234 returnedAsStream = _on;
00235 }
00236
00237 boolean isReturnedAsReader() {
00238 return returnedAsReader;
00239 }
00240
00241 void setReturnedAsReader(boolean _on) {
00242 returnedAsReader = _on;
00243 }
00244
00245
00246 }