home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / ByteArrayInputStream.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  129 lines

  1. /*
  2.  * @(#)ByteArrayInputStream.java    1.16 95/08/11 Arthur van Hoff
  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 buffer that can be
  24.  * used as an InputStream. 
  25.  * @version     1.16, 11 Aug 1995
  26.  * @author    Arthur van Hoff
  27.  */
  28. public
  29. class ByteArrayInputStream extends InputStream {
  30.     /**
  31.      * The buffer where data is stored.
  32.      */
  33.     protected byte buf[];
  34.  
  35.     /**
  36.      * The current position in the buffer.
  37.      */
  38.     protected int pos;
  39.  
  40.     /**
  41.      * The number of characters to use in the buffer.
  42.      */
  43.     protected int count;
  44.  
  45.     /**
  46.      * Creates an ByteArrayInputStream from the specified array of bytes.
  47.      * @param buf    the input buffer (not copied)
  48.      */
  49.     public ByteArrayInputStream(byte buf[]) {
  50.     this.buf = buf;
  51.         this.pos = 0;
  52.     this.count = buf.length;
  53.     }
  54.  
  55.     /**
  56.      * Creates an ByteArrayInputStream from the specified array of bytes.
  57.      * @param buf    the input buffer (not copied)
  58.      * @param offset    the offset of the first byte to read
  59.      * @param length    the number of bytes to read
  60.      */
  61.     public ByteArrayInputStream(byte buf[], int offset, int length) {
  62.     this.buf = buf;
  63.         this.pos = offset;
  64.     this.count = Math.min(offset + length, buf.length);
  65.     }
  66.  
  67.     /**
  68.      * Reads a byte of data.
  69.      * @return     the byte read, or -1 if the end of the
  70.      *        stream is reached.
  71.      */
  72.     public synchronized int read() {
  73.     return (pos < count) ? (buf[pos++] & 0xff) : -1;
  74.     }
  75.  
  76.     /**
  77.      * Reads into an array of bytes.
  78.      * @param b    the buffer into which the data is read
  79.      * @param off the start offset of the data
  80.      * @param len the maximum number of bytes read
  81.      * @return  the actual number of bytes read; -1 is
  82.      *         returned when the end of the stream is reached.
  83.      */
  84.     public synchronized int read(byte b[], int off, int len) {
  85.     if (pos >= count) {
  86.         return -1;
  87.     }
  88.     if (pos + len > count) {
  89.         len = count - pos;
  90.     }
  91.     if (len <= 0) {
  92.         return 0;
  93.     }
  94.     System.arraycopy(buf, pos, b, off, len);
  95.     pos += len;
  96.     return len;
  97.     }
  98.  
  99.     /**
  100.      * Skips n bytes of input.
  101.      * @param n the number of bytes to be skipped
  102.      * @return    the actual number of bytes skipped.
  103.      */
  104.     public synchronized long skip(long n) {
  105.     if (pos + n > count) {
  106.         n = count - pos;
  107.     }
  108.     if (n < 0) {
  109.         return 0;
  110.     }
  111.     pos += n;
  112.     return n;
  113.     }
  114.  
  115.     /**
  116.      * Returns the number of available bytes in the buffer.
  117.      */
  118.     public synchronized int available() {
  119.     return count - pos;
  120.     }
  121.  
  122.     /**
  123.      * Resets the buffer to the beginning.
  124.      */
  125.     public synchronized void reset() {
  126.     pos = 0;
  127.     }
  128. }
  129.