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

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 
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               //if (chunking)
00087               //log("Cannot handle CR in chunking mode");
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       //System.err.println(buf.toString()); //###
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       //System.err.println(new String(b,off,len)); //###        
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       // Get next non blank line
00202       chunking = false;
00203       String line = readLine();
00204       while (line != null && line.length() == 0)
00205         line = readLine();
00206       chunking = true;
00207 
00208       // Handle early EOF or error in format
00209       if (line == null)
00210         return -1;
00211 
00212       // Get chunksize
00213       int i = line.indexOf(';');
00214       if (i > 0)
00215         line = line.substring(0, i).trim();
00216       chunksize = Integer.parseInt(line, 16);
00217 
00218       // check for EOF
00219       if (chunksize == 0) {
00220         chunksize = -1;
00221         // Look for footers
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 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4