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

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