Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java ByteArrayOutputStream Class

Java ByteArrayOutputStream Class

Java Stream Java Stream


A byte array output stream creates a byte array buffer in memory, and all data sent to the output stream is saved in this byte array buffer. There are several ways to create a byte array output stream object.

The following constructor creates a buffer of 32 bytes (default size).


OutputStream bOut = new ByteArrayOutputStream();

Another constructor creates a buffer of size a bytes.


OutputStream bOut = new ByteArrayOutputStream(int a)

After successfully creating the byte array output stream object, you can refer to the methods in the list below to perform write or other operations on the stream.

No. Method Description
1 public void reset()
Resets the count field of this byte array output stream to zero, thereby discarding all data output currently accumulated in the output stream.
2 public byte[] toByteArray()
Creates a newly allocated byte array. The size of the array is the same as the current output stream, and the content is a copy of the current output stream.
3 public String toString()
Converts the content of the buffer to a string, converting bytes to characters according to the platform's default character encoding.
4 public void write(int w)
 Writes the specified byte to this byte array output stream.
5 public void write(byte []b, int off, int len)
 Writes len bytes from the specified byte array starting at offset off starting at len bytes into this byte array output stream.
6 public void writeTo(OutputStream outSt)
Writes the entire content of this byte array output stream to the specified output stream parameter.

Example

The example below demonstrates the use of ByteArrayInputStream and ByteArrayOutputStream:

Example

import

java

.

io

.*;

public

class

ByteStreamTest

{

public

static

void

main

(

String

args

[

]

)

throws

IOException

{

ByteArrayOutputStream

bOutput

=

new

ByteArrayOutputStream

(

12

)

;

while

(

bOutput

.

size

(

)

!=

10

)

{

//

Get user input

bOutput

.

write

(

System

.

in

.

read

(

)

)

;

}

byte

b

[

]

=

bOutput

.

toByteArray

(

)

;

System

.

out

.

println

(

"

Print the content

"

)

;

for

(

int

x

=

0

;

x

<

b

.

length

;

x

++

)

{

//

Print character

System

.

out

.

print

(

(

char

)

b

[

x

]

"

"

)

;

}

System

.

out

.

println

(

"

"

)

;

int

c

;

ByteArrayInputStream

bInput

=

new

ByteArrayInputStream

(

b

)

;

System

.

out

.

println

(

"

Converting characters to Upper case

"

)

;

for

(

int

y

=

0

;

y

<

1

;

y

++

)

{

while

(

(

c

=

bInput

.

read

(

)

)

!= -

1

)

{

System

.

out

.

println

(

Character

.

toUpperCase

(

(

char

)

c

)

)

;

}

bInput

.

reset

(

)

;

}

}

}

The compilation and execution results of the above example are as follows:


asdfghjkly
Print the content
a   s   d   f   g   h   j   k   l   y
Converting characters to Upper case
A
S
D
F
G
H
J
K
L
Y

Java Stream Java Stream