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

  1. /*
  2.  * @(#)ByteArrayOutputStream.java    1.30 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. /**
  19.  * This class implements an output stream in which the data is 
  20.  * written into a byte array. The buffer automatically grows as data 
  21.  * is written to it. 
  22.  * The data can be retrieved using <code>toByteArray()</code> and
  23.  * <code>toString()</code>.
  24.  *
  25.  * @author  Arthur van Hoff
  26.  * @version 1.27, 10/20/97
  27.  * @since   JDK1.0
  28.  */
  29.  
  30. public class ByteArrayOutputStream extends OutputStream {
  31.  
  32.     /** 
  33.      * The buffer where data is stored. 
  34.      */
  35.     protected byte buf[];
  36.  
  37.     /**
  38.      * The number of valid bytes in the buffer. 
  39.      */
  40.     protected int count;
  41.  
  42.     /**
  43.      * Creates a new byte array output stream. The buffer capacity is 
  44.      * initially 32 bytes, though its size increases if necessary. 
  45.      */
  46.     public ByteArrayOutputStream() {
  47.     this(32);
  48.     }
  49.  
  50.     /**
  51.      * Creates a new byte array output stream, with a buffer capacity of 
  52.      * the specified size, in bytes. 
  53.      *
  54.      * @param   size   the initial size.
  55.      */
  56.     public ByteArrayOutputStream(int size) {
  57.     buf = new byte[size];
  58.     }
  59.  
  60.     /**
  61.      * Writes the specified byte to this byte array output stream. 
  62.      *
  63.      * @param   b   the byte to be written.
  64.      */
  65.     public synchronized void write(int b) {
  66.     int newcount = count + 1;
  67.     if (newcount > buf.length) {
  68.         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  69.         System.arraycopy(buf, 0, newbuf, 0, count);
  70.         buf = newbuf;
  71.     }
  72.     buf[count] = (byte)b;
  73.     count = newcount;
  74.     }
  75.  
  76.     /**
  77.      * Writes <code>len</code> bytes from the specified byte array 
  78.      * starting at offset <code>off</code> to this byte array output stream.
  79.      *
  80.      * @param   b     the data.
  81.      * @param   off   the start offset in the data.
  82.      * @param   len   the number of bytes to write.
  83.      */
  84.     public synchronized void write(byte b[], int off, int len) {
  85.     int newcount = count + len;
  86.     
  87.     if (b == null) {
  88.             throw new NullPointerException();
  89.         } else if (off < 0 || len < 0 || off + len > b.length) {
  90.             throw new IndexOutOfBoundsException();
  91.         } else if (len == 0) {
  92.             return;
  93.         }
  94.  
  95.     if (newcount > buf.length) {
  96.         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  97.         System.arraycopy(buf, 0, newbuf, 0, count);
  98.         buf = newbuf;
  99.     }
  100.     System.arraycopy(b, off, buf, count, len);
  101.     count = newcount;
  102.     }
  103.  
  104.     /**
  105.      * Writes the complete contents of this byte array output stream to 
  106.      * the specified output stream argument, as if by calling the output 
  107.      * stream's write method using <code>out.write(buf, 0, count)</code>.
  108.      *
  109.      * @param      out   the output stream to which to write the data.
  110.      * @exception  IOException  if an I/O error occurs.
  111.      */
  112.     public synchronized void writeTo(OutputStream out) throws IOException {
  113.     out.write(buf, 0, count);
  114.     }
  115.  
  116.     /**
  117.      * Resets the <code>count</code> field of this byte array output 
  118.      * stream to zero, so that all currently accumulated output in the 
  119.      * ouput stream is discarded. The output stream can be used again, 
  120.      * reusing the already allocated buffer space. 
  121.      *
  122.      * @see     java.io.ByteArrayInputStream#count
  123.      */
  124.     public synchronized void reset() {
  125.     count = 0;
  126.     }
  127.  
  128.     /**
  129.      * Creates a newly allocated byte array. Its size is the current 
  130.      * size of this output stream and the valid contents of the buffer 
  131.      * have been copied into it. 
  132.      *
  133.      * @return  the current contents of this output stream, as a byte array.
  134.      * @see     java.io.ByteArrayOutputStream#size()
  135.      */
  136.     public synchronized byte toByteArray()[] {
  137.     byte newbuf[] = new byte[count];
  138.     System.arraycopy(buf, 0, newbuf, 0, count);
  139.     return newbuf;
  140.     }
  141.  
  142.     /**
  143.      * Returns the current size of the buffer.
  144.      *
  145.      * @return  the value of the <code>count</code> field, which is the number
  146.      *          of valid bytes in this output stream.
  147.      * @see     java.io.ByteArrayOutputStream#count
  148.      */
  149.     public int size() {
  150.     return count;
  151.     }
  152.  
  153.     /**
  154.      * Converts the buffer's contents into a string, translating bytes into
  155.      * characters according to the platform's default character encoding.
  156.      */
  157.     public String toString() {
  158.     return new String(buf, 0, count);
  159.     }
  160.  
  161.     /**
  162.      * Converts the buffer's contents into a string, translating bytes into
  163.      * characters according to the specified character encoding.
  164.      *
  165.      * @param   enc  a character-encoding name.
  166.      * @since   JDK1.1
  167.      */
  168.     public String toString(String enc) throws UnsupportedEncodingException {
  169.     return new String(buf, 0, count, enc);
  170.     }
  171.  
  172.     /**
  173.      * Creates a newly allocated string. Its size is the current size of 
  174.      * the output stream and the valid contents of the buffer have been 
  175.      * copied into it. Each character <i>c</i> in the resulting string is 
  176.      * constructed from the corresponding element <i>b</i> in the byte 
  177.      * array such that:
  178.      * <blockquote><pre>
  179.      *     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
  180.      * </pre></blockquote>
  181.      *
  182.      * @deprecated This method does not properly convert bytes into characters.
  183.      * As of JDK 1.1, the preferred way to do this is via the
  184.      * <code>toString(String enc)</code> method, which takes an encoding-name
  185.      * argument, or the <code>toString()</code> method, which uses the
  186.      * platform's default character encoding.
  187.      *
  188.      * @param      hibyte    the high byte of each resulting Unicode character.
  189.      * @return     the current contents of the output stream, as a string.
  190.      * @see        java.io.ByteArrayOutputStream#size()
  191.      * @see        java.io.ByteArrayOutputStream#toString(String)
  192.      * @see        java.io.ByteArrayOutputStream#toString()
  193.      */
  194.     public String toString(int hibyte) {
  195.     return new String(buf, hibyte, 0, count);
  196.     }
  197.  
  198. }
  199.