public class LoggingByteArrayOutputStream
extends java.io.ByteArrayOutputStream
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.
Constructor and Description |
---|
LoggingByteArrayOutputStream(java.util.logging.Logger logger,
java.util.logging.Level loggingLevel,
int maximumBytesToLog) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
int |
getBytesWritten()
Returns the bytes written to the stream (may or may not have been logged).
|
int |
getMaximumBytesToLog()
Returns the maximum number of bytes to log (may be
0 to avoid logging content). |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public LoggingByteArrayOutputStream(java.util.logging.Logger logger, java.util.logging.Level loggingLevel, int maximumBytesToLog)
logger
- loggerloggingLevel
- logging levelmaximumBytesToLog
- maximum number of bytes to log (may be 0
to avoid logging
content)public void write(int b)
write
in class java.io.ByteArrayOutputStream
public void write(byte[] b, int off, int len)
write
in class java.io.ByteArrayOutputStream
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.ByteArrayOutputStream
java.io.IOException
public final int getMaximumBytesToLog()
0
to avoid logging content).public final int getBytesWritten()
Copyright © 2011-2018 Google. All Rights Reserved.