Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ServeInputStream.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
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     // TODO: won't work with encoding
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               //if (chunking)
00089               //log("Cannot handle CR in chunking mode");
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       //System.err.println(buf.toString()); //###
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       //System.err.println(new String(b,off,len)); //###        
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       // Get next non blank line
00204       chunking = false;
00205       String line = readLine();
00206       while (line != null && line.length() == 0)
00207         line = readLine();
00208       chunking = true;
00209 
00210       // Handle early EOF or error in format
00211       if (line == null)
00212         return -1;
00213 
00214       // Get chunksize
00215       int i = line.indexOf(';');
00216       if (i > 0)
00217         line = line.substring(0, i).trim();
00218       chunksize = Integer.parseInt(line, 16);
00219 
00220       // check for EOF
00221       if (chunksize == 0) {
00222         chunksize = -1;
00223         // Look for footers
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 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3