1 /*
  2  * @(#)ByteArray.java       1.45 01/12/03
  3  *
  4  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6  */
  7 
  8 import java.io.OutputStream;
  9 import java.io.IOException;
 10 import java.io.UnsupportedEncodingException;
 11 
 12 /**
 13  * <b>
 14  * Derived from Suns ByteArrayOutputStream,
 15  * added method insert() and toByteArray(int len). 
 16  * Removed synchronized - this is not thread save anymore but better
 17  * performing!
 18  * </b>
 19  * <br />
 20  * <br />
 21  * This class implements an output stream in which the data is 
 22  * written into a byte array. The buffer automatically grows as data 
 23  * is written to it. 
 24  * The data can be retrieved using <code>toByteArray()</code> and
 25  * <code>toString()</code>.
 26  * <p>
 27  * Closing a <tt>ByteArray</tt> has no effect. The methods in
 28  * this class can be called after the stream has been closed without
 29  * generating an <tt>IOException</tt>.
 30  *
 31  * @author  Arthur van Hoff
 32  * @version 1.45, 12/03/01
 33  * @since   JDK1.0
 34  */
 35 
 36 public class ByteArray extends OutputStream {
 37 
 38     /** 
 39      * The buffer where data is stored. 
 40      */
 41     protected byte buf[];
 42 
 43     /**
 44      * The number of valid bytes in the buffer. 
 45      */
 46     protected int count;
 47 
 48     /**
 49      * Creates a new byte array output stream. The buffer capacity is 
 50      * initially 32 bytes, though its size increases if necessary. 
 51      */
 52     public ByteArray() {
 53         this(32);
 54     }
 55 
 56     /**
 57      * Creates a new byte array output stream, with a buffer capacity of 
 58      * the specified size, in bytes. 
 59      *
 60      * @param   size   the initial size.
 61      * @exception  IllegalArgumentException if size is negative.
 62      */
 63     public ByteArray(int size) {
 64         if (size < 0) {
 65             throw new IllegalArgumentException("Negative initial size: "
 66                                                + size);
 67         }
 68         buf = new byte[size];
 69     }
 70 
 71     /**
 72      * Get from current position the len bytes
 73      */
 74     public byte[] toByteArray(int len) {
 75         
 76         byte newbuf[] = new byte[len];
 77         System.arraycopy(buf, count, newbuf, 0, len);
 78         count += len;
 79         return newbuf;
 80     }
 81 
 82     /**
 83      * Get the inner byte array buffer, handle with care.
 84      * It is only filled up to count bytes, the rest
 85      * of this array is undefined
 86      */
 87     public byte[] getByteArray() {
 88         return buf;
 89     }
 90 
 91     /**
 92      * Insert byte at position
 93      */
 94     public void insert(int index, byte b) {
 95         if (index < 0 || index >= buf.length) {
 96             throw new IllegalArgumentException("Index is too small or too big: " + index);
 97         }
 98         buf[index] = b;
 99     }
100 
101     /**
102      * Insert byte at position
103      */
104     public void insert(int index, byte[] b) {
105         if (index < 0 || (index+b.length) >= buf.length) {
106             throw new IllegalArgumentException("Index is too small or too big: " + index);
107         }
108         for (int ii=0; ii<b.length; ii++) {
109            buf[index] = b[ii];
110            index++;
111         }
112     }
113 
114     /**
115      * Writes the specified byte to this byte array output stream. 
116      *
117      * @param   b   the byte to be written.
118      */
119     public void write(int b) {
120         int newcount = count + 1;
121         if (newcount > buf.length) {
122             byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
123             System.arraycopy(buf, 0, newbuf, 0, count);
124             buf = newbuf;
125         }
126         buf[count] = (byte)b;
127         count = newcount;
128     }
129 
130     /**
131      * Writes <code>len</code> bytes from the specified byte array 
132      * starting at offset <code>off</code> to this byte array output stream.
133      *
134      * @param   b     the data.
135      * @param   off   the start offset in the data.
136      * @param   len   the number of bytes to write.
137      */
138     public void write(byte b[], int off, int len) {
139         if ((off < 0) || (off > b.length) || (len < 0) ||
140             ((off + len) > b.length) || ((off + len) < 0)) {
141             throw new IndexOutOfBoundsException();
142         } else if (len == 0) {
143             return;
144         }
145         int newcount = count + len;
146         if (newcount > buf.length) {
147             byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
148             System.arraycopy(buf, 0, newbuf, 0, count);
149             buf = newbuf;
150         }
151         System.arraycopy(b, off, buf, count, len);
152         count = newcount;
153     }
154 
155     /**
156      * Writes the complete contents of this byte array output stream to 
157      * the specified output stream argument, as if by calling the output 
158      * stream's write method using <code>out.write(buf, 0, count)</code>.
159      *
160      * @param      out   the output stream to which to write the data.
161      * @exception  IOException  if an I/O error occurs.
162      */
163     public void writeTo(OutputStream out) throws IOException {
164         out.write(buf, 0, count);
165     }
166 
167     /**
168      * Resets the <code>count</code> field of this byte array output 
169      * stream to zero, so that all currently accumulated output in the 
170      * ouput stream is discarded. The output stream can be used again, 
171      * reusing the already allocated buffer space. 
172      *
173      * @see     java.io.ByteArrayInputStream#count
174      */
175     public void reset() {
176         count = 0;
177     }
178 
179     /**
180      * Creates a newly allocated byte array. Its size is the current 
181      * size of this output stream and the valid contents of the buffer 
182      * have been copied into it. 
183      *
184      * @return  the current contents of this output stream, as a byte array.
185      * @see     java.io.ByteArray#size()
186      */
187     public byte[] toByteArray() {
188         byte newbuf[] = new byte[count];
189         System.arraycopy(buf, 0, newbuf, 0, count);
190         return newbuf;
191     }
192 
193     /**
194      * Returns the current size of the buffer.
195      *
196      * @return  the value of the <code>count</code> field, which is the number
197      *          of valid bytes in this output stream.
198      * @see     java.io.ByteArray#count
199      */
200     public int size() {
201         return count;
202     }
203 
204     /**
205      * Converts the buffer's contents into a string, translating bytes into
206      * characters according to the platform's default character encoding.
207      *
208      * @return String translated from the buffer's contents.
209      * @since   JDK1.1
210      */
211     public String toString() {
212         return new String(buf, 0, count);
213     }
214 
215     /**
216      * Converts the buffer's contents into a string, translating bytes into
217      * characters according to the specified character encoding.
218      *
219      * @param   enc  a character-encoding name.
220      * @return String translated from the buffer's contents.
221      * @throws UnsupportedEncodingException
222      *         If the named encoding is not supported.
223      * @since   JDK1.1
224      */
225     public String toString(String enc) throws UnsupportedEncodingException {
226         return new String(buf, 0, count, enc);
227     }
228 
229     /**
230      * Creates a newly allocated string. Its size is the current size of 
231      * the output stream and the valid contents of the buffer have been 
232      * copied into it. Each character <i>c</i> in the resulting string is 
233      * constructed from the corresponding element <i>b</i> in the byte 
234      * array such that:
235      * <blockquote><pre>
236      *     c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
237      * </pre></blockquote>
238      *
239      * @deprecated This method does not properly convert bytes into characters.
240      * As of JDK&nbsp;1.1, the preferred way to do this is via the
241      * <code>toString(String enc)</code> method, which takes an encoding-name
242      * argument, or the <code>toString()</code> method, which uses the
243      * platform's default character encoding.
244      *
245      * @param      hibyte    the high byte of each resulting Unicode character.
246      * @return     the current contents of the output stream, as a string.
247      * @see        java.io.ByteArray#size()
248      * @see        java.io.ByteArray#toString(String)
249      * @see        java.io.ByteArray#toString()
250      */
251     public String toString(int hibyte) {
252         return new String(buf, hibyte, 0, count);
253     }
254 
255     /**
256      * Closing a <tt>ByteArray</tt> has no effect. The methods in
257      * this class can be called after the stream has been closed without
258      * generating an <tt>IOException</tt>.
259      * <p>
260      *
261      */
262     public void close() throws IOException {
263     }
264 
265 }


syntax highlighted by Code2HTML, v. 0.9.1