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 javax.servlet.*; import java.net.*; import java.io.*; import java.util.*; public class SerflerOutputStream extends ServletOutputStream{ private OutputStream o; private SerflerHandler handler; private PrintWriter p; public boolean sStatusDone=false; public boolean sHeadersDone=false; public SerflerOutputStream(SerflerHandler handler, OutputStream o){ this.handler=handler; this.o=o; p=new PrintWriter(o, true); } public void checkSH(){ if(!sStatusDone){ p.println(handler.rStatusLine); } sStatusDone=true; if(!sHeadersDone){ // Enumeration e = handler.rHeaders.keys(); if (e.hasMoreElements()) { while (e.hasMoreElements()) { String name = (String)e.nextElement(); p.println(name + ": " + handler.rHeaders.get(name)); } } p.println(); sHeadersDone=true; } } //======================================================================== // abstract class OutputStream /** * Writes the specified byte to this output stream. *

* @param b the byte. * @exception IOException if an I/O error occurs. */ public void write(int b) throws IOException{ checkSH(); o.write(b); } /** * Writes b.length bytes from the specified byte array * to this output stream. *

* The write method of OutputStream calls * the write method of three arguments with the three * arguments b, 0, and * b.length. * * @param b the data. * @exception IOException if an I/O error occurs. * @see java.io.OutputStream#write(byte[], int, int) * @since JDK1.0 */ public void write(byte b[]) throws IOException { checkSH(); o.write(b, 0, b.length); } /** * Writes len bytes from the specified byte array * starting at offset off to this output stream. * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException if an I/O error occurs. */ public void write(byte b[], int off, int len) throws IOException { checkSH(); o.write(b, off, len); } /** * Flushes this output stream and forces any buffered output bytes * to be written out. * @exception IOException if an I/O error occurs. */ public void flush() throws IOException { checkSH(); o.flush(); } /** * Closes this output stream and releases any system resources * associated with this stream. * @exception IOException if an I/O error occurs. */ public void close() throws IOException { checkSH(); o.close(); } //======================================================================== //======================================================================== // abstract class ServletOutputStream /** * Prints a string. * @exception IOException if an I/O error has occurred */ public void print(String s) throws IOException { checkSH(); p.print(s); } /** * Prints a boolean. * @exception IOException if an I/O error has occurred. */ public void print(boolean b) throws IOException { checkSH(); p.print(b ? "true" : "false"); } /* * Prints a character. * @exception IOException if an I/O error has occurred */ public void print(char c) throws IOException { checkSH(); p.print(String.valueOf(c)); } /** * Prints an integer. * @exception IOException if an I/O error has occurred */ public void print(int i) throws IOException { checkSH(); p.print(String.valueOf(i)); } /** * Prints a long. * @exception IOException if an I/O error has occurred */ public void print(long l) throws IOException { checkSH(); p.print(String.valueOf(l)); } /** * Prints a float. * @exception IOException if an I/O error has occurred */ public void print(float f) throws IOException { checkSH(); p.print(String.valueOf(f)); } /** * Prints a double. * @exception IOException if an I/O error has occurred */ public void print(double d) throws IOException { checkSH(); p.print(String.valueOf(d)); } /** * Prints a CRLF. * @exception IOException if an I/O error has occurred */ public void println() throws IOException { checkSH(); p.println(); } /** * Prints a string followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(String s) throws IOException { checkSH(); p.println(s); } /** * Prints a boolean followed by a CRLF. * @exception IOException if an I/O error has occurred. */ public void println(boolean b) throws IOException { checkSH(); p.println(b); } /* * Prints a character followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(char c) throws IOException { checkSH(); p.println(c); } /** * Prints an integer followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(int i) throws IOException { checkSH(); p.println(i); } /** * Prints a long followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(long l) throws IOException { checkSH(); p.println(l); } /** * Prints a float followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(float f) throws IOException { checkSH(); p.println(f); } /** * Prints a double followed by a CRLF. * @exception IOException if an I/O error has occurred */ public void println(double d) throws IOException { checkSH(); p.println(d); } }