package Serfler; // Serfler - an Http server written in Java based on servlets // Copyright (c) 1998 by Douglas Harris // Distributed under the GNU General Public License // // A copy is included with this software distribution. // The package is available from // http://spectral.mscs.mu.edu/javadev/src/serfler.zip // import java.io.*; import javax.servlet.*; public class SerflerInputStream extends ServletInputStream{ protected PushbackInputStream i; public SerflerInputStream(InputStream i){ this.i=new PushbackInputStream(i); } public int read() throws IOException{ return i.read(); } public int read(byte b[]) throws IOException { return i.read(b, 0, b.length); } public int read(byte b[], int off, int len) throws IOException { return i.read(b, off, len); } public long skip(long n) throws IOException { return i.skip(n); } public int available() throws IOException { return i.available(); } public void close() throws IOException { i.close(); } public synchronized void mark(int readlimit) { i.mark(readlimit); } public synchronized void reset() throws IOException { i.reset(); } public boolean markSupported() { return i.markSupported(); } /** * Reads into an array of bytes until all requested bytes have been * read or a '\n' is encountered, in which case the '\n' is read into * the array as well. * @param b the buffer where data is stored * @param off the start offset of the data * @param len the length of the data * @return the actual number of bytes read, or -1 if the end of the * stream is reached * @exception IOException if an I/O error has occurred */ public int readLine(byte[] b, int off, int len) throws IOException{ int ch; int n=0; if (len==0){ return len; } while (-1!=(ch=i.read())){ b[off+(n++)]=(byte)ch; if((n==len)||(ch==10)){ return n; } } return ch; } /** * Read bytes until CR, LF, or CRLF has been read. * Note LFCR will return an empty String this time * and next time this routine is run. * @return the bytes read converted to a String * @exception IOException if an I/O error has occurred */ public String readLine() throws IOException{ StringBuffer s=new StringBuffer(1024); boolean seenCR=false; int ch; while(-1!=(ch=i.read())){ if(ch==10){break;} if (seenCR){i.unread(ch);break;} if (ch==13){seenCR=true;continue;} s.append((char)ch); } return s.toString(); } }