< prev index next >

src/java.base/share/classes/java/io/ByteArrayOutputStream.java

Print this page

142      * This method is equivalent to {@link #write(byte[],int,int)
143      * write(b, 0, b.length)}.
144      *
145      * @param   b     the data.
146      * @throws  NullPointerException if {@code b} is {@code null}.
147      * @since   11
148      */
149     public void writeBytes(byte[] b) {
150         write(b, 0, b.length);
151     }
152 
153     /**
154      * Writes the complete contents of this {@code ByteArrayOutputStream} to
155      * the specified output stream argument, as if by calling the output
156      * stream's write method using {@code out.write(buf, 0, count)}.
157      *
158      * @param   out   the output stream to which to write the data.
159      * @throws  NullPointerException if {@code out} is {@code null}.
160      * @throws  IOException if an I/O error occurs.
161      */
162     public void writeTo(OutputStream out) throws IOException {
163         if (Thread.currentThread().isVirtual()) {
164             byte[] bytes;
165             synchronized (this) {
166                 bytes = Arrays.copyOf(buf, count);
167             }
168             out.write(bytes);
169         } else synchronized (this) {
170             out.write(buf, 0, count);
171         }
172     }
173 
174     /**
175      * Resets the {@code count} field of this {@code ByteArrayOutputStream}
176      * to zero, so that all currently accumulated output in the
177      * output stream is discarded. The output stream can be used again,
178      * reusing the already allocated buffer space.
179      *
180      * @see     java.io.ByteArrayInputStream#count
181      */
182     public synchronized void reset() {
183         count = 0;
184     }
185 
186     /**
187      * Creates a newly allocated byte array. Its size is the current
188      * size of this output stream and the valid contents of the buffer
189      * have been copied into it.
190      *
191      * @return  the current contents of this output stream, as a byte array.

142      * This method is equivalent to {@link #write(byte[],int,int)
143      * write(b, 0, b.length)}.
144      *
145      * @param   b     the data.
146      * @throws  NullPointerException if {@code b} is {@code null}.
147      * @since   11
148      */
149     public void writeBytes(byte[] b) {
150         write(b, 0, b.length);
151     }
152 
153     /**
154      * Writes the complete contents of this {@code ByteArrayOutputStream} to
155      * the specified output stream argument, as if by calling the output
156      * stream's write method using {@code out.write(buf, 0, count)}.
157      *
158      * @param   out   the output stream to which to write the data.
159      * @throws  NullPointerException if {@code out} is {@code null}.
160      * @throws  IOException if an I/O error occurs.
161      */
162     public synchronized void writeTo(OutputStream out) throws IOException {
163         out.write(buf, 0, count);








164     }
165 
166     /**
167      * Resets the {@code count} field of this {@code ByteArrayOutputStream}
168      * to zero, so that all currently accumulated output in the
169      * output stream is discarded. The output stream can be used again,
170      * reusing the already allocated buffer space.
171      *
172      * @see     java.io.ByteArrayInputStream#count
173      */
174     public synchronized void reset() {
175         count = 0;
176     }
177 
178     /**
179      * Creates a newly allocated byte array. Its size is the current
180      * size of this output stream and the valid contents of the buffer
181      * have been copied into it.
182      *
183      * @return  the current contents of this output stream, as a byte array.
< prev index next >