Class ByteSourceInverse


  • public final class ByteSourceInverse
    extends java.lang.Object
    Contains inverse transformation utilities for ByteSources. See ByteComparable.md for details about the encoding scheme.
    • Constructor Detail

      • ByteSourceInverse

        public ByteSourceInverse()
    • 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 next int from the current position of the given ByteSource. The source position is modified accordingly (moved 4 bytes forward).

        The source is not strictly required to represent just the encoding of an int value, so theoretically this API could be used for reading data in 4-byte strides. Nevertheless its usage is fairly limited because:

        1. ...it presupposes signed fixed-length encoding for the encoding of the original value
        2. ...it decodes the data returned on each stride as an int (i.e. it inverts its leading bit)
        3. ...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 next long from the current position of the given ByteSource. The source position is modified accordingly (moved 8 bytes forward).

        The source is not strictly required to represent just the encoding of a long value, so theoretically this API could be used for reading data in 8-byte strides. Nevertheless its usage is fairly limited because:

        1. ...it presupposes signed fixed-length encoding for the encoding of the original value
        2. ...it decodes the data returned on each stride as a long (i.e. it inverts its leading bit)
        3. ...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 given ByteSource to a byte.
        Parameters:
        byteSource - A non-null byte source, containing at least 1 byte.
      • 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.
      • 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 an ByteSource.END_OF_STREAM byte, after which it returns an array of all the read bytes, excluding the ByteSource.END_OF_STREAM.

        This method sizes a tentative internal buffer array at initialBufferCapacity. However, if byteSource exceeds this size, the buffer array is recreated with doubled capacity as many times as necessary. If, after byteSource is 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 null source, 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 an ByteSource.END_OF_STREAM byte, after which it returns an array of all the read bytes, excluding the ByteSource.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 null source, 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 given ByteSource to a UTF-8 String.
        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 null if it is.
      • nextComponentNull

        public static boolean nextComponentNull​(int separator)