Class LoggingByteArrayOutputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
com.google.api.client.util.LoggingByteArrayOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
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.
- Since:
- 1.9
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intBytes written to the stream (may or may not have been logged).private booleanWhether the stream has already been closed.private final LoggerLogger.private final LevelLogging level.private final intMaximum number of bytes to log (may be0to avoid logging content).Fields inherited from class ByteArrayOutputStream
buf, count -
Constructor Summary
ConstructorsConstructorDescriptionLoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog) -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendBytes(StringBuilder buf, int x) voidclose()final intReturns the bytes written to the stream (may or may not have been logged).final intReturns the maximum number of bytes to log (may be0to avoid logging content).voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class ByteArrayOutputStream
reset, size, toByteArray, toString, toString, toString, writeToMethods inherited from class OutputStream
flush, write
-
Field Details
-
bytesWritten
private int bytesWrittenBytes written to the stream (may or may not have been logged). -
maximumBytesToLog
private final int maximumBytesToLogMaximum number of bytes to log (may be0to avoid logging content). -
closed
private boolean closedWhether the stream has already been closed. -
loggingLevel
Logging level. -
logger
Logger.
-
-
Constructor Details
-
LoggingByteArrayOutputStream
-
-
Method Details
-
write
public void write(int b) - Overrides:
writein classByteArrayOutputStream
-
write
public void write(byte[] b, int off, int len) - Overrides:
writein classByteArrayOutputStream
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classByteArrayOutputStream- Throws:
IOException
-
getMaximumBytesToLog
public final int getMaximumBytesToLog()Returns the maximum number of bytes to log (may be0to avoid logging content). -
getBytesWritten
public final int getBytesWritten()Returns the bytes written to the stream (may or may not have been logged). -
appendBytes
-