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

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