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
00073 LineLoop : while ((c = chunking ? read() : in.read()) != -1) {
00074 switch (c) {
00075 case 10 :
00076 break LineLoop;
00077
00078 case 13 :
00079 cr = true;
00080 if (!chunking)
00081 in.mark(2);
00082 break;
00083
00084 default :
00085 if (cr) {
00086
00087
00088 in.reset();
00089 break LineLoop;
00090 } else
00091 buf.append((char) c);
00092 break;
00093 }
00094 }
00095
00096 if (c == -1 && buf.length() == 0)
00097 return null;
00098
00099
00100 return buf.toString();
00101 }
00102
00103
00104 public int read() throws IOException {
00105 if (chunking) {
00106 int b = -1;
00107 if (chunksize <= 0 && getChunkSize() <= 0)
00108 return -1;
00109 b = in.read();
00110 chunksize = (b < 0) ? -1 : (chunksize - 1);
00111 return b;
00112 }
00113
00114 return in.read();
00115 }
00116
00117
00118 public int read(byte b[]) throws IOException {
00119 return read(b, 0, b.length);
00120 }
00121
00122
00123 public int read(byte b[], int off, int len) throws IOException {
00124 if (chunking) {
00125 if (chunksize <= 0 && getChunkSize() <= 0)
00126 return -1;
00127 if (len > chunksize)
00128 len = chunksize;
00129 len = in.read(b, off, len);
00130 chunksize = (len < 0) ? -1 : (chunksize - len);
00131 } else
00132 len = in.read(b, off, len);
00133
00134
00135 return len;
00136 }
00137
00138
00139 public long skip(long len) throws IOException {
00140 if (chunking) {
00141 if (chunksize <= 0 && getChunkSize() <= 0)
00142 return -1;
00143 if (len > chunksize)
00144 len = chunksize;
00145 len = in.skip(len);
00146 chunksize = (len < 0) ? -1 : (chunksize - (int) len);
00147 } else
00148 len = in.skip(len);
00149 return len;
00150 }
00151
00152
00156 public int available() throws IOException {
00157 if (chunking) {
00158 int len = in.available();
00159 if (len <= chunksize)
00160 return len;
00161 return chunksize;
00162 }
00163
00164 return in.available();
00165 }
00166
00167
00168 public void close() throws IOException {
00169 in.close();
00170 chunksize = -1;
00171 }
00172
00173
00177 public boolean markSupported() {
00178 return false;
00179 }
00180
00181
00184 public void reset() {
00185 }
00186
00187
00191 public void mark(int readlimit) {
00192 }
00193
00194
00195 private int getChunkSize() throws IOException {
00196 if (chunksize < 0)
00197 return -1;
00198
00199 chunksize = -1;
00200
00201
00202 chunking = false;
00203 String line = readLine();
00204 while (line != null && line.length() == 0)
00205 line = readLine();
00206 chunking = true;
00207
00208
00209 if (line == null)
00210 return -1;
00211
00212
00213 int i = line.indexOf(';');
00214 if (i > 0)
00215 line = line.substring(0, i).trim();
00216 chunksize = Integer.parseInt(line, 16);
00217
00218
00219 if (chunksize == 0) {
00220 chunksize = -1;
00221
00222 chunking = false;
00223 }
00224 return chunksize;
00225 }
00226
00227 boolean isReturnedAsStream() {
00228 return returnedAsStream;
00229 }
00230
00231 void setReturnedAsStream(boolean _on) {
00232 returnedAsStream = _on;
00233 }
00234
00235 boolean isReturnedAsReader() {
00236 return returnedAsReader;
00237 }
00238
00239 void setReturnedAsReader(boolean _on) {
00240 returnedAsReader = _on;
00241 }
00242
00243
00244 }