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

  1. /*
  2.  * @(#)CharArrayReader.java    1.7 97/01/22
  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.  * This class implements a character buffer that can be used as a
  27.  * character-input stream.
  28.  *
  29.  * @author    Herb Jellinek
  30.  * @version     1.7, 01/22/97
  31.  * @since       JDK1.1
  32.  */
  33. public
  34. class CharArrayReader extends Reader {
  35.     /** Character buffer */
  36.     protected char buf[];
  37.  
  38.     /** Current buffer position */
  39.     protected int pos;
  40.  
  41.     /** Position of mark in buffer */
  42.     protected int markedPos = 0;
  43.  
  44.     /** Number of valid characters in buffer */
  45.     protected int count;
  46.  
  47.     /**
  48.      * Create an CharArrayReader from the specified array of chars.
  49.      * @param buf    Input buffer (not copied)
  50.      * @since JDK1.1
  51.      */
  52.     public CharArrayReader(char buf[]) {
  53.     this.buf = buf;
  54.         this.pos = 0;
  55.     this.count = buf.length;
  56.     }
  57.  
  58.     /**
  59.      * Create an CharArrayReader from the specified array of chars.
  60.      * @param buf    Input buffer (not copied)
  61.      * @param offset    Offset of the first char to read
  62.      * @param length    Number of chars to read
  63.      * @since JDK1.1
  64.      */
  65.     public CharArrayReader(char buf[], int offset, int length) {
  66.     this.buf = buf;
  67.         this.pos = offset;
  68.     this.count = Math.min(offset + length, buf.length);
  69.     }
  70.  
  71.     /** Check to make sure that the stream has not been closed */
  72.     private void ensureOpen() throws IOException {
  73.     if (buf == null)
  74.         throw new IOException("Stream closed");
  75.     }
  76.  
  77.     /**
  78.      * Read a single character.
  79.      * 
  80.      * @exception   IOException  If an I/O error occurs
  81.      * @since       JDK1.1
  82.      */
  83.     public int read() throws IOException {
  84.     synchronized (lock) {
  85.         ensureOpen();
  86.         if (pos >= count)
  87.         return -1;
  88.         else
  89.         return buf[pos++];
  90.     }
  91.     }
  92.  
  93.     /**
  94.      * Read characters into a portion of an array.
  95.      * @param b     Destination buffer
  96.      * @param off  Offset at which to start storing characters
  97.      * @param len   Maximum number of characters to read
  98.      * @return  The actual number of characters read, or -1 if
  99.      *         the end of the stream has been reached
  100.      * 
  101.      * @exception   IOException  If an I/O error occurs
  102.      * @since       JDK1.1
  103.      */
  104.     public int read(char b[], int off, int len) throws IOException {
  105.     synchronized (lock) {
  106.         ensureOpen();
  107.         if (pos >= count) {
  108.         return -1;
  109.         }
  110.         if (pos + len > count) {
  111.         len = count - pos;
  112.         }
  113.         if (len <= 0) {
  114.         return 0;
  115.         }
  116.         System.arraycopy(buf, pos, b, off, len);
  117.         pos += len;
  118.         return len;
  119.     }
  120.     }
  121.  
  122.     /**
  123.      * Skip characters.
  124.      * @param n The number of characters to skip
  125.      * @return    The number of characters actually skipped
  126.      * 
  127.      * @exception   IOException  If an I/O error occurs
  128.      * @since       JDK1.1
  129.      */
  130.     public long skip(long n) throws IOException {
  131.     synchronized (lock) {
  132.         ensureOpen();
  133.         if (pos + n > count) {
  134.         n = count - pos;
  135.         }
  136.         if (n < 0) {
  137.         return 0;
  138.         }
  139.         pos += n;
  140.         return n;
  141.     }
  142.     }
  143.  
  144.     /**
  145.      * Tell whether this stream is ready to be read.  Character-array readers
  146.      * are always ready to be read.
  147.      *
  148.      * @exception  IOException  If an I/O error occurs
  149.      * @since       JDK1.1
  150.      */
  151.     public boolean ready() throws IOException {
  152.     synchronized (lock) {
  153.         ensureOpen();
  154.         return (count - pos) > 0;
  155.     }
  156.     }
  157.  
  158.     /**
  159.      * Tell whether this stream supports the mark() operation, which it does.
  160.      * @since       JDK1.1
  161.      */
  162.     public boolean markSupported() {
  163.     return true;
  164.     }
  165.  
  166.     /**
  167.      * Mark the present position in the stream.  Subsequent calls to reset()
  168.      * will reposition the stream to this point.
  169.      *
  170.      * @param  readAheadLimit  Limit on the number of characters that may be
  171.      *                         read while still preserving the mark.  Because
  172.      *                         the stream's input comes from a character array,
  173.      *                         there is no actual limit; hence this argument is
  174.      *                         ignored.
  175.      *
  176.      * @exception  IOException  If an I/O error occurs
  177.      * @since       JDK1.1
  178.      */
  179.     public void mark(int readAheadLimit) throws IOException {
  180.     synchronized (lock) {
  181.         ensureOpen();
  182.         markedPos = pos;
  183.     }
  184.     }
  185.  
  186.     /**
  187.      * Reset the stream to the most recent mark, or to the beginning if it has
  188.      * never been marked.
  189.      *
  190.      * @exception  IOException  If an I/O error occurs
  191.      * @since       JDK1.1
  192.      */
  193.     public void reset() throws IOException {
  194.     synchronized (lock) {
  195.         ensureOpen();
  196.         pos = markedPos;
  197.     }
  198.     }
  199.  
  200.     /**
  201.      * Close the stream.
  202.      * @since       JDK1.1
  203.      */
  204.     public void close() {
  205.     buf = null;
  206.     }
  207. }
  208.