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

  1. /*
  2.  * @(#)PipedReader.java    1.5 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 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.  
  26. /**
  27.  * Piped character-input streams.
  28.  *
  29.  * @version     1.5, 97/01/27
  30.  * @author    Mark Reinhold
  31.  * @since    JDK1.1
  32.  */
  33.  
  34. public class PipedReader extends Reader {
  35.  
  36.     PipedInputStream byteSink;
  37.  
  38.     private byte buf[];        /* Conversion buffer */
  39.     private int leftOver = 0;
  40.  
  41.     /**
  42.      * Create a reader that is not yet connected to a piped writer.
  43.      */
  44.     public PipedReader() {
  45.     byteSink = new PipedInputStream();
  46.     lock = byteSink;
  47.     }
  48.  
  49.     /**
  50.      * Create a reader for the specified piped character-output stream.
  51.      */
  52.     public PipedReader(PipedWriter src) throws IOException {
  53.     this();
  54.     connect(src);
  55.     }
  56.  
  57.     /** Check to make sure that the stream has not been closed */
  58.     private void ensureOpen() throws IOException {
  59.     if (byteSink == null)
  60.         throw new IOException("Stream closed");
  61.     }
  62.  
  63.     /**
  64.      * Connect the specified piped writer to this reader.
  65.      *
  66.      * @exception  IOException  If this reader is already connected
  67.      */
  68.     public void connect(PipedWriter src) throws IOException {
  69.     synchronized (lock) {
  70.         ensureOpen();
  71.         src.connect(this);
  72.     }
  73.     }
  74.  
  75.     /**
  76.      * Read characters into a portion of an array.
  77.      *
  78.      * @param      cbuf  Destination buffer
  79.      * @param      off   Offset at which to start storing characters
  80.      * @param      len   Maximum number of characters to read
  81.      *
  82.      * @return     The number of characters read, or -1 if the end of the
  83.      *             stream has been reached
  84.      *
  85.      * @exception  IOException  If an I/O error occurs
  86.      */
  87.     public int read(char cbuf[], int off, int len) throws IOException {
  88.     synchronized (lock) {
  89.         ensureOpen();
  90.  
  91.         int blen = leftOver + len * 2;
  92.         if ((buf == null) || (buf.length < blen))
  93.         buf = new byte[blen];
  94.         int nb = byteSink.read(buf, leftOver, blen);
  95.         if (nb < 0)
  96.         return -1;
  97.         nb += leftOver;
  98.         for (int i = 0; i < nb; i += 2)
  99.         cbuf[i >> 1] = (char) (((buf[i] & 0xff) << 8)
  100.                        | (buf[i + 1] & 0xff));
  101.         if (nb % 2 != 0) {
  102.         buf[0] = buf[nb - 1];
  103.         leftOver = 1;
  104.         }
  105.         else
  106.         leftOver = 0;
  107.         return nb / 2;
  108.     }
  109.     }
  110.  
  111.     /**
  112.      * Close the stream.
  113.      *
  114.      * @exception  IOException  If an I/O error occurs
  115.      */
  116.     public void close() throws IOException {
  117.     synchronized (lock) {
  118.         if (byteSink == null)
  119.         return;
  120.         byteSink.close();
  121.         byteSink = null;
  122.     }
  123.     }
  124.  
  125. }
  126.