Class Bytes

All Implemented Interfaces:
PointerInterface
Direct Known Subclasses:
Bytes

public class Bytes extends Record
A simple refcounted data type representing an immutable sequence of zero or
more bytes from an unspecified origin.

The purpose of a #GBytes is to keep the memory region that it holds
alive for as long as anyone holds a reference to the bytes. When
the last reference count is dropped, the memory is released. Multiple
unrelated callers can use byte data in the #GBytes without coordinating
their activities, resting assured that the byte data will not change or
move while they hold a reference.

A #GBytes can come from many different origins that may have
different procedures for freeing the memory region. Examples are
memory from g_malloc(), from memory slices, from a #GMappedFile or
memory from other allocators.

#GBytes work well as keys in #GHashTable. Use g_bytes_equal() and
g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
#GBytes can also be used as keys in a #GTree by passing the g_bytes_compare()
function to g_tree_new().

The data pointed to by this bytes must not be modified. For a mutable
array of bytes see #GByteArray. Use g_bytes_unref_to_array() to create a
mutable array for a #GBytes sequence. To create an immutable #GBytes from
a mutable #GByteArray, use the g_byte_array_free_to_bytes() function.

https://docs.gtk.org/glib/struct.Bytes.html

  • Constructor Details

    • Bytes

      public Bytes(PointerContainer pointer)
    • Bytes

      public Bytes(@Nullable Pointer data, long size)
      Creates a new #GBytes from @data.

      @data is copied. If @size is 0, @data may be %NULL.
      Parameters:
      data - the data to be used for the bytes
      size - the size of @data
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • newStaticBytes

      public static Bytes newStaticBytes(@Nullable Pointer data, long size)
      Creates a new #GBytes from static data.

      @data must be static (ie: never modified or freed). It may be %NULL if @size
      is 0.
      Parameters:
      data - the data to be used for the bytes
      size - the size of @data
      Returns:
      a new #GBytes
    • newTakeBytes

      public static Bytes newTakeBytes(@Nullable Pointer data, long size)
      Creates a new #GBytes from @data.

      After this call, @data belongs to the bytes and may no longer be
      modified by the caller. g_free() will be called on @data when the
      bytes is no longer in use. Because of this @data must have been created by
      a call to g_malloc(), g_malloc0() or g_realloc() or by one of the many
      functions that wrap these calls (such as g_new(), g_strdup(), etc).

      For creating #GBytes with memory from other allocators, see
      g_bytes_new_with_free_func().

      @data may be %NULL if @size is 0.
      Parameters:
      data - the data to be used for the bytes
      size - the size of @data
      Returns:
      a new #GBytes
    • newWithFreeFuncBytes

      public static Bytes newWithFreeFuncBytes(@Nullable Pointer data, long size, Bytes.OnDestroyNotify free_func, @Nullable Pointer user_data)
      Creates a #GBytes from @data.

      When the last reference is dropped, @free_func will be called with the
      @user_data argument.

      @data must not be modified after this call is made until @free_func has
      been called to indicate that the bytes is no longer in use.

      @data may be %NULL if @size is 0.
      Parameters:
      data - the data to be used for the bytes
      size - the size of @data
      free_func - the function to call to release the data
      user_data - data to pass to @free_func
      Returns:
      a new #GBytes
    • compare

      public int compare(@Nonnull Pointer bytes2)
      Compares the two #GBytes values.

      This function can be used to sort GBytes instances in lexicographical order.

      If @bytes1 and @bytes2 have different length but the shorter one is a
      prefix of the longer one then the shorter one is considered to be less than
      the longer one. Otherwise the first byte where both differ is used for
      comparison. If @bytes1 has a smaller value at that position it is
      considered less, otherwise greater than @bytes2.
      Parameters:
      bytes2 - a pointer to a #GBytes to compare with @bytes1
      Returns:
      a negative value if @bytes1 is less than @bytes2, a positive value if @bytes1 is greater than @bytes2, and zero if @bytes1 is equal to @bytes2
    • equal

      public boolean equal(@Nonnull Pointer bytes2)
      Compares the two #GBytes values being pointed to and returns
      %TRUE if they are equal.

      This function can be passed to g_hash_table_new() as the @key_equal_func
      parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
      Parameters:
      bytes2 - a pointer to a #GBytes to compare with @bytes1
      Returns:
      %TRUE if the two keys match.
    • getData

      public Pointer getData(@Nullable Int64 size)
      Get the byte data in the #GBytes. This data should not be modified.

      This function will always return the same pointer for a given #GBytes.

      %NULL may be returned if @size is 0. This is not guaranteed, as the #GBytes
      may represent an empty string with @data non-%NULL and @size as 0. %NULL will
      not be returned if @size is non-zero.
      Parameters:
      size - location to return size of byte data
      Returns:
      a pointer to the byte data, or %NULL
    • getRegion

      public Pointer getRegion(long element_size, long offset, long n_elements)
      Gets a pointer to a region in @bytes.

      The region starts at @offset many bytes from the start of the data
      and contains @n_elements many elements of @element_size size.

      @n_elements may be zero, but @element_size must always be non-zero.
      Ideally, @element_size is a static constant (eg: sizeof a struct).

      This function does careful bounds checking (including checking for
      arithmetic overflows) and returns a non-%NULL pointer if the
      specified region lies entirely within the @bytes. If the region is
      in some way out of range, or if an overflow has occurred, then %NULL
      is returned.

      Note: it is possible to have a valid zero-size region. In this case,
      the returned pointer will be equal to the base pointer of the data of
      @bytes, plus @offset. This will be non-%NULL except for the case
      where @bytes itself was a zero-sized region. Since it is unlikely
      that you will be using this function to check for a zero-sized region
      in a zero-sized @bytes, %NULL effectively always means "error".
      Parameters:
      element_size - a non-zero element size
      offset - an offset to the start of the region within the @bytes
      n_elements - the number of elements in the region
      Returns:
      the requested region, or %NULL in case of an error
    • getSize

      public long getSize()
      Get the size of the byte data in the #GBytes.

      This function will always return the same value for a given #GBytes.
      Returns:
      the size
    • hash

      public int hash()
      Creates an integer hash code for the byte data in the #GBytes.

      This function can be passed to g_hash_table_new() as the @key_hash_func
      parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
      Returns:
      a hash value corresponding to the key.
    • newFromBytes

      public Bytes newFromBytes(long offset, long length)
      Creates a #GBytes which is a subsection of another #GBytes. The @offset +
      @length may not be longer than the size of @bytes.

      A reference to @bytes will be held by the newly created #GBytes until
      the byte data is no longer needed.

      Since 2.56, if @offset is 0 and @length matches the size of @bytes, then
      @bytes will be returned with the reference count incremented by 1. If @bytes
      is a slice of another #GBytes, then the resulting #GBytes will reference
      the same #GBytes instead of @bytes. This allows consumers to simplify the
      usage of #GBytes when asynchronously writing to streams.
      Parameters:
      offset - offset which subsection starts at
      length - length of subsection
      Returns:
      a new #GBytes
    • ref

      public Bytes ref()
      Increase the reference count on @bytes.
      Returns:
      the #GBytes
    • unref

      public void unref()
      Releases a reference on @bytes. This may result in the bytes being
      freed. If @bytes is %NULL, it will return immediately.
    • unrefToArray

      public ByteArray unrefToArray()
      Unreferences the bytes, and returns a new mutable #GByteArray containing
      the same byte data.

      As an optimization, the byte data is transferred to the array without copying
      if this was the last reference to bytes and bytes was created with
      g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
      other cases the data is copied.

      Do not use it if @bytes contains more than %G_MAXUINT
      bytes. #GByteArray stores the length of its data in #guint, which
      may be shorter than #gsize, that @bytes is using.
      Returns:
      a new mutable #GByteArray containing the same byte data
    • unrefToData

      public Pointer unrefToData(@Nonnull Int64 size)
      Unreferences the bytes, and returns a pointer the same byte data
      contents.

      As an optimization, the byte data is returned without copying if this was
      the last reference to bytes and bytes was created with g_bytes_new(),
      g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the
      data is copied.
      Parameters:
      size - location to place the length of the returned data
      Returns:
      a pointer to the same byte data, which should be freed with g_free()
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()