Package org.apache.qpid.protonj2.buffer
Class ProtonBufferUtils
java.lang.Object
org.apache.qpid.protonj2.buffer.ProtonBufferUtils
Set of Utility methods useful when dealing with byte arrays and other
 primitive types.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe maximum buffer size that allows for JDK byte reservations on buffer size or addresses.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidcheckArgumentIsNotNegative(int value, String description) Checks the argument to determine if it less than zero and throws if it is.static voidcheckArgumentIsNotNegative(long value, String description) Checks the argument to determine if it less than zero and throws if it is.static voidcheckBufferCanGrowTo(int currentCapacity, int additional) Checks if a buffer can grow buffer the given amount or if that would exceed the maximum allowed buffer size.static voidcheckImplicitGrowthLimit(int implicitCapacity, int currentCapacity) Checks the implicit growth limit value for buffer implementationsstatic voidcheckIndexIsNotNegative(int value, String description) Checks the index to determine if it less than zero and throws if it is.static voidcheckIndexIsNotNegative(long value, String description) Checks the index to determine if it less than zero and throws if it is.static voidcheckIsClosed(ProtonBuffer buffer) Checks the given ProtonBuffer to see if it has already been closed.static voidcheckIsNotNegative(int value, String description) Checks the value to determine if it less than zero and throws if it is.static voidcheckIsNotNegative(long value, String description) Checks the value to determine if it less than zero and throws if it is.static voidcheckIsReadOnly(ProtonBuffer buffer) Checks the given ProtonBuffer to see if it has already been closed.static voidcheckLength(int length) Checks the length value is not negative and throws an exception if it is.static voidcheckOffset(int offset) Checks the offset value is not negative and throws an exception if it is.static intcompare(ProtonBuffer lhs, ProtonBuffer rhs) Compares twoProtonBufferinstances.static CharSequencecopyToCharSequence(ProtonBuffer source, int offset, int length, Charset charset) Copies the given length number of bytes from the provided buffer and returns aCharSequencethat is comprised of the characters of that sequence using the providedCharsetto make the transformation.static booleanequals(ProtonBuffer left, int leftStartIndex, ProtonBuffer right, int rightStartIndex, int length) Compares twoProtonBufferinstances for equality.static booleanequals(ProtonBuffer left, ProtonBuffer right) Compares twoProtonBufferinstances for equality.static booleanequals(ProtonBuffer left, ProtonBuffer right, int length) Compares twoProtonBufferinstances for equality.static ProtonBufferClosedExceptiongenericBufferIsClosed(ProtonBuffer buffer) genericBufferIsReadOnly(ProtonBuffer buffer) static IndexOutOfBoundsExceptiongenericOutOfBounds(ProtonBuffer buffer, int index) static CleanerCreate and / or return a Cleaner instance on demand and then serve out only that instance from then on.static inthashCode(ProtonBuffer buffer) Compute a hash code from the givenProtonBuffer.static bytereadByte(byte[] array, int offset) Reads a single byte from the given array from the provided offset.static charreadChar(byte[] array, int offset) Reads a two byte UTF-16 character from the given array from the provided offset.static CharSequencereadCharSequence(ProtonBuffer source, int length, Charset charset) Reads aCharSequencefrom the givenProtonBufferadvancing the read offset by the length value provided.static doublereadDouble(byte[] array, int offset) Reads an eight byte double precision value from the given array from the provided offset.static floatreadFloat(byte[] array, int offset) Reads a four byte floating point value from the given array from the provided offset.static intreadInt(byte[] array, int offset) Reads a four byte integer from the given array from the provided offset.static longreadLong(byte[] array, int offset) Reads an eight byte integer from the given array from the provided offset.static shortreadShort(byte[] array, int offset) Reads a two byte short from the given array from the provided offset.static intreadUnsignedByte(byte[] array, int offset) Reads a single byte from the given array from the provided offset and returns it as an integer that represents the unsigned byte value.static longreadUnsignedInt(byte[] array, int offset) Reads a four byte integer from the given array from the provided offset.static intreadUnsignedShort(byte[] array, int offset) Reads a two byte short from the given array from the provided offset and return it in an integer value that represents the unsigned short value.static Cleaner.CleanableregisterCleanup(Object observed, ProtonBuffer buffer) Register a cleanup watch on the given object which is related to theProtonBufferprovided and ensure that if the object does not close the buffer by the time it becomes unreachable that the buffer is closed.static voidsetCleaner(Cleaner cleaner) Allows an external applicationCleanerinstance to be assigned to the buffer utilities Cleaner instance which will then be used if a cleaner for aProtonBufferis registered.static byte[]toByteArray(byte value) Given a byte value returns an array containing the given byte as the only entry.static byte[]toByteArray(int value) Given an integer value returns an array containing the given byte as the only entry.static byte[]toByteArray(long value) Given a long value returns an array containing the given byte as the only entry.static byte[]toByteArray(short value) Given a short value returns an array containing the given byte as the only entry.static byte[]toByteArray(ByteBuffer buffer) Given aByteBufferreturns an array containing a copy of the readable bytes from the provided buffer.static byte[]toByteArray(ProtonBuffer buffer) Given aProtonBufferreturns an array containing a deep copy of the readable bytes from the provided buffer.static ByteBuffertoByteBuffer(ProtonBuffer buffer) Given aProtonBufferreturns anByteBuffercontaining a copy of the readable bytes from the provided buffer.static StringtoString(ProtonBuffer buffer, Charset charset) Create a new String that is a copy of the boffer's readable bytes and is defined by theCharsetprovided.static ProtonBufferAllocatorunclosable(ProtonBufferAllocator allocator) Creates a wrapper around the given allocator that prevents the close call from having any effect.static byte[]writeByte(byte value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeChar(char value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeDouble(double value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeFloat(float value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeInt(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeLong(long value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeShort(short value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeUnsignedByte(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeUnsignedInt(long value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.static byte[]writeUnsignedShort(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.
