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

  1. /*
  2.  * @(#)StringBufferInputStream.java    1.16 97/01/25
  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 allows an application to create an input stream in 
  27.  * which the bytes read are supplied by the contents of a string. 
  28.  * Applications can also read bytes from a byte array by using a 
  29.  * <code>ByteArrayInputStream</code>. 
  30.  * <p>
  31.  * Only the low eight bits of each character in the string are used by
  32.  * this class. 
  33.  *
  34.  * @author     Arthur van Hoff
  35.  * @version    1.16, 01/25/97
  36.  * @see        java.io.ByteArrayInputStream
  37.  * @see        java.io.StringReader
  38.  * @since      JDK1.0
  39.  * @deprecated This class does not properly convert characters into bytes.  As
  40.  *             of JDK 1.1, the preferred way to create a stream from a
  41.  *             string is via the <code>StringReader</code> class.
  42.  */
  43. public
  44. class StringBufferInputStream extends InputStream {
  45.     /**
  46.      * The string from which bytes are read. 
  47.      *
  48.      * @since      JDK1.0
  49.      */
  50.     protected String buffer;
  51.  
  52.     /**
  53.      * The index of the next character to read from the input stream buffer.
  54.      *
  55.      * @see        java.io.StringBufferInputStream#buffer
  56.      * @since      JDK1.0
  57.      */
  58.     protected int pos;
  59.  
  60.     /**
  61.      * The number of valid characters in the input stream buffer. 
  62.      *
  63.      * @see        java.io.StringBufferInputStream#buffer
  64.      * @since      JDK1.0
  65.      */
  66.     protected int count;
  67.  
  68.     /**
  69.      * Creates a string input stream to read data from the specified string.
  70.      *
  71.      * @param      s   the underlying input buffer.
  72.      * @since      JDK1.0
  73.      */
  74.     public StringBufferInputStream(String s) {
  75.     this.buffer = s;
  76.     count = s.length();
  77.     }
  78.  
  79.     /**
  80.      * Reads the next byte of data from this input stream. The value 
  81.      * byte is returned as an <code>int</code> in the range 
  82.      * <code>0</code> to <code>255</code>. If no byte is available 
  83.      * because the end of the stream has been reached, the value 
  84.      * <code>-1</code> is returned. 
  85.      * <p>
  86.      * The <code>read</code> method of 
  87.      * <code>StringBufferInputStream</code> cannot block. It returns the 
  88.      * low eight bits of the next character in this input stream's buffer. 
  89.      *
  90.      * @return     the next byte of data, or <code>-1</code> if the end of the
  91.      *             stream is reached.
  92.      * @since      JDK1.0
  93.      */
  94.     public synchronized int read() {
  95.     return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  96.     }
  97.  
  98.     /**
  99.      * Reads up to <code>len</code> bytes of data from this input stream 
  100.      * into an array of bytes. 
  101.      * <p>
  102.      * The <code>read</code> method of 
  103.      * <code>StringBufferInputStream</code> cannot block. It copies the 
  104.      * low eight bits from the characters in this input stream's buffer into 
  105.      * the byte array argument. 
  106.      *
  107.      * @param      b     the buffer into which the data is read.
  108.      * @param      off   the start offset of the data.
  109.      * @param      len   the maximum number of bytes read.
  110.      * @return     the total number of bytes read into the buffer, or
  111.      *             <code>-1</code> if there is no more data because the end of
  112.      *             the stream has been reached.
  113.      * @since      JDK1.0
  114.      */
  115.     public synchronized int read(byte b[], int off, int len) {
  116.     if (pos >= count) {
  117.         return -1;
  118.     }
  119.     if (pos + len > count) {
  120.         len = count - pos;
  121.     }
  122.     if (len <= 0) {
  123.         return 0;
  124.     }
  125.     String    s = buffer;
  126.     int cnt = len;
  127.     while (--cnt >= 0) {
  128.         b[off++] = (byte)s.charAt(pos++);
  129.     }
  130.  
  131.     return len;
  132.     }
  133.  
  134.     /**
  135.      * Skips <code>n</code> bytes of input from this input stream. Fewer 
  136.      * bytes might be skipped if the end of the input stream is reached. 
  137.      *
  138.      * @param      n   the number of bytes to be skipped.
  139.      * @return     the actual number of bytes skipped.
  140.      * @since      JDK1.0
  141.      */
  142.     public synchronized long skip(long n) {
  143.     if (n < 0) {
  144.         return 0;
  145.     }
  146.     if (n > count - pos) {
  147.         n = count - pos;
  148.     }
  149.     pos += n;
  150.     return n;
  151.     }
  152.  
  153.     /**
  154.      * Returns the number of bytes that can be read from the input 
  155.      * stream without blocking. 
  156.      *
  157.      * @return     the value of <code>count - pos</code>, which is the
  158.      *             number of bytes remaining to be read from the input buffer. 
  159.      * @since      JDK1.0
  160.      */
  161.     public synchronized int available() {
  162.     return count - pos;
  163.     }
  164.  
  165.     /**
  166.      * Resets the input stream to begin reading from the first character 
  167.      * of this input stream's underlying buffer. 
  168.      *
  169.      * @since      JDK1.0
  170.      */
  171.     public synchronized void reset() {
  172.     pos = 0;
  173.     }
  174. }
  175.