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

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