# Class LoggingByteArrayOutputStream (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/latest/com.google.api.client.util.LoggingByteArrayOutputStream)
- [2.0.3](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/2.0.3/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.47.1](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.46.3](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.45.3](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.44.2](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.43.2](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.42.3](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.LoggingByteArrayOutputStream)
- [1.41.8](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.LoggingByteArrayOutputStream)

    public class LoggingByteArrayOutputStream extends ByteArrayOutputStream

Thread-safe byte array output stream that logs what was written to it when the stream is closed.

Use this as a safe way to log a limited amount of content. As content is written to the
stream, it is stored as a byte array, up to the maximum number of bytes limit that was set in the
constructor. Note that if the maximum limit is set too high, it risks an OutOfMemoryError
on low-memory devices. This class also keeps track of the total number of bytes written,
regardless of whether they were logged. On [#close()](https://docs-0cloud-0google-0com.linkedin.net.nz/java/docs/reference/google-http-client/latest/com.google.api.client.util.LoggingByteArrayOutputStream#com_google_api_client_util_LoggingByteArrayOutputStream_close__), it then logs two records to the
specified logger and logging level: the total number of bytes written, and the bounded content
logged (assuming charset "UTF-8"). Any control characters are stripped out of the content.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> [java.io.OutputStream](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html) \> [ByteArrayOutputStream](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html) \> LoggingByteArrayOutputStream

## Inherited Members

[ByteArrayOutputStream.close()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#close--) [ByteArrayOutputStream.reset()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#reset--) [ByteArrayOutputStream.size()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#size--) [ByteArrayOutputStream.toByteArray()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toByteArray--) [ByteArrayOutputStream.toString()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString--) [ByteArrayOutputStream.toString(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-int-) [ByteArrayOutputStream.toString(String)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-java.lang.String-) [ByteArrayOutputStream.toString(Charset)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-java.nio.charset.Charset-) [ByteArrayOutputStream.write(byte\[\],int,int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-byte[]-int-int-) [ByteArrayOutputStream.write(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-int-) [ByteArrayOutputStream.writeBytes(byte\[\])](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#writeBytes-byte[]-) [ByteArrayOutputStream.writeTo(OutputStream)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#writeTo-java.io.OutputStream-) [OutputStream.flush()](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#flush--) [OutputStream.nullOutputStream()](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#nullOutputStream--) [OutputStream.write(byte\[\])](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#write-byte[]-) [Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Constructors

### LoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog)

    public LoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `logger` | `https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html` logger |
| `loggingLevel` | `https://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html` logging level |
| `maximumBytesToLog` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` maximum number of bytes to log (may be `0` to avoid logging content) |

## Methods

### close()

    public synchronized void close()

**Overrides** [ByteArrayOutputStream.close()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#close--)

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### getBytesWritten()

    public final synchronized int getBytesWritten()

Returns the bytes written to the stream (may or may not have been logged).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### getMaximumBytesToLog()

    public final int getMaximumBytesToLog()

Returns the maximum number of bytes to log (may be `0` to avoid logging content).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### write(byte\[\] b, int off, int len)

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

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `b` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[]` |
| `off` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |
| `len` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

**Overrides** [ByteArrayOutputStream.write(byte\[\],int,int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-byte[]-int-int-)

### write(int b)

    public synchronized void write(int b)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `b` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

**Overrides** [ByteArrayOutputStream.write(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-int-)