home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / StringBufferInputStream.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  2.9 KB  |  126 lines

  1. /*
  2.  * @(#)StringBufferInputStream.java    1.11 96/04/02 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * This class implements a String buffer that can be
  24.  * used as an InputStream. 
  25.  * @version     1.11, 02 Apr 1996
  26.  * @author    Arthur van Hoff
  27.  */
  28.  
  29. public
  30. class StringBufferInputStream extends InputStream {
  31.  
  32.     /**
  33.      * The buffer where data is stored.
  34.      */
  35.     protected String buffer;
  36.  
  37.     /**
  38.      * The position in the buffer.
  39.      */
  40.     protected int pos;
  41.  
  42.     /**
  43.      * The number of characters to use in the buffer.
  44.      */
  45.     protected int count;
  46.  
  47.  
  48.     /**
  49.      * Creates an StringBufferInputStream from the specified array of 
  50.      * bytes.
  51.      * @param s    the input buffer (not copied)
  52.      */
  53.     public StringBufferInputStream(String s) {
  54.     this.buffer = s;
  55.     count = s.length();
  56.     }
  57.  
  58.     /**
  59.      * Reads a byte of data.
  60.      * @return     the byte read, or -1 if the end of the
  61.      * stream is reached.
  62.      */
  63.     public synchronized int read() {
  64.     return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  65.     }
  66.  
  67.     /**
  68.      * Reads into an array of bytes.
  69.      * @param b    the buffer into which the data is read
  70.      * @param off the start offset of the data
  71.      * @param len the maximum number of bytes read
  72.      * @return  the actual number of bytes read; -1 is
  73.      * returned when the end of the stream is reached.
  74.      */
  75.     public synchronized int read(byte b[], int off, int len) {
  76.     if (pos >= count) {
  77.         return -1;
  78.     }
  79.     if (pos + len > count) {
  80.         len = count - pos;
  81.     }
  82.     if (len <= 0) {
  83.         return 0;
  84.     }
  85.     String    s = buffer;
  86.     int cnt = len;
  87.     while (--cnt >= 0) {
  88.         b[off++] = (byte)s.charAt(pos++);
  89.     }
  90.  
  91.     return len;
  92.     }
  93.  
  94.     /**
  95.      * Skips n bytes of input.
  96.      * @param n the number of bytes to be skipped
  97.      * @return    the actual number of bytes skipped.
  98.      */
  99.     public synchronized long skip(long n) {
  100.     if (pos + n > count) {
  101.         n = count - pos;
  102.     }
  103.     if (n < 0) {
  104.         return 0;
  105.     }
  106.     pos += n;
  107.     return n;
  108.     }
  109.  
  110.     /**
  111.      * Returns the number of available bytes in the buffer.
  112.      */
  113.     public synchronized int available() {
  114.     return count - pos;
  115.     }
  116.  
  117.     /**
  118.      * Resets the buffer to the beginning.
  119.      */
  120.     public synchronized void reset() {
  121.     pos = 0;
  122.     }
  123. }
  124.  
  125.  
  126.