Class ByteSourceInverse
- java.lang.Object
-
- org.apache.cassandra.utils.bytecomparable.ByteSourceInverse
-
public final class ByteSourceInverse extends java.lang.ObjectContains inverse transformation utilities forByteSources. See ByteComparable.md for details about the encoding scheme.
-
-
Constructor Summary
Constructors Constructor Description ByteSourceInverse()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopyBytes(ByteSource byteSource, byte[] bytes)static <V> VgetFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)Consume the next length bytes from the source unchanged.static <V> VgetOptionalFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)Consume the next length bytes from the source unchanged, also translating null to an empty buffer.static <V> VgetOptionalSignedFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)Produce the bytes for an encoded signed fixed-length number, also translating null to empty buffer.static <V> VgetOptionalSignedFixedLengthFloat(ValueAccessor<V> accessor, ByteSource byteSource, int length)Produce the bytes for an encoded signed fixed-length floating-point number, also translating null to an empty buffer.static bytegetSignedByte(ByteSource byteSource)Converts the givenByteSourceto abyte.static <V> VgetSignedFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)Produce the bytes for an encoded signed fixed-length number.static <V> VgetSignedFixedLengthFloat(ValueAccessor<V> accessor, ByteSource byteSource, int length)Produce the bytes for an encoded signed fixed-length floating-point number.static intgetSignedInt(ByteSource byteSource)Consume the nextintfrom the current position of the givenByteSource.static longgetSignedLong(ByteSource byteSource)Consume the nextlongfrom the current position of the givenByteSource.static shortgetSignedShort(ByteSource byteSource)Converts the givenByteSourceto ashort.static java.lang.StringgetString(ByteSource.Peekable byteSource)Converts the givenByteSourceto a UTF-8String.static byte[]getUnescapedBytes(ByteSource.Peekable byteSource)Reads a single variable-length byte sequence (blob, string, ...) encoded according to the scheme described in ByteComparable.md, decoding it back to its original, unescaped form.static longgetUnsignedFixedLengthAsLong(ByteSource byteSource, int length)Consume the given number of bytes and produce a long from them, effectively treating the bytes as a big-endian unsigned encoding of the number.static longgetVariableLengthInteger(ByteSource byteSource)Decode a variable-length signed integer.static longgetVariableLengthUnsignedIntegerXoring(ByteSource byteSource, int xorWith)Decode a variable-length unsigned integer, passing all bytes read through XOR with the given xorWith parameter.static booleannextComponentNull(int separator)static ByteSource.PeekablenextComponentSource(ByteSource.Peekable source)A utility for consuming components from a peekable multi-component sequence.static ByteSource.PeekablenextComponentSource(ByteSource.Peekable source, int separator)A utility for consuming components from a peekable multi-component sequence, very similar tonextComponentSource(ByteSource.Peekable)- the difference being that here the separator can be passed in case it had to be consumed beforehand.static byte[]readBytes(ByteSource byteSource)Reads the bytes of the given source into a byte array.static byte[]readBytes(ByteSource byteSource, int initialBufferCapacity)Reads the bytes of the given source into a byte array.static ByteSourceunescape(ByteSource.Peekable byteSource)As above, but converts the result to a ByteSource.
-
-
-
Method Detail
-
getUnsignedFixedLengthAsLong
public static long getUnsignedFixedLengthAsLong(ByteSource byteSource, int length)
Consume the given number of bytes and produce a long from them, effectively treating the bytes as a big-endian unsigned encoding of the number.
-
getSignedFixedLength
public static <V> V getSignedFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Produce the bytes for an encoded signed fixed-length number. The first byte has its sign bit inverted, and the rest are passed unchanged.
-
getOptionalSignedFixedLength
public static <V> V getOptionalSignedFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Produce the bytes for an encoded signed fixed-length number, also translating null to empty buffer. The first byte has its sign bit inverted, and the rest are passed unchanged.
-
getSignedFixedLengthFloat
public static <V> V getSignedFixedLengthFloat(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Produce the bytes for an encoded signed fixed-length floating-point number. If sign bit is on, returns negated bytes. If not, clears the sign bit and passes the rest of the bytes unchanged.
-
getOptionalSignedFixedLengthFloat
public static <V> V getOptionalSignedFixedLengthFloat(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Produce the bytes for an encoded signed fixed-length floating-point number, also translating null to an empty buffer. If sign bit is on, returns negated bytes. If not, clears the sign bit and passes the rest of the bytes unchanged.
-
getFixedLength
public static <V> V getFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Consume the next length bytes from the source unchanged.
-
getOptionalFixedLength
public static <V> V getOptionalFixedLength(ValueAccessor<V> accessor, ByteSource byteSource, int length)
Consume the next length bytes from the source unchanged, also translating null to an empty buffer.
-
getSignedInt
public static int getSignedInt(ByteSource byteSource)
Consume the nextintfrom the current position of the givenByteSource. The source position is modified accordingly (moved 4 bytes forward).The source is not strictly required to represent just the encoding of an
intvalue, so theoretically this API could be used for reading data in 4-byte strides. Nevertheless its usage is fairly limited because:- ...it presupposes signed fixed-length encoding for the encoding of the original value
- ...it decodes the data returned on each stride as an
int(i.e. it inverts its leading bit) - ...it doesn't provide any meaningful guarantees (with regard to throwing) in case there are not enough bytes to read, in case a special escape value was not interpreted as such, etc.
- Parameters:
byteSource- A non-null byte source, containing at least 4 bytes.
-
getSignedLong
public static long getSignedLong(ByteSource byteSource)
Consume the nextlongfrom the current position of the givenByteSource. The source position is modified accordingly (moved 8 bytes forward).The source is not strictly required to represent just the encoding of a
longvalue, so theoretically this API could be used for reading data in 8-byte strides. Nevertheless its usage is fairly limited because:- ...it presupposes signed fixed-length encoding for the encoding of the original value
- ...it decodes the data returned on each stride as a
long(i.e. it inverts its leading bit) - ...it doesn't provide any meaningful guarantees (with regard to throwing) in case there are not enough bytes to read, in case a special escape value was not interpreted as such, etc.
- Parameters:
byteSource- A non-null byte source, containing at least 8 bytes.
-
getSignedByte
public static byte getSignedByte(ByteSource byteSource)
Converts the givenByteSourceto abyte.- Parameters:
byteSource- A non-null byte source, containing at least 1 byte.
-
getSignedShort
public static short getSignedShort(ByteSource byteSource)
Converts the givenByteSourceto ashort. All terms and conditions valid forgetSignedInt(ByteSource)andgetSignedLong(ByteSource)translate to this as well.- Parameters:
byteSource- A non-null byte source, containing at least 2 bytes.- See Also:
getSignedInt(ByteSource),getSignedLong(ByteSource)
-
getVariableLengthInteger
public static long getVariableLengthInteger(ByteSource byteSource)
Decode a variable-length signed integer.
-
getVariableLengthUnsignedIntegerXoring
public static long getVariableLengthUnsignedIntegerXoring(ByteSource byteSource, int xorWith)
Decode a variable-length unsigned integer, passing all bytes read through XOR with the given xorWith parameter. Used in BigInteger encoding to read number length, where negative numbers have their length negated (i.e. xorWith = 0xFF) to ensure correct ordering.
-
getUnescapedBytes
public static byte[] getUnescapedBytes(ByteSource.Peekable byteSource)
Reads a single variable-length byte sequence (blob, string, ...) encoded according to the scheme described in ByteComparable.md, decoding it back to its original, unescaped form.- Parameters:
byteSource- The source of the variable-length bytes sequence.- Returns:
- A byte array containing the original, unescaped bytes of the given source. Unescaped here means not including any of the escape sequences of the encoding scheme used for variable-length byte sequences.
-
unescape
public static ByteSource unescape(ByteSource.Peekable byteSource)
As above, but converts the result to a ByteSource.
-
readBytes
public static byte[] readBytes(ByteSource byteSource, int initialBufferCapacity)
Reads the bytes of the given source into a byte array. Doesn't do any transformation on the bytes, just reads them until it reads anByteSource.END_OF_STREAMbyte, after which it returns an array of all the read bytes, excluding theByteSource.END_OF_STREAM.This method sizes a tentative internal buffer array at
initialBufferCapacity. However, ifbyteSourceexceeds this size, the buffer array is recreated with doubled capacity as many times as necessary. If, afterbyteSourceis fully exhausted, the number of bytes read from it does not exactly match the current size of the tentative buffer array, then it is copied into another array sized to fit the number of bytes read; otherwise, it is returned without that final copy step.- Parameters:
byteSource- The source which bytes we're interested in.initialBufferCapacity- The initial size of the internal buffer.- Returns:
- A byte array containing exactly all the read bytes. In case of a
nullsource, the returned byte array will be empty.
-
readBytes
public static byte[] readBytes(ByteSource byteSource)
Reads the bytes of the given source into a byte array. Doesn't do any transformation on the bytes, just reads them until it reads anByteSource.END_OF_STREAMbyte, after which it returns an array of all the read bytes, excluding theByteSource.END_OF_STREAM.This is equivalent to
readBytes(ByteSource, int)where the second actual parameter is INITIAL_BUFFER_CAPACITY (32).- Parameters:
byteSource- The source which bytes we're interested in.- Returns:
- A byte array containing exactly all the read bytes. In case of a
nullsource, the returned byte array will be empty.
-
copyBytes
public static void copyBytes(ByteSource byteSource, byte[] bytes)
-
getString
public static java.lang.String getString(ByteSource.Peekable byteSource)
Converts the givenByteSourceto a UTF-8String.- Parameters:
byteSource- The source we're interested in.- Returns:
- A UTF-8 string corresponding to the given source.
-
nextComponentSource
public static ByteSource.Peekable nextComponentSource(ByteSource.Peekable source)
A utility for consuming components from a peekable multi-component sequence. It uses the component separators, so the given sequence needs to have its last component fully consumed, in order for the next consumable byte to be a separator. Identifying the end of the component that will then be consumed is the responsibility of the consumer (the user of this method).- Parameters:
source- A peekable multi-component sequence, which next byte is a component separator.- Returns:
- the given multi-component sequence if its next component is not null, or
nullif it is.
-
nextComponentSource
public static ByteSource.Peekable nextComponentSource(ByteSource.Peekable source, int separator)
A utility for consuming components from a peekable multi-component sequence, very similar tonextComponentSource(ByteSource.Peekable)- the difference being that here the separator can be passed in case it had to be consumed beforehand.
-
nextComponentNull
public static boolean nextComponentNull(int separator)
-
-