home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / CharArrayReader.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.7 KB  |  191 lines

  1. /*
  2.  * @(#)CharArrayReader.java    1.10 98/03/18
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class implements a character buffer that can be used as a
  19.  * character-input stream.
  20.  *
  21.  * @author    Herb Jellinek
  22.  * @version     1.10, 03/18/98
  23.  * @since       JDK1.1
  24.  */
  25. public
  26. class CharArrayReader extends Reader {
  27.     /** Character buffer */
  28.     protected char buf[];
  29.  
  30.     /** Current buffer position */
  31.     protected int pos;
  32.  
  33.     /** Position of mark in buffer */
  34.     protected int markedPos = 0;
  35.  
  36.     /** Number of valid characters in buffer */
  37.     protected int count;
  38.  
  39.     /**
  40.      * Create an CharArrayReader from the specified array of chars.
  41.      * @param buf    Input buffer (not copied)
  42.      */
  43.     public CharArrayReader(char buf[]) {
  44.     this.buf = buf;
  45.         this.pos = 0;
  46.     this.count = buf.length;
  47.     }
  48.  
  49.     /**
  50.      * Create an CharArrayReader from the specified array of chars.
  51.      * @param buf    Input buffer (not copied)
  52.      * @param offset    Offset of the first char to read
  53.      * @param length    Number of chars to read
  54.      */
  55.     public CharArrayReader(char buf[], int offset, int length) {
  56.     this.buf = buf;
  57.         this.pos = offset;
  58.     this.count = Math.min(offset + length, buf.length);
  59.         this.markedPos = offset;
  60.     }
  61.  
  62.     /** Check to make sure that the stream has not been closed */
  63.     private void ensureOpen() throws IOException {
  64.     if (buf == null)
  65.         throw new IOException("Stream closed");
  66.     }
  67.  
  68.     /**
  69.      * Read a single character.
  70.      * 
  71.      * @exception   IOException  If an I/O error occurs
  72.      */
  73.     public int read() throws IOException {
  74.     synchronized (lock) {
  75.         ensureOpen();
  76.         if (pos >= count)
  77.         return -1;
  78.         else
  79.         return buf[pos++];
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * Read characters into a portion of an array.
  85.      * @param b     Destination buffer
  86.      * @param off  Offset at which to start storing characters
  87.      * @param len   Maximum number of characters to read
  88.      * @return  The actual number of characters read, or -1 if
  89.      *         the end of the stream has been reached
  90.      * 
  91.      * @exception   IOException  If an I/O error occurs
  92.      */
  93.     public int read(char b[], int off, int len) throws IOException {
  94.     synchronized (lock) {
  95.         ensureOpen();
  96.         if (pos >= count) {
  97.         return -1;
  98.         }
  99.         if (pos + len > count) {
  100.         len = count - pos;
  101.         }
  102.         if (len <= 0) {
  103.         return 0;
  104.         }
  105.         System.arraycopy(buf, pos, b, off, len);
  106.         pos += len;
  107.         return len;
  108.     }
  109.     }
  110.  
  111.     /**
  112.      * Skip characters.
  113.      * @param n The number of characters to skip
  114.      * @return    The number of characters actually skipped
  115.      * 
  116.      * @exception   IOException  If an I/O error occurs
  117.      */
  118.     public long skip(long n) throws IOException {
  119.     synchronized (lock) {
  120.         ensureOpen();
  121.         if (pos + n > count) {
  122.         n = count - pos;
  123.         }
  124.         if (n < 0) {
  125.         return 0;
  126.         }
  127.         pos += n;
  128.         return n;
  129.     }
  130.     }
  131.  
  132.     /**
  133.      * Tell whether this stream is ready to be read.  Character-array readers
  134.      * are always ready to be read.
  135.      *
  136.      * @exception  IOException  If an I/O error occurs
  137.      */
  138.     public boolean ready() throws IOException {
  139.     synchronized (lock) {
  140.         ensureOpen();
  141.         return (count - pos) > 0;
  142.     }
  143.     }
  144.  
  145.     /**
  146.      * Tell whether this stream supports the mark() operation, which it does.
  147.      */
  148.     public boolean markSupported() {
  149.     return true;
  150.     }
  151.  
  152.     /**
  153.      * Mark the present position in the stream.  Subsequent calls to reset()
  154.      * will reposition the stream to this point.
  155.      *
  156.      * @param  readAheadLimit  Limit on the number of characters that may be
  157.      *                         read while still preserving the mark.  Because
  158.      *                         the stream's input comes from a character array,
  159.      *                         there is no actual limit; hence this argument is
  160.      *                         ignored.
  161.      *
  162.      * @exception  IOException  If an I/O error occurs
  163.      */
  164.     public void mark(int readAheadLimit) throws IOException {
  165.     synchronized (lock) {
  166.         ensureOpen();
  167.         markedPos = pos;
  168.     }
  169.     }
  170.  
  171.     /**
  172.      * Reset the stream to the most recent mark, or to the beginning if it has
  173.      * never been marked.
  174.      *
  175.      * @exception  IOException  If an I/O error occurs
  176.      */
  177.     public void reset() throws IOException {
  178.     synchronized (lock) {
  179.         ensureOpen();
  180.         pos = markedPos;
  181.     }
  182.     }
  183.  
  184.     /**
  185.      * Close the stream.
  186.      */
  187.     public void close() {
  188.     buf = null;
  189.     }
  190. }
  191.