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(), 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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Class LoggingByteArrayOutputStream (2.0.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.0.0 (latest)](/java/docs/reference/google-http-client/latest/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.47.1](/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.46.3](/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.45.3](/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.44.2](/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.43.2](/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.42.3](/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.LoggingByteArrayOutputStream)\n- [1.41.8](/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.LoggingByteArrayOutputStream) \n\n public class LoggingByteArrayOutputStream extends ByteArrayOutputStream\n\nThread-safe byte array output stream that logs what was written to it when the stream is closed.\n\nUse this as a safe way to log a limited amount of content. As content is written to the\nstream, it is stored as a byte array, up to the maximum number of bytes limit that was set in the\nconstructor. Note that if the maximum limit is set too high, it risks an OutOfMemoryError\non low-memory devices. This class also keeps track of the total number of bytes written,\nregardless of whether they were logged. On [#close()](/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\nspecified logger and logging level: the total number of bytes written, and the bounded content\nlogged (assuming charset \"UTF-8\"). Any control characters are stripped out of the content. \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e [java.io.OutputStream](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html) \\\u003e [ByteArrayOutputStream](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html) \\\u003e LoggingByteArrayOutputStream \n\nInherited Members\n-----------------\n\n[ByteArrayOutputStream.close()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#close--) \n[ByteArrayOutputStream.reset()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#reset--) \n[ByteArrayOutputStream.size()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#size--) \n[ByteArrayOutputStream.toByteArray()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toByteArray--) \n[ByteArrayOutputStream.toString()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString--) \n[ByteArrayOutputStream.toString(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-int-) \n[ByteArrayOutputStream.toString(String)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-java.lang.String-) \n[ByteArrayOutputStream.toString(Charset)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-java.nio.charset.Charset-) \n[ByteArrayOutputStream.write(byte\\[\\],int,int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-byte[]-int-int-) \n[ByteArrayOutputStream.write(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-int-) \n[ByteArrayOutputStream.writeBytes(byte\\[\\])](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#writeBytes-byte[]-) \n[ByteArrayOutputStream.writeTo(OutputStream)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#writeTo-java.io.OutputStream-) \n[OutputStream.flush()](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#flush--) \n[OutputStream.nullOutputStream()](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#nullOutputStream--) \n[OutputStream.write(byte\\[\\])](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#write-byte[]-) \n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nConstructors\n------------\n\n### LoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog)\n\n public LoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog)\n\nMethods\n-------\n\n### close()\n\n public synchronized void close()\n\n**Overrides** \n[ByteArrayOutputStream.close()](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#close--)\n\n### getBytesWritten()\n\n public final synchronized int getBytesWritten()\n\nReturns the bytes written to the stream (may or may not have been logged).\n\n### getMaximumBytesToLog()\n\n public final int getMaximumBytesToLog()\n\nReturns the maximum number of bytes to log (may be `0` to avoid logging content).\n\n### write(byte\\[\\] b, int off, int len)\n\n public synchronized void write(byte[] b, int off, int len)\n\n**Overrides** \n[ByteArrayOutputStream.write(byte\\[\\],int,int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-byte[]-int-int-)\n\n### write(int b)\n\n public synchronized void write(int b)\n\n**Overrides** \n[ByteArrayOutputStream.write(int)](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#write-int-)"]]