Package com.google.api.client.util
Class LoggingByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.ByteArrayOutputStream
-
- com.google.api.client.util.LoggingByteArrayOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class LoggingByteArrayOutputStream extends java.io.ByteArrayOutputStreamThread-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
OutOfMemoryErroron low-memory devices. This class also keeps track of the total number of bytes written, regardless of whether they were logged. Onclose(), 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
Fields Modifier and Type Field Description private intbytesWrittenBytes written to the stream (may or may not have been logged).private booleanclosedWhether the stream has already been closed.private java.util.logging.LoggerloggerLogger.private java.util.logging.LevelloggingLevelLogging level.private intmaximumBytesToLogMaximum number of bytes to log (may be0to avoid logging content).
-
Constructor Summary
Constructors Constructor Description LoggingByteArrayOutputStream(java.util.logging.Logger logger, java.util.logging.Level loggingLevel, int maximumBytesToLog)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidappendBytes(java.lang.StringBuilder buf, int x)voidclose()intgetBytesWritten()Returns the bytes written to the stream (may or may not have been logged).intgetMaximumBytesToLog()Returns the maximum number of bytes to log (may be0to avoid logging content).voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Field Detail
-
bytesWritten
private int bytesWritten
Bytes written to the stream (may or may not have been logged).
-
maximumBytesToLog
private final int maximumBytesToLog
Maximum number of bytes to log (may be0to avoid logging content).
-
closed
private boolean closed
Whether the stream has already been closed.
-
loggingLevel
private final java.util.logging.Level loggingLevel
Logging level.
-
logger
private final java.util.logging.Logger logger
Logger.
-
-
Constructor Detail
-
LoggingByteArrayOutputStream
public LoggingByteArrayOutputStream(java.util.logging.Logger logger, java.util.logging.Level loggingLevel, int maximumBytesToLog)- Parameters:
logger- loggerloggingLevel- logging levelmaximumBytesToLog- maximum number of bytes to log (may be0to avoid logging content)
-
-
Method Detail
-
write
public void write(int b)
- Overrides:
writein classjava.io.ByteArrayOutputStream
-
write
public void write(byte[] b, int off, int len)- Overrides:
writein classjava.io.ByteArrayOutputStream
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.ByteArrayOutputStream- Throws:
java.io.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
private static void appendBytes(java.lang.StringBuilder buf, int x)
-
-