# ByteArrayOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Fields](https://developer.android.com/reference/java/io/ByteArrayOutputStream#lfields)\|[Ctors](https://developer.android.com/reference/java/io/ByteArrayOutputStream#pubctors)\|[Methods](https://developer.android.com/reference/java/io/ByteArrayOutputStream#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/io/ByteArrayOutputStream#inhmethods)  

# ByteArrayOutputStream

`
public


class
ByteArrayOutputStream
`  
`


``

extends `[OutputStream](https://developer.android.com/reference/java/io/OutputStream)`


``


`

|---|---|-------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.io.OutputStream](https://developer.android.com/reference/java/io/OutputStream) ||
|   | ↳ | java.io.ByteArrayOutputStream |

<br />

*** ** * ** ***

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using`toByteArray()`and`toString()`.

Closing a`ByteArrayOutputStream`has no effect. The methods in this class can be called after the stream has been closed without generating an`IOException`.

## Summary

|                                                                     ### Fields                                                                     ||
|---------------------|-------------------------------------------------------------------------------------------------------------------------------|
| ` protected byte[]` | [buf](https://developer.android.com/reference/java/io/ByteArrayOutputStream#buf) The buffer where data is stored.             |
| ` protected int`    | [count](https://developer.android.com/reference/java/io/ByteArrayOutputStream#count) The number of valid bytes in the buffer. |

|                                                                                                          ### Public constructors                                                                                                           ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[ByteArrayOutputStream](https://developer.android.com/reference/java/io/ByteArrayOutputStream#ByteArrayOutputStream())`() ` Creates a new`ByteArrayOutputStream`.                                                                    |
| ` `[ByteArrayOutputStream](https://developer.android.com/reference/java/io/ByteArrayOutputStream#ByteArrayOutputStream(int))`(int size) ` Creates a new`ByteArrayOutputStream`, with a buffer capacity of the specified size, in bytes. |

|                                                                                                                                                                                                                                                                                                                                                                                ### Public methods                                                                                                                                                                                                                                                                                                                                                                                 ||
|-----------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                               | ` `[close](https://developer.android.com/reference/java/io/ByteArrayOutputStream#close())`() ` Closing a`ByteArrayOutputStream`has no effect.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ` void`                                                               | ` `[reset](https://developer.android.com/reference/java/io/ByteArrayOutputStream#reset())`() ` Resets the`count`field of this`ByteArrayOutputStream`to zero, so that all currently accumulated output in the output stream is discarded.                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ` int`                                                                | ` `[size](https://developer.android.com/reference/java/io/ByteArrayOutputStream#size())`() ` Returns the current size of the buffer.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| ` byte[]`                                                             | ` `[toByteArray](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toByteArray())`() ` Creates a newly allocated byte array.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` charsetName) ` Converts the buffer's contents into a string by decoding the bytes using the named[charset](https://developer.android.com/reference/java/nio/charset/Charset).                                                                                                                                                                                                                                                                                                                                        |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.nio.charset.Charset))`(`[Charset](https://developer.android.com/reference/java/nio/charset/Charset)` charset) ` Converts the buffer's contents into a string by decoding the bytes using the specified[charset](https://developer.android.com/reference/java/nio/charset/Charset).                                                                                                                                                                                                                                                                                                                       |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString())`() ` Converts the buffer's contents into a string decoding bytes using the platform's default character set.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(int))`(int hibyte) ` *This method was deprecated in API level 15. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the[toString(java.lang.String)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.lang.String))or[toString(java.nio.charset.Charset)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.nio.charset.Charset))method, which takes an encoding-name or charset argument, or the`toString()`method, which uses the platform's default character encoding.* |
| ` void`                                                               | ` `[write](https://developer.android.com/reference/java/io/ByteArrayOutputStream#write(byte[],%20int,%20int))`(byte[] b, int off, int len) ` Writes`len`bytes from the specified byte array starting at offset`off`to this`ByteArrayOutputStream`.                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ` void`                                                               | ` `[write](https://developer.android.com/reference/java/io/ByteArrayOutputStream#write(int))`(int b) ` Writes the specified byte to this`ByteArrayOutputStream`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` void`                                                               | ` `[writeBytes](https://developer.android.com/reference/java/io/ByteArrayOutputStream#writeBytes(byte[]))`(byte[] b) ` Writes the complete contents of the specified byte array to this`ByteArrayOutputStream`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` void`                                                               | ` `[writeTo](https://developer.android.com/reference/java/io/ByteArrayOutputStream#writeTo(java.io.OutputStream))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out) ` Writes the complete contents of this`ByteArrayOutputStream`to the specified output stream argument, as if by calling the output stream's write method using`out.write(buf, 0, count)`.                                                                                                                                                                                                                                                                                                            |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.io.OutputStream](https://developer.android.com/reference/java/io/OutputStream)` ` |----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void`                                                                                | ` `[close](https://developer.android.com/reference/java/io/OutputStream#close())`() ` Closes this output stream and releases any system resources associated with this stream.                                                   | | ` void`                                                                                | ` `[flush](https://developer.android.com/reference/java/io/OutputStream#flush())`() ` Flushes this output stream and forces any buffered output bytes to be written out.                                                         | | ` static `[OutputStream](https://developer.android.com/reference/java/io/OutputStream) | ` `[nullOutputStream](https://developer.android.com/reference/java/io/OutputStream#nullOutputStream())`() ` Returns a new`OutputStream`which discards all bytes.                                                                 | | ` void`                                                                                | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(byte[]))`(byte[] b) ` Writes`b.length`bytes from the specified byte array to this output stream.                                                   | | ` void`                                                                                | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(byte[],%20int,%20int))`(byte[] b, int off, int len) ` Writes`len`bytes from the specified byte array starting at offset`off`to this output stream. | | ` abstract void`                                                                       | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(int))`(int b) ` Writes the specified byte to this output stream.                                                                                   | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| From interface` `[java.io.Flushable](https://developer.android.com/reference/java/io/Flushable)` ` |------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[flush](https://developer.android.com/reference/java/io/Flushable#flush())`() ` Flushes this stream by writing any buffered output to the underlying stream. | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Fields

