home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / ByteArrayOutputStream.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  3.7 KB  |  139 lines

  1. /*
  2.  * @(#)ByteArrayOutputStream.java    1.16 95/12/18 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 OutputStream. The buffer automatically
  25.  * grows when data is written to the stream.
  26.  * The data can be retrieved using toByteArray() and
  27.  * toString().
  28.  * @version     1.16, 18 Dec 1995
  29.  * @author    Arthur van Hoff
  30.  */
  31. public
  32. class ByteArrayOutputStream extends OutputStream {
  33.     /** 
  34.      * The buffer where data is stored.
  35.      */
  36.     protected byte buf[];
  37.  
  38.     /**
  39.      * The number of bytes in the buffer.
  40.      */
  41.     protected int count;
  42.  
  43.     /**
  44.      * Creates a new ByteArrayOutputStream.
  45.      */
  46.     public ByteArrayOutputStream() {
  47.     this(32);
  48.     }
  49.  
  50.     /**
  51.      * Creates a new ByteArrayOutputStream with the specified initial size.
  52.      * @param size the initial size
  53.      */
  54.     public ByteArrayOutputStream(int size) {
  55.     buf = new byte[size];
  56.     }
  57.  
  58.     /**
  59.      * Writes a byte to the buffer.
  60.      * @param b    the byte
  61.      */
  62.     public synchronized void write(int b) {
  63.     int newcount = count + 1;
  64.     if (newcount > buf.length) {
  65.         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  66.         System.arraycopy(buf, 0, newbuf, 0, count);
  67.         buf = newbuf;
  68.     }
  69.     buf[count] = (byte)b;
  70.     count = newcount;
  71.     }
  72.  
  73.     /**
  74.      * Writes bytes to the buffer.
  75.      * @param b    the data to be written
  76.      * @param off    the start offset in the data
  77.      * @param len    the number of bytes that are written
  78.      */
  79.     public synchronized void write(byte b[], int off, int len) {
  80.     int newcount = count + len;
  81.     if (newcount > buf.length) {
  82.         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  83.         System.arraycopy(buf, 0, newbuf, 0, count);
  84.         buf = newbuf;
  85.     }
  86.     System.arraycopy(b, off, buf, count, len);
  87.     count = newcount;
  88.     }
  89.  
  90.     /**
  91.      * Writes the contents of the buffer to another stream.
  92.      * @param out    the output stream to write to
  93.      */
  94.     public synchronized void writeTo(OutputStream out) throws IOException {
  95.     out.write(buf, 0, count);
  96.     }
  97.  
  98.     /**
  99.      * Resets the buffer so that you can use it again without
  100.      * throwing away the already allocated buffer.
  101.      */
  102.     public synchronized void reset() {
  103.     count = 0;
  104.     }
  105.  
  106.     /**
  107.      * Returns a copy of the input data.
  108.      */
  109.     public synchronized byte toByteArray()[] {
  110.     byte newbuf[] = new byte[count];
  111.     System.arraycopy(buf, 0, newbuf, 0, count);
  112.     return newbuf;
  113.     }
  114.  
  115.     /**
  116.      * Returns the current size of the buffer.
  117.      */
  118.     public int size() {
  119.     return count;
  120.     }
  121.  
  122.     /**
  123.      * Converts input data to a string.
  124.      * @return the string.
  125.      */
  126.     public String toString() {
  127.     return new String(toByteArray(), 0);
  128.     }
  129.  
  130.     /**
  131.      * Converts input data to a string. The top 8 bits of 
  132.      * each 16 bit Unicode character are set to hibyte.
  133.      * @param hibyte the bits set
  134.      */
  135.     public String toString(int hibyte) {
  136.     return new String(toByteArray(), hibyte);
  137.     }
  138. }
  139.