com.google.api.client.util
Class LoggingByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by com.google.api.client.util.LoggingByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable

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(), 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
Author:
Yaniv Inbar

Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
LoggingByteArrayOutputStream(Logger logger, Level loggingLevel, int maximumBytesToLog)
           
 
Method Summary
 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)
           
 
Methods inherited from class java.io.ByteArrayOutputStream
reset, size, toByteArray, toString, toString, toString, writeTo
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LoggingByteArrayOutputStream

public LoggingByteArrayOutputStream(Logger logger,
                                    Level loggingLevel,
                                    int maximumBytesToLog)
Parameters:
logger - logger
loggingLevel - logging level
maximumBytesToLog - maximum number of bytes to log (may be 0 to avoid logging content)
Method Detail

write

public void write(int b)
Overrides:
write in class ByteArrayOutputStream

write

public void write(byte[] b,
                  int off,
                  int len)
Overrides:
write in class ByteArrayOutputStream

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class ByteArrayOutputStream
Throws:
IOException

getMaximumBytesToLog

public final int getMaximumBytesToLog()
Returns the maximum number of bytes to log (may be 0 to avoid logging content).


getBytesWritten

public final int getBytesWritten()
Returns the bytes written to the stream (may or may not have been logged).



Copyright © 2011-2012 Google. All Rights Reserved.