- 
Field Details- 
MAX_BUFFER_CAPACITYpublic static final int MAX_BUFFER_CAPACITYThe maximum buffer size that allows for JDK byte reservations on buffer size or addresses.- See Also:
 
 
- 
- 
Constructor Details- 
ProtonBufferUtilspublic ProtonBufferUtils()
 
- 
- 
Method Details- 
getCleanerCreate and / or return a Cleaner instance on demand and then serve out only that instance from then on.Care should be taken when using Cleaners as the instance will be tired to a thread that will run to perform the cleanups, an application is advised to assign a single global value for the whole application, - Returns:
- a Cleanerinstance which is created on demand or was already assigned.
 
- 
setCleanerAllows an external applicationCleanerinstance to be assigned to the buffer utilities Cleaner instance which will then be used if a cleaner for aProtonBufferis registered.- Parameters:
- cleaner- The cleaner to assign as the global- Cleanerfor buffer instances.
 
- 
registerCleanupRegister a cleanup watch on the given object which is related to theProtonBufferprovided and ensure that if the object does not close the buffer by the time it becomes unreachable that the buffer is closed.- Parameters:
- observed- The resource holder that if unreachable should release the given buffer
- buffer- The buffer to be closed when the observed object is unreachable.
- Returns:
- a Cleanerinstance the caller can invoke explicitly.
 
- 
toStringCreate a new String that is a copy of the boffer's readable bytes and is defined by theCharsetprovided.- Parameters:
- buffer- The buffer to convert to a string
- charset- The charset to use when creating the new string.
- Returns:
- a Stringthat is a view of the given buffer's readable bytes.
 
- 
toByteArrayGiven aProtonBufferreturns an array containing a deep copy of the readable bytes from the provided buffer.- Parameters:
- buffer- The buffer whose readable bytes are to be copied.
- Returns:
- a new array containing a copy of the readable bytes from the buffer.
 
- 
toByteArrayGiven aByteBufferreturns an array containing a copy of the readable bytes from the provided buffer.- Parameters:
- buffer- The buffer whose readable bytes are to be copied.
- Returns:
- a new array containing a copy of the readable bytes from the buffer.
 
- 
toByteBufferGiven aProtonBufferreturns anByteBuffercontaining a copy of the readable bytes from the provided buffer.- Parameters:
- buffer- The buffer whose readable bytes are to be copied.
- Returns:
- a new ByteBuffercontaining a copy of the readable bytes from the buffer.
 
