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

  1. /*
  2.  * @(#)StringReader.java    1.8 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. /**
  19.  * A character stream whose source is a string.
  20.  *
  21.  * @version     1.8, 98/03/18
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class StringReader extends Reader {
  27.  
  28.     private String str;
  29.     private int length;
  30.     private int next = 0;
  31.     private int mark = 0;
  32.  
  33.     /**
  34.      * Create a new string reader.
  35.      */
  36.     public StringReader(String s) {
  37.     this.str = s;
  38.     this.length = s.length();
  39.     }
  40.  
  41.     /** Check to make sure that the stream has not been closed */
  42.     private void ensureOpen() throws IOException {
  43.     if (str == null)
  44.         throw new IOException("Stream closed");
  45.     }
  46.  
  47.     /**
  48.      * Read a single character.
  49.      *
  50.      * @return     The character read, or -1 if the end of the stream has been
  51.      *             reached
  52.      *
  53.      * @exception  IOException  If an I/O error occurs
  54.      */
  55.     public int read() throws IOException {
  56.     synchronized (lock) {
  57.         ensureOpen();
  58.         if (next >= length)
  59.         return -1;
  60.         return str.charAt(next++);
  61.     }
  62.     }
  63.  
  64.     /**
  65.      * Read characters into a portion of an array.
  66.      *
  67.      * @param      cbuf  Destination buffer
  68.      * @param      off   Offset at which to start writing characters
  69.      * @param      len   Maximum number of characters to read
  70.      *
  71.      * @return     The number of characters read, or -1 if the end of the
  72.      *             stream has been reached
  73.      *
  74.      * @exception  IOException  If an I/O error occurs
  75.      */
  76.     public int read(char cbuf[], int off, int len) throws IOException {
  77.     synchronized (lock) {
  78.         ensureOpen();
  79.         if (next >= length)
  80.         return -1;
  81.         int n = Math.min(length - next, len);
  82.         str.getChars(next, next + n, cbuf, off);
  83.         next += n;
  84.         return n;
  85.     }
  86.     }
  87.  
  88.     /**
  89.      * Skip characters.
  90.      *
  91.      * @exception  IOException  If an I/O error occurs
  92.      */
  93.     public long skip(long ns) throws IOException {
  94.     synchronized (lock) {
  95.         ensureOpen();
  96.         if (next >= length)
  97.         return 0;
  98.         long n = Math.min(length - next, ns);
  99.         next += n;
  100.         return n;
  101.     }
  102.     }
  103.  
  104.     /**
  105.      * Tell whether this stream is ready to be read.
  106.      *
  107.      * @return True if the next read() is guaranteed not to block for input
  108.      *
  109.      * @exception  IOException  If the stream is closed
  110.      */
  111.     public boolean ready() throws IOException {
  112.         synchronized (lock) {
  113.         ensureOpen();
  114.         return true;
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Tell whether this stream supports the mark() operation, which it does.
  120.      */
  121.     public boolean markSupported() {
  122.     return true;
  123.     }
  124.  
  125.     /**
  126.      * Mark the present position in the stream.  Subsequent calls to reset()
  127.      * will reposition the stream to this point.
  128.      *
  129.      * @param  readAheadLimit  Limit on the number of characters that may be
  130.      *                         read while still preserving the mark.  Because
  131.      *                         the stream's input comes from a string, there
  132.      *                         is no actual limit, so this argument is ignored.
  133.      *
  134.      * @exception  IOException  If an I/O error occurs
  135.      */
  136.     public void mark(int readAheadLimit) throws IOException {
  137.     synchronized (lock) {
  138.         ensureOpen();
  139.         mark = next;
  140.     }
  141.     }
  142.  
  143.     /**
  144.      * Reset the stream to the most recent mark, or to the beginning of the
  145.      * string if it has never been marked.
  146.      *
  147.      * @exception  IOException  If an I/O error occurs
  148.      */
  149.     public void reset() throws IOException {
  150.     synchronized (lock) {
  151.         ensureOpen();
  152.         next = mark;
  153.     }
  154.     }
  155.  
  156.     /**
  157.      * Close the stream.
  158.      */
  159.     public void close() {
  160.     str = null;
  161.     }
  162.  
  163. }
  164.