Class DumpUtility
java.lang.Object
org.terracotta.utilities.io.buffer.DumpUtility
Provides methods to dump
Buffer instances.
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 toBuffer implementations.
Operation of the following methods requires this access:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThrown if reflective access to a required internal field fails. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final CharsetEncoderCharsetEncoderused to test bytes for printability.Tracks objects already dumped.private final CharSequenceThe prefix to prepend to each emitted line.private final PrintStreamThePrintStreamto which the dump is written. -
Constructor Summary
ConstructorsConstructorDescriptionDumpUtility(PrintStream printStream) Constructs a newDumpUtilityusing thePrintStreamprovided.DumpUtility(PrintStream printStream, CharSequence linePrefix) Constructs a newDumpUtilityusing thePrintStreamand line prefix provided. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidaddGroupSpace(int i, StringBuilder dumpBuilder, StringBuilder charBuilder) voiddump(byte[] bytes) Writes the contents ofbyte[]to thePrintStreamprovided.voiddumpBuffer(ByteBuffer buffer) Writes the contents ofByteBufferto thePrintStreamprovided.static voiddumpBuffer(ByteBuffer buffer, PrintStream printStream) A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.voiddumpBuffer(IntBuffer buffer) Attempts to dump aIntBuffer.static voiddumpBuffer(IntBuffer buffer, PrintStream printStream) A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.voiddumpBuffer(LongBuffer buffer) Attempts to dump aLongBuffer.static voiddumpBuffer(LongBuffer buffer, PrintStream printStream) A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.private voiddumpBufferInternal(ByteBuffer view) private voidflushDumpLine(PrintStream out, StringBuilder dumpBuilder, StringBuilder charBuilder) Emit a dump line and prepare the buffers for the next.private <V,T> V getFieldValue(Class<V> expectedType, T instance, String fieldName) Gets the value of the designated field from the object instance provided.private <V,T> V getFieldValue(Class<V> expectedType, T instance, String fieldName, boolean quiet) Gets the value of the designated field from the object instance provided.private static StringGets an object identifier similar to the one produced byObject.toString.private booleanTracks objects dumped by thisDumpUtilityinstance to permit avoidance of duplicate dumps of data structures.
-
Field Details
-
printStream
ThePrintStreamto which the dump is written. -
linePrefix
The prefix to prepend to each emitted line. -
dumpedSet
Tracks objects already dumped. -
ASCII_ENCODER
CharsetEncoderused to test bytes for printability.
-
-
Constructor Details
-
DumpUtility
Constructs a newDumpUtilityusing thePrintStreamprovided. This constructor is equivalent tonew DumpUtility(printStream, "").- Parameters:
printStream- thePrintStreamto which the dump is written
-
DumpUtility
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 Details
-
wasDumped
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
A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theIntBufferto dumpprintStream- thePrintStreamto which the dump is written- Throws:
UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theLongBufferto dumpprintStream- thePrintStreamto which the dump is written- Throws:
UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
A convenience method that allocates aDumpUtilityinstance, dumps a buffer, and discards theDumpUtilityinstance.- Parameters:
buffer- theByteBufferto dumpprintStream- thePrintStreamto which the dump is written
-
dumpBuffer
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:
UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
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:
UnsupportedOperationException- if thebufferis not a supported type or reflective access to a required field fails
-
dumpBuffer
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
-
addGroupSpace
-
flushDumpLine
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
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 fieldAssertionError- if the fieldfieldNamecontainsnull
-
getFieldValue
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 fieldAssertionError- if the fieldfieldNamecontainsnull
-
getObjectId
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
-