home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Komunik / sambar / sambar51p.exe / lib / javaeng.jar / javax / servlet / ServletOutputStream.java < prev    next >
Encoding:
Java Source  |  2001-10-22  |  10.5 KB  |  409 lines

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution, if
  20.  *    any, must include the following acknowlegement:  
  21.  *       "This product includes software developed by the 
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowlegement may appear in the software itself,
  24.  *    if and wherever such third-party acknowlegements normally appear.
  25.  *
  26.  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27.  *    Foundation" must not be used to endorse or promote products derived
  28.  *    from this software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache"
  32.  *    nor may "Apache" appear in their names without prior written
  33.  *    permission of the Apache Group.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * ====================================================================
  55.  *
  56.  * This source code implements specifications defined by the Java
  57.  * Community Process. In order to remain compliant with the specification
  58.  * DO NOT add / change / or delete method signatures!
  59.  */
  60.  
  61. package javax.servlet;
  62.  
  63. import java.io.OutputStream;
  64. import java.io.IOException;
  65. import java.io.CharConversionException;
  66. import java.text.MessageFormat;
  67. import java.util.ResourceBundle;
  68.  
  69. /**
  70.  * Provides an output stream for sending binary data to the
  71.  * client. A <code>ServletOutputStream</code> object is normally retrieved 
  72.  * via the {@link ServletResponse#getOutputStream} method.
  73.  *
  74.  * <p>This is an abstract class that the servlet container implements.
  75.  * Subclasses of this class
  76.  * must implement the <code>java.io.OutputStream.write(int)</code>
  77.  * method.
  78.  *
  79.  * 
  80.  * @author     Various
  81.  * @version     $Version$
  82.  *
  83.  * @see     ServletResponse
  84.  *
  85.  */
  86.  
  87. public abstract class ServletOutputStream extends OutputStream {
  88.  
  89.     private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
  90.     private static ResourceBundle lStrings =
  91.     ResourceBundle.getBundle(LSTRING_FILE);
  92.  
  93.  
  94.     
  95.     /**
  96.      *
  97.      * Does nothing, because this is an abstract class.
  98.      *
  99.      */
  100.  
  101.     protected ServletOutputStream() { }
  102.  
  103.  
  104.     /**
  105.      * Writes a <code>String</code> to the client, 
  106.      * without a carriage return-line feed (CRLF) 
  107.      * character at the end.
  108.      *
  109.      *
  110.      * @param s            the <code>String</code to send to the client
  111.      *
  112.      * @exception IOException     if an input or output exception occurred
  113.      *
  114.      */
  115.  
  116.     public void print(String s) throws IOException {
  117.     if (s==null) s="null";
  118.     int len = s.length();
  119.     for (int i = 0; i < len; i++) {
  120.         char c = s.charAt (i);
  121.  
  122.         //
  123.         // XXX NOTE:  This is clearly incorrect for many strings,
  124.         // but is the only consistent approach within the current
  125.         // servlet framework.  It must suffice until servlet output
  126.         // streams properly encode their output.
  127.         //
  128.         if ((c & 0xff00) != 0) {    // high order byte must be zero
  129.         String errMsg = lStrings.getString("err.not_iso8859_1");
  130.         Object[] errArgs = new Object[1];
  131.         errArgs[0] = new Character(c);
  132.         errMsg = MessageFormat.format(errMsg, errArgs);
  133.         throw new CharConversionException(errMsg);
  134.         }
  135.         write (c);
  136.     }
  137.     }
  138.  
  139.  
  140.  
  141.     /**
  142.      * Writes a <code>boolean</code> value to the client,
  143.      * with no carriage return-line feed (CRLF) 
  144.      * character at the end.
  145.      *
  146.      * @param b            the <code>boolean</code> value 
  147.      *                to send to the client
  148.      *
  149.      * @exception IOException     if an input or output exception occurred
  150.      *
  151.      */
  152.  
  153.     public void print(boolean b) throws IOException {
  154.     String msg;
  155.     if (b) {
  156.         msg = lStrings.getString("value.true");
  157.     } else {
  158.         msg = lStrings.getString("value.false");
  159.     }
  160.     print(msg);
  161.     }
  162.  
  163.  
  164.  
  165.     /**
  166.      * Writes a character to the client,
  167.      * with no carriage return-line feed (CRLF) 
  168.      * at the end.
  169.      *
  170.      * @param c            the character to send to the client
  171.      *
  172.      * @exception IOException     if an input or output exception occurred
  173.      *
  174.      */
  175.  
  176.     public void print(char c) throws IOException {
  177.     print(String.valueOf(c));
  178.     }
  179.  
  180.  
  181.  
  182.  
  183.     /**
  184.      *
  185.      * Writes an int to the client,
  186.      * with no carriage return-line feed (CRLF) 
  187.      * at the end.
  188.      *
  189.      * @param i            the int to send to the client
  190.      *
  191.      * @exception IOException     if an input or output exception occurred
  192.      *
  193.      */  
  194.  
  195.     public void print(int i) throws IOException {
  196.     print(String.valueOf(i));
  197.     }
  198.  
  199.  
  200.  
  201.  
  202.     /**
  203.      * 
  204.      * Writes a <code>long</code> value to the client,
  205.      * with no carriage return-line feed (CRLF) at the end.
  206.      *
  207.      * @param l            the <code>long</code> value 
  208.      *                to send to the client
  209.      *
  210.      * @exception IOException     if an input or output exception 
  211.      *                occurred
  212.      * 
  213.      */
  214.  
  215.     public void print(long l) throws IOException {
  216.     print(String.valueOf(l));
  217.     }
  218.  
  219.  
  220.  
  221.     /**
  222.      *
  223.      * Writes a <code>float</code> value to the client,
  224.      * with no carriage return-line feed (CRLF) at the end.
  225.      *
  226.      * @param f            the <code>float</code> value
  227.      *                to send to the client
  228.      *
  229.      * @exception IOException    if an input or output exception occurred
  230.      *
  231.      *
  232.      */
  233.  
  234.     public void print(float f) throws IOException {
  235.     print(String.valueOf(f));
  236.     }
  237.  
  238.  
  239.  
  240.     /**
  241.      *
  242.      * Writes a <code>double</code> value to the client,
  243.      * with no carriage return-line feed (CRLF) at the end.
  244.      * 
  245.      * @param d            the <code>double</code> value
  246.      *                to send to the client
  247.      *
  248.      * @exception IOException     if an input or output exception occurred
  249.      *
  250.      */
  251.  
  252.     public void print(double d) throws IOException {
  253.     print(String.valueOf(d));
  254.     }
  255.  
  256.  
  257.  
  258.     /**
  259.      * Writes a carriage return-line feed (CRLF)
  260.      * to the client.
  261.      *
  262.      *
  263.      *
  264.      * @exception IOException     if an input or output exception occurred
  265.      *
  266.      */
  267.  
  268.     public void println() throws IOException {
  269.     print("\r\n");
  270.     }
  271.  
  272.  
  273.  
  274.     /**
  275.      * Writes a <code>String</code> to the client, 
  276.      * followed by a carriage return-line feed (CRLF).
  277.      *
  278.      *
  279.      * @param s            the </code>String</code> to write to the client
  280.      *
  281.      * @exception IOException     if an input or output exception occurred
  282.      *
  283.      */
  284.  
  285.     public void println(String s) throws IOException {
  286.     print(s);
  287.     println();
  288.     }
  289.  
  290.  
  291.  
  292.  
  293.     /**
  294.      *
  295.      * Writes a <code>boolean</code> value to the client, 
  296.      * followed by a 
  297.      * carriage return-line feed (CRLF).
  298.      *
  299.      *
  300.      * @param b            the <code>boolean</code> value 
  301.      *                to write to the client
  302.      *
  303.      * @exception IOException     if an input or output exception occurred
  304.      *
  305.      */
  306.  
  307.     public void println(boolean b) throws IOException {
  308.     print(b);
  309.     println();
  310.     }
  311.  
  312.  
  313.  
  314.     /**
  315.      *
  316.      * Writes a character to the client, followed by a carriage
  317.      * return-line feed (CRLF).
  318.      *
  319.      * @param c            the character to write to the client
  320.      *
  321.      * @exception IOException     if an input or output exception occurred
  322.      *
  323.      */
  324.  
  325.     public void println(char c) throws IOException {
  326.     print(c);
  327.     println();
  328.     }
  329.  
  330.  
  331.  
  332.     /**
  333.      *
  334.      * Writes an int to the client, followed by a 
  335.      * carriage return-line feed (CRLF) character.
  336.      *
  337.      *
  338.      * @param i            the int to write to the client
  339.      *
  340.      * @exception IOException     if an input or output exception occurred
  341.      *
  342.      */
  343.  
  344.     public void println(int i) throws IOException {
  345.     print(i);
  346.     println();
  347.     }
  348.  
  349.  
  350.  
  351.     /**  
  352.      *
  353.      * Writes a <code>long</code> value to the client, followed by a 
  354.      * carriage return-line feed (CRLF).
  355.      *
  356.      *
  357.      * @param l            the <code>long</code> value to write to the client
  358.      *
  359.      * @exception IOException     if an input or output exception occurred
  360.      *
  361.      */  
  362.  
  363.     public void println(long l) throws IOException {
  364.     print(l);
  365.     println();
  366.     }
  367.  
  368.  
  369.  
  370.     /**
  371.      *
  372.      * Writes a <code>float</code> value to the client, 
  373.      * followed by a carriage return-line feed (CRLF).
  374.      *
  375.      * @param f            the <code>float</code> value 
  376.      *                to write to the client
  377.      *
  378.      *
  379.      * @exception IOException     if an input or output exception 
  380.      *                occurred
  381.      *
  382.      */
  383.  
  384.     public void println(float f) throws IOException {
  385.     print(f);
  386.     println();
  387.     }
  388.  
  389.  
  390.  
  391.     /**
  392.      *
  393.      * Writes a <code>double</code> value to the client, 
  394.      * followed by a carriage return-line feed (CRLF).
  395.      *
  396.      *
  397.      * @param d            the <code>double</code> value
  398.      *                to write to the client
  399.      *
  400.      * @exception IOException     if an input or output exception occurred
  401.      *
  402.      */
  403.  
  404.     public void println(double d) throws IOException {
  405.     print(d);
  406.     println();
  407.     }
  408. }
  409.