- 
toByteArraypublic static byte[] toByteArray(byte value) Given a byte value returns an array containing the given byte as the only entry.- Parameters:
- value- The value to wrap in an array instance.
- Returns:
- a new array containing the primitive value as the only array element.
 
- 
toByteArraypublic static byte[] toByteArray(short value) Given a short value returns an array containing the given byte as the only entry.- Parameters:
- value- The value to wrap in an array instance.
- Returns:
- a new array containing the primitive value as the only array element.
 
- 
toByteArraypublic static byte[] toByteArray(int value) Given an integer value returns an array containing the given byte as the only entry.- Parameters:
- value- The value to wrap in an array instance.
- Returns:
- a new array containing the primitive value as the only array element.
 
- 
toByteArraypublic static byte[] toByteArray(long value) Given a long value returns an array containing the given byte as the only entry.- Parameters:
- value- The value to wrap in an array instance.
- Returns:
- a new array containing the primitive value as the only array element.
 
- 
writeCharpublic static byte[] writeChar(char value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeBytepublic static byte[] writeByte(byte value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeUnsignedBytepublic static byte[] writeUnsignedByte(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeShortpublic static byte[] writeShort(short value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeUnsignedShortpublic static byte[] writeUnsignedShort(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeIntpublic static byte[] writeInt(int value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeUnsignedIntpublic static byte[] writeUnsignedInt(long value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeLongpublic static byte[] writeLong(long value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeFloatpublic static byte[] writeFloat(float value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
writeDoublepublic static byte[] writeDouble(double value, byte[] destination, int offset) Writes the value given into the provided array at the specified offset returning to destination once done. The provided array must have enough space starting from the given offset for the value to be encoded or an exception will be thrown.- Parameters:
- value- The value to be encoded into the given array
- destination- The given array where the provided value should be written.
- offset- The offset into the array to start writing.
- Returns:
- the provided destination array.
 
- 
readBytepublic static byte readByte(byte[] array, int offset) Reads a single byte from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readUnsignedBytepublic static int readUnsignedByte(byte[] array, int offset) Reads a single byte from the given array from the provided offset and returns it as an integer that represents the unsigned byte value.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readShortpublic static short readShort(byte[] array, int offset) Reads a two byte short from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readUnsignedShortpublic static int readUnsignedShort(byte[] array, int offset) Reads a two byte short from the given array from the provided offset and return it in an integer value that represents the unsigned short value.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readCharpublic static char readChar(byte[] array, int offset) Reads a two byte UTF-16 character from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readIntpublic static int readInt(byte[] array, int offset) Reads a four byte integer from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readUnsignedIntpublic static long readUnsignedInt(byte[] array, int offset) Reads a four byte integer from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readLongpublic static long readLong(byte[] array, int offset) Reads an eight byte integer from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readFloatpublic static float readFloat(byte[] array, int offset) Reads a four byte floating point value from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
readDoublepublic static double readDouble(byte[] array, int offset) Reads an eight byte double precision value from the given array from the provided offset.- Parameters:
- array- The array to be read from
- offset- The offset into the array to start reading from.
- Returns:
- the resulting value read from the array at the provided array offset.
 
- 
checkLengthpublic static void checkLength(int length) Checks the length value is not negative and throws an exception if it is.- Parameters:
- length- The length value to be validated
 
- 
checkIsNotNegativeChecks the value to determine if it less than zero and throws if it is.- Parameters:
- value- The length value to be validated
- description- A description value appended to the exception string.
 
- 
checkIsNotNegativeChecks the value to determine if it less than zero and throws if it is.- Parameters:
- value- The length value to be validated
- description- A description value appended to the exception string.
 
- 
checkIndexIsNotNegativeChecks the index to determine if it less than zero and throws if it is.- Parameters:
- value- The index value to be validated
- description- A description value appended to the exception string.
 
- 
checkIndexIsNotNegativeChecks the index to determine if it less than zero and throws if it is.- Parameters:
- value- The index value to be validated
- description- A description value appended to the exception string.
 
- 
checkArgumentIsNotNegativeChecks the argument to determine if it less than zero and throws if it is.- Parameters:
- value- The argument value to be validated
- description- A description value appended to the exception string.
 
- 
checkArgumentIsNotNegativeChecks the argument to determine if it less than zero and throws if it is.- Parameters:
- value- The argument value to be validated
- description- A description value appended to the exception string.
 
- 
checkOffsetpublic static void checkOffset(int offset) Checks the offset value is not negative and throws an exception if it is.- Parameters:
- offset- The offset value to be validated
 
- 
checkIsClosedChecks the given ProtonBuffer to see if it has already been closed.- Parameters:
- buffer- The buffer to check if closed
 
- 
checkIsReadOnlyChecks the given ProtonBuffer to see if it has already been closed.- Parameters:
- buffer- The buffer to check if closed
 
- 
checkBufferCanGrowTopublic static void checkBufferCanGrowTo(int currentCapacity, int additional) Checks if a buffer can grow buffer the given amount or if that would exceed the maximum allowed buffer size.- Parameters:
- currentCapacity- The buffer's current capacity.
- additional- The amount of new space that will be added to the buffer.
 
- 
checkImplicitGrowthLimitpublic static void checkImplicitGrowthLimit(int implicitCapacity, int currentCapacity) Checks the implicit growth limit value for buffer implementations- Parameters:
- implicitCapacity- The intended implicit growth limit to assign
- currentCapacity- The current buffer capacity
 
- 
genericBufferIsClosed
- 
genericBufferIsReadOnly
- 
genericOutOfBounds
- 
equalsCompares twoProtonBufferinstances for equality.- Parameters:
- left- The left hand side buffer to compare.
- right- The right hand side buffer to compare.
- Returns:
- true if both buffers are equal.
 
- 
equalsCompares twoProtonBufferinstances for equality.- Parameters:
- left- The left hand side buffer to compare.
- right- The right hand side buffer to compare.
- length- The number of bytes in the two buffers to compare.
- Returns:
- true if both buffers are equal.
 
- 
equalspublic static boolean equals(ProtonBuffer left, int leftStartIndex, ProtonBuffer right, int rightStartIndex, int length) Compares twoProtonBufferinstances for equality.- Parameters:
- left- The left hand side buffer to compare.
- leftStartIndex- The index in the readable bytes of the left buffer to start the comparison
- right- The right hand side buffer to compare.
- rightStartIndex- The index in the readable bytes of the right buffer to start the comparison
- length- The number of bytes in the two buffers to compare.
- Returns:
- true if both buffers are equal.
 
- 
hashCodeCompute a hash code from the givenProtonBuffer.- Parameters:
- buffer- The buffer to compute the hash code for.
- Returns:
- the computed hash code for the given buffer
 
- 
compareCompares twoProtonBufferinstances.- Parameters:
- lhs- The left hand side buffer to compare
- rhs- The right hand side buffer to compare.
- Returns:
- the value 0 if x == y; a value less than 0 ifx < y; and a value greater than 0 ifx > y.
 
- 
readCharSequenceReads aCharSequencefrom the givenProtonBufferadvancing the read offset by the length value provided.- Parameters:
- source- The- ProtonBufferthat will provide the bytes to create the- CharSequence.
- length- The number of bytes to copy starting at the given offset.
- charset- The- Charsetto use to create the returned- CharSequence.
- Returns:
- a CharSequencethat is made up of the copied bytes using the providedCharset.
 
- 
copyToCharSequencepublic static CharSequence copyToCharSequence(ProtonBuffer source, int offset, int length, Charset charset) Copies the given length number of bytes from the provided buffer and returns aCharSequencethat is comprised of the characters of that sequence using the providedCharsetto make the transformation.- Parameters:
- source- The- ProtonBufferthat will provide the bytes to create the- CharSequence.
- offset- The offset into the given buffer where the copy of bytes should be started from.
- length- The number of bytes to copy starting at the given offset.
- charset- The- Charsetto use to create the returned- CharSequence.
- Returns:
- a CharSequencethat is made up of the copied bytes using the providedCharset.
 
- 
unclosableCreates a wrapper around the given allocator that prevents the close call from having any effect.Care should be taken to ensure that the allocator being wrapper is safe to leave unclosed or that the code closes it purposefully in some other context as certain wrapped allocators might require a close to free native resources. - Parameters:
- allocator- the- ProtonBufferAllocatorto wrap.
- Returns:
- a buffer allocator that cannot be closed.
 
 
-