Class DumpUtility
- java.lang.Object
-
- org.terracotta.utilities.io.buffer.DumpUtility
-
public final class DumpUtility extends java.lang.ObjectProvides methods to dumpBufferinstances.Each instance of this class tracks the buffers it dumps and does not dump the same buffer more than once. Because of this, an instance of this class should not be retained beyond actual need to avoid interfering with garbage collection of the dumped buffers.
Use of Reflection
This class uses reflection to access fields internal toBufferimplementations. Operation of the following methods requires this access:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDumpUtility.FieldInaccessibleExceptionThrown if reflective access to a required internal field fails.
-
Field Summary
Fields Modifier and Type Field Description private static java.nio.charset.CharsetEncoderASCII_ENCODERCharsetEncoderused to test bytes for printability.private java.util.Set<java.lang.Object>dumpedSetTracks objects already dumped.private java.lang.CharSequencelinePrefixThe prefix to prepend to each emitted line.private java.io.PrintStreamprintStreamThePrintStreamto which the dump is written.
-
Constructor Summary
Constructors Constructor Description DumpUtility(java.io.PrintStream printStream)Constructs a newDumpUtilityusing thePrintStreamprovided.DumpUtility(java.io.PrintStream printStream, java.lang.CharSequence linePrefix)Constructs a newDumpUtilityusing thePrintStreamand line prefix provided.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidaddGroupSpace(int i, java.lang.StringBuilder dumpBuilder, java.lang.StringBuilder charBuilder)voiddump(byte[] bytes)Writes the contents ofbyte[]to thePrintStreamprovided.voiddumpBuffer(java.nio.ByteBuffer buffer)Writes the contents ofByteBufferto thePrintStreamprovided.static voiddumpBuffer(java.nio.ByteBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.voiddumpBuffer(java.nio.IntBuffer buffer)Attempts to dump aIntBuffer.static voiddumpBuffer(java.nio.IntBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.voiddumpBuffer(java.nio.LongBuffer buffer)Attempts to dump aLongBuffer.static voiddumpBuffer(java.nio.LongBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.private voiddumpBufferInternal(java.nio.ByteBuffer view)private voidflushDumpLine(java.io.PrintStream out, java.lang.StringBuilder dumpBuilder, java.lang.StringBuilder charBuilder)Emit a dump line and prepare the buffers for the next.private <V,T>
VgetFieldValue(java.lang.Class<V> expectedType, T instance, java.lang.String fieldName)Gets the value of the designated field from the object instance provided.private <V,T>
VgetFieldValue(java.lang.Class<V> expectedType, T instance, java.lang.String fieldName, boolean quiet)Gets the value of the designated field from the object instance provided.private static java.lang.StringgetObjectId(java.lang.Object o)Gets an object identifier similar to the one produced byObject.toString.private booleanwasDumped(java.lang.Object o)Tracks objects dumped by thisDumpUtilityinstance to permit avoidance of duplicate dumps of data structures.
-
-
-
Field Detail
-
printStream
private final java.io.PrintStream printStream
ThePrintStreamto which the dump is written.
-
linePrefix
private final java.lang.CharSequence linePrefix
The prefix to prepend to each emitted line.
-
dumpedSet
private final java.util.Set<java.lang.Object> dumpedSet
Tracks objects already dumped.
-
ASCII_ENCODER
private static final java.nio.charset.CharsetEncoder ASCII_ENCODER
CharsetEncoderused to test bytes for printability.
-
-
Constructor Detail
-
DumpUtility
public DumpUtility(java.io.PrintStream printStream)
Constructs a newDumpUtilityusing thePrintStreamprovided. This constructor is equivalent tonew DumpUtility(printStream, "").- Parameters:
printStream- thePrintStreamto which the dump is written
-
DumpUtility
public DumpUtility(java.io.PrintStream printStream, java.lang.CharSequence linePrefix)Constructs a newDumpUtilityusing thePrintStreamand line prefix provided.- Parameters:
printStream- thePrintStreamto which the dump is writtenlinePrefix- the value to prepend to each dump line written
-
-
Method Detail
-
wasDumped
private boolean wasDumped(java.lang.Object o)
Tracks objects dumped by thisDumpUtilityinstance to permit avoidance of duplicate dumps of data structures. This method write a line toprintStream: if the object not been dumped, the line is suitable to identify the object being dumped; if the object has been dumped, the line is a reference to the identity previously displayed.- Parameters:
o- the object to track- Returns:
trueifowas previously dumped
-
dumpBuffer
public static void dumpBuffer(java.nio.IntBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theIntBufferto dumpprintStream- thePrintStreamto which the dump is written- Throws:
java.lang.UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
public static void dumpBuffer(java.nio.LongBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theLongBufferto dumpprintStream- thePrintStreamto which the dump is written- Throws:
java.lang.UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
public static void dumpBuffer(java.nio.ByteBuffer buffer, java.io.PrintStream printStream)A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theByteBufferto dumpprintStream- thePrintStreamto which the dump is written
-
dumpBuffer
public void dumpBuffer(java.nio.IntBuffer buffer)
Attempts to dump aIntBuffer. The present implementation of this method depends the use ofByteBuffer.asIntBufferin creation ofbufferand on the internal implementation ofByteBuffer.- Parameters:
buffer- theIntBufferinstance to dump- Throws:
java.lang.UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
public void dumpBuffer(java.nio.LongBuffer buffer)
Attempts to dump aLongBuffer. The present implementation of this method depends the use ofByteBuffer.asLongBufferin creation ofbufferand on the internal implementation ofByteBuffer.- Parameters:
buffer- theLongBufferinstance to dump- Throws:
java.lang.UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
public void dumpBuffer(java.nio.ByteBuffer buffer)
Writes the contents ofByteBufferto thePrintStreamprovided. The dump consists of a sequence of lines where each line is the dump of a 32-byte segment from the buffer and includes data in hexadecimal and in printable ASCII. A sample dump is shown:00000000 00000000 00000000 00000000 00000023 A5000000 00000000 00000000 00000000 *...............# ................* 00000020 00000000 00000020 00000000 00000023 A5000000 00000000 00000000 00000000 *....... .......# ................* 00000040-00000C7F duplicates above * * 00000C80 00000000 00000020 00000000 00000351 00000000 00000000 00000000 00000000 *....... .......Q ................* 00000CA0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 *................ ................* 00000CC0-00FFFFDF duplicates above * * 00FFFFE0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 *................ ................*For a direct
ByteBuffer, this method attempts to access the value of an internal field. Failure to access this field does not prevent this method from dumping theByteBuffer.- Parameters:
buffer- theByteBufferto dump; the buffer is printed from position zero (0) through the buffer limit using a view over the buffer
-
dump
public void dump(byte[] bytes)
Writes the contents ofbyte[]to thePrintStreamprovided. The dump consists of a sequence of lines where each line is the dump of a 32-byte segment from the array and includes data in hexadecimal and in printable ASCII. A sample dump is shown:00000000 00000000 00000000 00000000 00000023 A5000000 00000000 00000000 00000000 *...............# ................* 00000020 00000000 00000020 00000000 00000023 A5000000 00000000 00000000 00000000 *....... .......# ................* 00000040-00000C7F duplicates above * * 00000C80 00000000 00000020 00000000 00000351 00000000 00000000 00000000 00000000 *....... .......Q ................* 00000CA0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 *................ ................* 00000CC0-00FFFFDF duplicates above * * 00FFFFE0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 *................ ................*- Parameters:
bytes- thebytearray to dump
-
dumpBufferInternal
private void dumpBufferInternal(java.nio.ByteBuffer view)
-
addGroupSpace
private static void addGroupSpace(int i, java.lang.StringBuilder dumpBuilder, java.lang.StringBuilder charBuilder)
-
flushDumpLine
private void flushDumpLine(java.io.PrintStream out, java.lang.StringBuilder dumpBuilder, java.lang.StringBuilder charBuilder)Emit a dump line and prepare the buffers for the next.- Parameters:
out- thePrintStreamto which the dump line is writtendumpBuilder- theStringBuilderinto which the hex portion of the dump line, including the displacement, are recordedcharBuilder- theStringBuilderinto which the ASCII portion of the dump line is recorded
-
getFieldValue
private <V,T> V getFieldValue(java.lang.Class<V> expectedType, T instance, java.lang.String fieldName)Gets the value of the designated field from the object instance provided. This method expects a non-nullvalue in the field.- Type Parameters:
V- the declared type of the returned valueT- the declared type of the object from which the value is obtained- Parameters:
expectedType- the type to which the fetched value is castinstance- the instance from which the value is fetchedfieldName- the name of the field ininstanceholding the value- Returns:
- the value of the named field
- Throws:
DumpUtility.FieldInaccessibleException- if an error occurs while attempting to access the fieldjava.lang.AssertionError- if the fieldfieldNamecontainsnull
-
getFieldValue
private <V,T> V getFieldValue(java.lang.Class<V> expectedType, T instance, java.lang.String fieldName, boolean quiet)Gets the value of the designated field from the object instance provided. This method expects a non-nullvalue in the field.- Type Parameters:
V- the declared type of the returned valueT- the declared type of the object from which the value is obtained- Parameters:
expectedType- the type to which the fetched value is castinstance- the instance from which the value is fetchedfieldName- the name of the field ininstanceholding the valuequiet- iftrue, suppress the informational message related to the value fetch and permit the return of anullvalue- Returns:
- the value of the named field; may be
nullif, and only if,quietistrue - Throws:
DumpUtility.FieldInaccessibleException- if an error occurs while attempting to access the fieldjava.lang.AssertionError- if the fieldfieldNamecontainsnull
-
getObjectId
private static java.lang.String getObjectId(java.lang.Object o)
Gets an object identifier similar to the one produced byObject.toString.- Parameters:
o- the object for which the identifier is generated- Returns:
- the object identifier
-
-