### buf

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected byte[] buf
```

The buffer where data is stored.

<br />

### count

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected int count
```

The number of valid bytes in the buffer.

<br />

## Public constructors

### ByteArrayOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public ByteArrayOutputStream ()
```

Creates a new`ByteArrayOutputStream`. The buffer capacity is initially 32 bytes, though its size increases if necessary.

<br />

### ByteArrayOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public ByteArrayOutputStream (int size)
```

Creates a new`ByteArrayOutputStream`, with a buffer capacity of the specified size, in bytes.

<br />

|               Parameters                ||
|--------|---------------------------------|
| `size` | `int`: the initial size. <br /> |

|                                                            Throws                                                            ||
|--------------------------------------------------------------------------------------------------------|----------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if size is negative. |

## Public methods

### close

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void close ()
```

Closing a`ByteArrayOutputStream`has no effect. The methods in this class can be called after the stream has been closed without generating an`IOException`.

<br />

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

### reset

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void reset ()
```

Resets the`count`field of this`ByteArrayOutputStream`to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.

<br />

**See also:**

- [ByteArrayInputStream.count](https://developer.android.com/reference/java/io/ByteArrayInputStream#count)  

### size

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int size ()
```

Returns the current size of the buffer.

<br />

|                                                Returns                                                ||
|-------|------------------------------------------------------------------------------------------------|
| `int` | the value of the`count`field, which is the number of valid bytes in this output stream. <br /> |

**See also:**

- [count](https://developer.android.com/reference/java/io/ByteArrayOutputStream#count)  

### toByteArray

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public byte[] toByteArray ()
```

Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

<br />

|                                    Returns                                    ||
|----------|---------------------------------------------------------------------|
| `byte[]` | the current contents of this output stream, as a byte array. <br /> |

**See also:**

- [size()](https://developer.android.com/reference/java/io/ByteArrayOutputStream#size())  

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString (String charsetName)
```

Converts the buffer's contents into a string by decoding the bytes using the named[charset](https://developer.android.com/reference/java/nio/charset/Charset).

This method is equivalent to`#toString(charset)`that takes a[charset](https://developer.android.com/reference/java/nio/charset/Charset).

An invocation of this method of the form  

     ByteArrayOutputStream b = ...
          b.toString("UTF-8")
          
     
behaves in exactly the same way as the expression  

     ByteArrayOutputStream b = ...
          b.toString(StandardCharsets.UTF_8)
          
     
<br />

<br />

|                                                             Parameters                                                             ||
|---------------|---------------------------------------------------------------------------------------------------------------------|
| `charsetName` | `String`: the name of a supported[charset](https://developer.android.com/reference/java/nio/charset/Charset) <br /> |

|                                                        Returns                                                        ||
|--------------------------------------------------------------------|---------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | String decoded from the buffer's contents. <br /> |

|                                                                       Throws                                                                        ||
|--------------------------------------------------------------------------------------------------------------|---------------------------------------|
| [UnsupportedEncodingException](https://developer.android.com/reference/java/io/UnsupportedEncodingException) | If the named charset is not supported |

### toString

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString (Charset charset)
```

Converts the buffer's contents into a string by decoding the bytes using the specified[charset](https://developer.android.com/reference/java/nio/charset/Charset). The length of the new`String`is a function of the charset, and hence may not be equal to the length of the byte array.

This method always replaces malformed-input and unmappable-character sequences with the charset's default replacement string. The[CharsetDecoder](https://developer.android.com/reference/java/nio/charset/CharsetDecoder)class should be used when more control over the decoding process is required.

<br />

|                                                                 Parameters                                                                 ||
|-----------|---------------------------------------------------------------------------------------------------------------------------------|
| `charset` | `Charset`: the[charset](https://developer.android.com/reference/java/nio/charset/Charset)to be used to decode the`bytes` <br /> |

|                                                        Returns                                                        ||
|--------------------------------------------------------------------|---------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | String decoded from the buffer's contents. <br /> |

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the new`String`is a function of the character set, and hence may not be equal to the size of the buffer.

This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The[CharsetDecoder](https://developer.android.com/reference/java/nio/charset/CharsetDecoder)class should be used when more control over the decoding process is required.

<br />

|                                                        Returns                                                        ||
|--------------------------------------------------------------------|---------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | String decoded from the buffer's contents. <br /> |

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Deprecated in[API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString (int hibyte)
```

<br />

**This method was deprecated in API level 15.**   
This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the[toString(java.lang.String)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.lang.String))or[toString(java.nio.charset.Charset)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.nio.charset.Charset))method, which takes an encoding-name or charset argument, or the`toString()`method, which uses the platform's default character encoding.

Creates a newly allocated string. Its size is the current size of the output stream and the valid contents of the buffer have been copied into it. Each character*c* in the resulting string is constructed from the corresponding element*b*in the byte array such that:
>
>     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
>      
<br />

<br />

|                                 Parameters                                 ||
|----------|------------------------------------------------------------------|
| `hibyte` | `int`: the high byte of each resulting Unicode character. <br /> |

|                                                              Returns                                                               ||
|--------------------------------------------------------------------|----------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the current contents of the output stream, as a string. <br /> |

**See also:**

- [size()](https://developer.android.com/reference/java/io/ByteArrayOutputStream#size())
- [toString(String)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString(java.lang.String))
- [toString()](https://developer.android.com/reference/java/io/ByteArrayOutputStream#toString())  

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (byte[] b, 
                int off, 
                int len)
```

Writes`len`bytes from the specified byte array starting at offset`off`to this`ByteArrayOutputStream`.

<br />

|                     Parameters                     ||
|-------|---------------------------------------------|
| `b`   | `byte`: the data. <br />                    |
| `off` | `int`: the start offset in the data. <br /> |
| `len` | `int`: the number of bytes to write. <br /> |

|                                                                                        Throws                                                                                         ||
|----------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)           | if`b`is`null`.                                                              |
| [IndexOutOfBoundsException](https://developer.android.com/reference/java/lang/IndexOutOfBoundsException) | if`off`is negative,`len`is negative, or`len`is greater than`b.length - off` |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (int b)
```

Writes the specified byte to this`ByteArrayOutputStream`.

<br />

|                 Parameters                 ||
|-----|---------------------------------------|
| `b` | `int`: the byte to be written. <br /> |

### writeBytes

Added in[API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void writeBytes (byte[] b)
```

Writes the complete contents of the specified byte array to this`ByteArrayOutputStream`.

<br />

##### API Note:

- This method is equivalent to[write(b, 0, b.length)](https://developer.android.com/reference/java/io/ByteArrayOutputStream#write(byte[],%20int,%20int)).

|          Parameters           ||
|-----|--------------------------|
| `b` | `byte`: the data. <br /> |

|                                                     Throws                                                     ||
|------------------------------------------------------------------------------------------------|----------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`b`is`null`. |

### writeTo

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void writeTo (OutputStream out)
```

Writes the complete contents of this`ByteArrayOutputStream`to the specified output stream argument, as if by calling the output stream's write method using`out.write(buf, 0, count)`.

<br />

|                                 Parameters                                  ||
|-------|----------------------------------------------------------------------|
| `out` | `OutputStream`: the output stream to which to write the data. <br /> |

|                                                         Throws                                                          ||
|------------------------------------------------------------------------------------------------|-------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if`out`is`null`.        |
| [IOException](https://developer.android.com/reference/java/io/IOException)                     | if an I/O error occurs. |