ThrowingPrintWriter.java

  1. /*
  2.  * $Source$
  3.  * $Revision$
  4.  *
  5.  * Copyright (C) 2000 William Chesters
  6.  *
  7.  * Part of Melati (http://melati.org), a framework for the rapid
  8.  * development of clean, maintainable web applications.
  9.  *
  10.  * Melati is free software; Permission is granted to copy, distribute
  11.  * and/or modify this software under the terms either:
  12.  *
  13.  * a) the GNU General Public License as published by the Free Software
  14.  *    Foundation; either version 2 of the License, or (at your option)
  15.  *    any later version,
  16.  *
  17.  *    or
  18.  *
  19.  * b) any version of the Melati Software License, as published
  20.  *    at http://melati.org
  21.  *
  22.  * You should have received a copy of the GNU General Public License and
  23.  * the Melati Software License along with this program;
  24.  * if not, write to the Free Software Foundation, Inc.,
  25.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
  26.  * GNU General Public License and visit http://melati.org to obtain the
  27.  * Melati Software License.
  28.  *
  29.  * Feel free to contact the Developers of Melati (http://melati.org),
  30.  * if you would like to work out a different arrangement than the options
  31.  * outlined here.  It is our intention to allow Melati to be used by as
  32.  * wide an audience as possible.
  33.  *
  34.  * This program is distributed in the hope that it will be useful,
  35.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37.  * GNU General Public License for more details.
  38.  *
  39.  * Contact details for copyright holder:
  40.  *
  41.  *     William Chesters <williamc At paneris.org>
  42.  *     http://paneris.org/~williamc
  43.  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
  44.  */
  45. package org.melati.util;

  46. import java.io.PrintWriter;

  47. /**
  48.  * A <code>PrintWriter</code> which can throw an <code>Exception</code>.
  49.  */
  50. public final class ThrowingPrintWriter extends PrintWriter {
  51.   private PrintWriter pw;
  52.  
  53.   /** Description of this PrintWriter. */
  54.   public final String description;

  55.  /**
  56.   * Thrown when a programmer attempts to use <code>super.out</code>.
  57.   */
  58.   public static class SuperUseException extends BugException {
  59.     private static final long serialVersionUID = 1L;
  60.     /**
  61.      * Constructor.
  62.      */
  63.     public SuperUseException() {
  64.       super("ThrowingPrintWriter tried to use super.out");
  65.     }
  66.   }

  67.   /**
  68.    * Constructor.
  69.    * @param pw Preint writer to write to
  70.    * @param description A description
  71.    */
  72.   public ThrowingPrintWriter(PrintWriter pw, String description) {
  73.     super(pw);
  74.     this.pw = pw;
  75.     this.description = description;
  76.   }

  77.   /**
  78.    * Thrown if there is a problem writing to this
  79.    * <code>ThowingPrintWriter</code>.
  80.    */
  81.   public class TroubleException extends MelatiRuntimeException {
  82.     private static final long serialVersionUID = 1L;
  83.     /**
  84.      * {@inheritDoc}
  85.      * @see org.melati.util.MelatiRuntimeException#getMessage()
  86.      */
  87.     public String getMessage() {
  88.       return "An exception condition occurred writing to " +
  89.              (description == null ? "a PrintWriter" : description);
  90.     }
  91.   }

  92.   /**
  93.    * Check for problem and throw it if found.
  94.    */
  95.   public void throwOnTrouble() {
  96.     if (pw.checkError())
  97.       throw new TroubleException();
  98.   }

  99.   /**
  100.    * Delegated method.
  101.    * {@inheritDoc}
  102.    * @see java.io.PrintWriter#flush()
  103.    */
  104.   public void flush() {
  105.     pw.flush();
  106.     throwOnTrouble();
  107.   }

  108.   /**
  109.    * Delegated method.
  110.    * {@inheritDoc}
  111.    * @see java.io.PrintWriter#close()
  112.    */
  113.   public void close() {
  114.     pw.close();
  115.     throwOnTrouble();
  116.   }

  117.   /**
  118.    * Delegated method.
  119.    * {@inheritDoc}
  120.    * @see java.io.PrintWriter#checkError()
  121.    */
  122.   public boolean checkError() {
  123.     return pw.checkError();
  124.   }

  125.   /**
  126.    * Delegated method.
  127.    * {@inheritDoc}
  128.    * @see java.io.PrintWriter#write(int)
  129.    */
  130.   public void write(int c) {
  131.     pw.write(c);
  132.     throwOnTrouble();
  133.   }

  134.   /**
  135.    * Delegated method.
  136.    * {@inheritDoc}
  137.    * @see java.io.PrintWriter#write(char[], int, int)
  138.    */
  139.   public void write(char buf[], int off, int len) {
  140.     pw.write(buf, off, len);
  141.     throwOnTrouble();
  142.   }

  143.   /**
  144.    * Delegated method.
  145.    * {@inheritDoc}
  146.    * @see java.io.PrintWriter#write(java.lang.String, int, int)
  147.    */
  148.   public void write(String buf, int off, int len) {
  149.     pw.write(buf, off, len);
  150.     throwOnTrouble();
  151.   }

  152.   /**
  153.    * Delegated method.
  154.    * {@inheritDoc}
  155.    * @see java.io.PrintWriter#println()
  156.    */
  157.   public void println() {
  158.     pw.println();
  159.     throwOnTrouble();
  160.   }
  161. }