home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / OutputStreamWriter.java < prev    next >
Text File  |  1997-05-20  |  7KB  |  252 lines

  1. /*
  2.  * @(#)OutputStreamWriter.java    1.10 97/01/27
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.io;
  24.  
  25. import sun.io.CharToByteConverter;
  26. import sun.io.ConversionBufferFullException;
  27.  
  28.  
  29. /**
  30.  * Write characters to an output stream, translating characters into bytes
  31.  * according to a specified character encoding.  Each OutputStreamWriter
  32.  * incorporates its own CharToByteConverter, and is thus a bridge from
  33.  * character streams to byte streams.
  34.  *
  35.  * <p> The encoding used by an OutputStreamWriter may be specified by name, by
  36.  * providing a CharToByteConverter, or by accepting the default encoding, which
  37.  * is defined by the system property <tt>file.encoding</tt>.
  38.  *
  39.  * <p> Each invocation of a write() method causes the encoding converter to be
  40.  * invoked on the given character(s).  The resulting bytes are accumulated in a
  41.  * buffer before being written to the underlying output stream.  The size of
  42.  * this buffer may be specified, but by default it is large enough for most
  43.  * purposes.  Note that the characters passed to the write() methods are not
  44.  * buffered.  For top efficiency, consider wrapping an OutputStreamWriter
  45.  * within a BufferedWriter so as to avoid frequent converter invocations.  For
  46.  * example,
  47.  *
  48.  * <pre>
  49.  * Writer out
  50.  *   = new BufferedWriter(new OutputStreamWriter(System.out));
  51.  * </pre>
  52.  *
  53.  * @see BufferedWriter
  54.  * @see OutputStream
  55.  *
  56.  * @version     1.10, 97/01/27
  57.  * @author    Mark Reinhold
  58.  * @since    JDK1.1
  59.  */
  60.  
  61. public class OutputStreamWriter extends Writer {
  62.  
  63.     private CharToByteConverter ctb;
  64.     private OutputStream out;
  65.  
  66.     private static final int defaultByteBufferSize = 8192;
  67.     private byte bb[];
  68.     private int nextByte = 0;
  69.     private int nBytes = 0;
  70.  
  71.     /**
  72.      * Create an OutputStreamWriter that uses the named character encoding.
  73.      *
  74.      * @param  out  An OutputStream
  75.      * @param  enc  Name of the encoding to be used
  76.      *
  77.      * @exception  UnsupportedEncodingException
  78.      *             If the named encoding is not supported
  79.      */
  80.     public OutputStreamWriter(OutputStream out, String enc)
  81.     throws UnsupportedEncodingException
  82.     {
  83.     this(out, CharToByteConverter.getConverter(enc));
  84.     }
  85.  
  86.     /**
  87.      * Create an OutputStreamWriter that uses the default character encoding.
  88.      *
  89.      * @param  out  An OutputStream
  90.      */
  91.     public OutputStreamWriter(OutputStream out) {
  92.     this(out, CharToByteConverter.getDefault());
  93.     }
  94.  
  95.     /**
  96.      * Create an OutputStreamWriter that uses the specified character-to-byte
  97.      * converter.  The converter is assumed to have been reset.
  98.      *
  99.      * @param  out  An OutputStream
  100.      * @param  ctb  A CharToByteConverter
  101.      */
  102.     private OutputStreamWriter(OutputStream out, CharToByteConverter ctb) {
  103.     super(out);
  104.     this.out = out;
  105.     this.ctb = ctb;
  106.     bb = new byte[defaultByteBufferSize];
  107.     nBytes = defaultByteBufferSize;
  108.     }
  109.  
  110.     /**
  111.      * Return the name of the encoding being used by this stream.  May return
  112.      * null if the stream has been closed.
  113.      */
  114.     public String getEncoding() {
  115.     synchronized (lock) {
  116.         if (ctb != null)
  117.         return ctb.getCharacterEncoding();
  118.         else
  119.         return null;
  120.     }
  121.     }
  122.  
  123.     /** Check to make sure that the stream has not been closed */
  124.     private void ensureOpen() throws IOException {
  125.     if (out == null)
  126.         throw new IOException("Stream closed");
  127.     }
  128.  
  129.     /**
  130.      * Write a single character.
  131.      *
  132.      * @exception  IOException  If an I/O error occurs
  133.      */
  134.     public void write(int c) throws IOException {
  135.     char cbuf[] = new char[1];
  136.     cbuf[0] = (char) c;
  137.     write(cbuf, 0, 1);
  138.     }
  139.  
  140.     /**
  141.      * Write a portion of an array of characters.
  142.      *
  143.      * @param  cbuf  Buffer of characters
  144.      * @param  off   Offset from which to start writing characters
  145.      * @param  len   Number of characters to write
  146.      *
  147.      * @exception  IOException  If an I/O error occurs
  148.      */
  149.     public void write(char cbuf[], int off, int len) throws IOException {
  150.     synchronized (lock) {
  151.         ensureOpen();
  152.  
  153.         int ci = off, end = off + len;
  154.         while (ci < end) {
  155.         boolean bufferFull = false;
  156.  
  157.         try {
  158.             nextByte += ctb.convert(cbuf, ci, end,
  159.                         bb, nextByte, nBytes);
  160.             ci = end;
  161.         }
  162.         catch (ConversionBufferFullException x) {
  163.             int nci = ctb.nextCharIndex();
  164.             if (nci == ci) {
  165.             /* Buffer doesn't even hold one character */
  166.             throw new CharConversionException("Output buffer too small");
  167.             }
  168.             ci = nci;
  169.             bufferFull = true;
  170.             nextByte = ctb.nextByteIndex();
  171.         }
  172.  
  173.         if ((nextByte >= nBytes) || bufferFull) {
  174.             out.write(bb, 0, nextByte);
  175.             nextByte = 0;
  176.         }
  177.         }
  178.     }
  179.     }
  180.  
  181.     /**
  182.      * Write a portion of a string.
  183.      *
  184.      * @param  str  A String
  185.      * @param  off  Offset from which to start writing characters
  186.      * @param  len  Number of characters to write
  187.      *
  188.      * @exception  IOException  If an I/O error occurs
  189.      */
  190.     public void write(String str, int off, int len) throws IOException {
  191.     char cbuf[] = new char[len];
  192.     str.getChars(off, off + len, cbuf, 0);
  193.     write(cbuf, 0, len);
  194.     }
  195.  
  196.     /**
  197.      * Flush the output buffer to the underlying byte stream, without flushing
  198.      * the byte stream itself.  This method is non-private only so that it may
  199.      * be invoked by PrintStream.
  200.      */
  201.     void flushBuffer() throws IOException {
  202.     synchronized (lock) {
  203.         ensureOpen();
  204.  
  205.         for (;;) {
  206.         try {
  207.             nextByte += ctb.flush(bb, nextByte, nBytes);
  208.         }
  209.         catch (ConversionBufferFullException x) {
  210.             nextByte = ctb.nextByteIndex();
  211.         }
  212.         if (nextByte == 0)
  213.             break;
  214.         if (nextByte > 0) {
  215.             out.write(bb, 0, nextByte);
  216.             nextByte = 0;
  217.         }
  218.         }
  219.     }
  220.     }
  221.  
  222.     /**
  223.      * Flush the stream.
  224.      *
  225.      * @exception  IOException  If an I/O error occurs
  226.      */
  227.     public void flush() throws IOException {
  228.     synchronized (lock) {
  229.         flushBuffer();
  230.         out.flush();
  231.     }
  232.     }
  233.  
  234.     /**
  235.      * Close the stream.
  236.      *
  237.      * @exception  IOException  If an I/O error occurs
  238.      */
  239.     public void close() throws IOException {
  240.     synchronized (lock) {
  241.         if (out == null)
  242.         return;
  243.         flush();
  244.         out.close();
  245.         out = null;
  246.         bb = null;
  247.         ctb = null;
  248.     }
  249.     }
  250.  
  251. }
  252.