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

  1. /*
  2.  * @(#)ByteArrayInputStream.java    1.24 98/03/18
  3.  *
  4.  * Copyright 1994-1998 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 byte array. 
  20.  * Applications can also read bytes from a string by using a 
  21.  * <code>StringBufferInputStream</code>. 
  22.  *
  23.  * @author  Arthur van Hoff
  24.  * @version 1.24, 03/18/98
  25.  * @see     java.io.StringBufferInputStream
  26.  * @since   JDK1.0
  27.  */
  28. public
  29. class ByteArrayInputStream extends InputStream {
  30.     /**
  31.      * The byte array containing the data. 
  32.      */
  33.     protected byte buf[];
  34.  
  35.     /**
  36.      * The index of the next character to read from the input stream buffer.
  37.      */
  38.     protected int pos;
  39.  
  40.     /**
  41.      * The currently marked position in the stream.
  42.      * ByteArrayInputStreams are marked at position zero by
  43.      * default when constructed.  They may be marked at another
  44.      * position within the buffer by the <code>mark()</code> method.
  45.      * The current buffer position is set to this point by the
  46.      * <code>reset()</code> method.
  47.      *
  48.      * @since   JDK1.1
  49.      */
  50.     protected int mark = 0;
  51.  
  52.     /**
  53.      * The index one greater than the last valid character in the input 
  54.      * stream buffer. 
  55.      */
  56.     protected int count;
  57.  
  58.     /**
  59.      * Creates a new byte array input stream that reads data from the 
  60.      * specified byte array. The byte array is not copied. 
  61.      *
  62.      * @param   buf   the input buffer.
  63.      */
  64.     public ByteArrayInputStream(byte buf[]) {
  65.     this.buf = buf;
  66.         this.pos = 0;
  67.     this.count = buf.length;
  68.     }
  69.  
  70.     /**
  71.      * Creates a new byte array input stream that reads data from the 
  72.      * specified byte array. Up to <code>length</code> characters are to 
  73.      * be read from the byte array, starting at the indicated offset. 
  74.      * <p>
  75.      * The byte array is not copied. 
  76.      *
  77.      * @param   buf      the input buffer.
  78.      * @param   offset   the offset in the buffer of the first byte to read.
  79.      * @param   length   the maximum number of bytes to read from the buffer.
  80.      */
  81.     public ByteArrayInputStream(byte buf[], int offset, int length) {
  82.     this.buf = buf;
  83.         this.pos = offset;
  84.     this.count = Math.min(offset + length, buf.length);
  85.         this.mark = offset;
  86.     }
  87.  
  88.     /**
  89.      * Reads the next byte of data from this input stream. The value 
  90.      * byte is returned as an <code>int</code> in the range 
  91.      * <code>0</code> to <code>255</code>. If no byte is available 
  92.      * because the end of the stream has been reached, the value 
  93.      * <code>-1</code> is returned. 
  94.      * <p>
  95.      * The <code>read</code> method of <code>ByteArrayInputStream</code> 
  96.      * cannot block. 
  97.      *
  98.      * @return  the next byte of data, or <code>-1</code> if the end of the
  99.      *          stream has been reached.
  100.      */
  101.     public synchronized int read() {
  102.     return (pos < count) ? (buf[pos++] & 0xff) : -1;
  103.     }
  104.  
  105.     /**
  106.      * Reads up to <code>len</code> bytes of data into an array of bytes 
  107.      * from this input stream. This <code>read</code> method cannot block. 
  108.      *
  109.      * @param   b     the buffer into which the data is read.
  110.      * @param   off   the start offset of the data.
  111.      * @param   len   the maximum number of bytes read.
  112.      * @return  the total number of bytes read into the buffer, or
  113.      *          <code>-1</code> if there is no more data because the end of
  114.      *          the stream has been reached.
  115.      */
  116.     public synchronized int read(byte b[], int off, int len) {
  117.     if (pos >= count) {
  118.         return -1;
  119.     }
  120.     if (pos + len > count) {
  121.         len = count - pos;
  122.     }
  123.     if (len <= 0) {
  124.         return 0;
  125.     }
  126.     System.arraycopy(buf, pos, b, off, len);
  127.     pos += len;
  128.     return len;
  129.     }
  130.  
  131.     /**
  132.      * Skips <code>n</code> bytes of input from this input stream. Fewer 
  133.      * bytes might be skipped if the end of the input stream is reached. 
  134.      *
  135.      * @param   n   the number of bytes to be skipped.
  136.      * @return  the actual number of bytes skipped.
  137.      */
  138.     public synchronized long skip(long n) {
  139.     if (pos + n > count) {
  140.         n = count - pos;
  141.     }
  142.     if (n < 0) {
  143.         return 0;
  144.     }
  145.     pos += n;
  146.     return n;
  147.     }
  148.  
  149.     /**
  150.      * Returns the number of bytes that can be read from this input 
  151.      * stream without blocking. 
  152.      * <p>
  153.      * The <code>available</code> method of 
  154.      * <code>ByteArrayInputStream</code> returns the value of 
  155.      * <code>count - pos</code>, 
  156.      * which is the number of bytes remaining to be read from the input buffer.
  157.      *
  158.      * @return  the number of bytes that can be read from the input stream
  159.      *          without blocking.
  160.      */
  161.     public synchronized int available() {
  162.     return count - pos;
  163.     }
  164.  
  165.     /**
  166.      * Tests if ByteArrayInputStream supports mark/reset.
  167.      *
  168.      * @since   JDK1.1
  169.      */
  170.     public boolean markSupported() {
  171.     return true;
  172.     }
  173.  
  174.     /**
  175.      * Set the current marked position in the stream.
  176.      * ByteArrayInputStreams are marked at position zero by
  177.      * default when constructed.  They may be marked at another
  178.      * position within the buffer by this method.
  179.      *
  180.      * @since   JDK1.1
  181.      */
  182.     public void mark(int readAheadLimit) {
  183.     mark = pos;
  184.     }
  185.  
  186.     /**
  187.      * Resets the buffer to the marked position.  The marked position
  188.      * is the beginning unless another position was marked.
  189.      */
  190.     public synchronized void reset() {
  191.     pos = mark;
  192.     }
  193. }
  194.