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

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