KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > ByteArrayOutputStream


1 /*
2  * @(#)ByteArrayOutputStream.java 1.49 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10
11 /**
12  * This class implements an output stream in which the data is
13  * written into a byte array. The buffer automatically grows as data
14  * is written to it.
15  * The data can be retrieved using <code>toByteArray()</code> and
16  * <code>toString()</code>.
17  * <p>
18  * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
19  * this class can be called after the stream has been closed without
20  * generating an <tt>IOException</tt>.
21  *
22  * @author Arthur van Hoff
23  * @version 1.49, 05/18/04
24  * @since JDK1.0
25  */

26
27 public class ByteArrayOutputStream extends OutputStream JavaDoc {
28
29     /**
30      * The buffer where data is stored.
31      */

32     protected byte buf[];
33
34     /**
35      * The number of valid bytes in the buffer.
36      */

37     protected int count;
38
39     /**
40      * Creates a new byte array output stream. The buffer capacity is
41      * initially 32 bytes, though its size increases if necessary.
42      */

43     public ByteArrayOutputStream() {
44     this(32);
45     }
46
47     /**
48      * Creates a new byte array output stream, with a buffer capacity of
49      * the specified size, in bytes.
50      *
51      * @param size the initial size.
52      * @exception IllegalArgumentException if size is negative.
53      */

54     public ByteArrayOutputStream(int size) {
55         if (size < 0) {
56             throw new IllegalArgumentException JavaDoc("Negative initial size: "
57                                                + size);
58         }
59     buf = new byte[size];
60     }
61
62     /**
63      * Writes the specified byte to this byte array output stream.
64      *
65      * @param b the byte to be written.
66      */

67     public synchronized void write(int b) {
68     int newcount = count + 1;
69     if (newcount > buf.length) {
70         byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
71         System.arraycopy(buf, 0, newbuf, 0, count);
72         buf = newbuf;
73     }
74     buf[count] = (byte)b;
75     count = newcount;
76     }
77
78     /**
79      * Writes <code>len</code> bytes from the specified byte array
80      * starting at offset <code>off</code> to this byte array output stream.
81      *
82      * @param b the data.
83      * @param off the start offset in the data.
84      * @param len the number of bytes to write.
85      */

86     public synchronized void write(byte b[], int off, int len) {
87     if ((off < 0) || (off > b.length) || (len < 0) ||
88             ((off + len) > b.length) || ((off + len) < 0)) {
89         throw new IndexOutOfBoundsException JavaDoc();
90     } else if (len == 0) {
91         return;
92     }
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 JavaDoc out) throws IOException JavaDoc {
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      * output 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      * @return String translated from the buffer's contents.
157      * @since JDK1.1
158      */

159     public String JavaDoc toString() {
160     return new String JavaDoc(buf, 0, count);
161     }
162
163     /**
164      * Converts the buffer's contents into a string, translating bytes into
165      * characters according to the specified character encoding.
166      *
167      * @param enc a character-encoding name.
168      * @return String translated from the buffer's contents.
169      * @throws UnsupportedEncodingException
170      * If the named encoding is not supported.
171      * @since JDK1.1
172      */

173     public String JavaDoc toString(String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
174     return new String JavaDoc(buf, 0, count, enc);
175     }
176
177     /**
178      * Creates a newly allocated string. Its size is the current size of
179      * the output stream and the valid contents of the buffer have been
180      * copied into it. Each character <i>c</i> in the resulting string is
181      * constructed from the corresponding element <i>b</i> in the byte
182      * array such that:
183      * <blockquote><pre>
184      * c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
185      * </pre></blockquote>
186      *
187      * @deprecated This method does not properly convert bytes into characters.
188      * As of JDK&nbsp;1.1, the preferred way to do this is via the
189      * <code>toString(String enc)</code> method, which takes an encoding-name
190      * argument, or the <code>toString()</code> method, which uses the
191      * platform's default character encoding.
192      *
193      * @param hibyte the high byte of each resulting Unicode character.
194      * @return the current contents of the output stream, as a string.
195      * @see java.io.ByteArrayOutputStream#size()
196      * @see java.io.ByteArrayOutputStream#toString(String)
197      * @see java.io.ByteArrayOutputStream#toString()
198      */

199     @Deprecated JavaDoc
200     public String JavaDoc toString(int hibyte) {
201     return new String JavaDoc(buf, hibyte, 0, count);
202     }
203
204     /**
205      * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
206      * this class can be called after the stream has been closed without
207      * generating an <tt>IOException</tt>.
208      * <p>
209      *
210      */

211     public void close() throws IOException JavaDoc {
212     }
213
214 }
215
Popular Tags