Class MemoryOutputStream

All Implemented Interfaces:
AutoCloseable, PollableOutputStream, Seekable, Proxy, AutoCloseable

@Generated("org.javagi.JavaGI") public class MemoryOutputStream extends OutputStream implements PollableOutputStream, Seekable

GMemoryOutputStream is a class for using arbitrary memory chunks as output for GIO streaming output operations.

As of GLib 2.34, GMemoryOutputStream trivially implements PollableOutputStream: it always polls as ready.

  • Constructor Details

    • MemoryOutputStream

      public MemoryOutputStream(MemorySegment address)
      Create a MemoryOutputStream instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • MemoryOutputStream

      public MemoryOutputStream(@Nullable MemorySegment data, long size, @Nullable ReallocFunc reallocFunction)

      Creates a new GMemoryOutputStream.

      In most cases this is not the function you want. See g_memory_output_stream_new_resizable() instead.

      If data is non-null, the stream will use that for its internal storage.

      If reallocFn is non-null, it will be used for resizing the internal storage when necessary and the stream will be considered resizable. In that case, the stream will start out being (conceptually) empty. size is used only as a hint for how big data is. Specifically, seeking to the end of a newly-created stream will seek to zero, not size. Seeking past the end of the stream and then writing will introduce a zero-filled gap.

      If reallocFn is null then the stream is fixed-sized. Seeking to the end will seek to size exactly. Writing past the end will give an 'out of space' error. Attempting to seek past the end will fail. Unlike the resizable case, seeking to an offset within the stream and writing will preserve the bytes passed in as data before that point and will return them as part of g_memory_output_stream_steal_data(). If you intend to seek you should probably therefore ensure that data is properly initialised.

      It is probably only meaningful to provide data and size in the case that you want a fixed-sized stream. Put another way: if reallocFn is non-null then it makes most sense to give data as null and size as 0 (allowing GMemoryOutputStream to do the initial allocation for itself).

      // a stream that can grow
      stream = g_memory_output_stream_new (NULL, 0, realloc, free);
      
      // another stream that can grow
      stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
      
      // a fixed-size stream
      data = malloc (200);
      stream3 = g_memory_output_stream_new (data, 200, NULL, free);
      
      Parameters:
      data - pointer to a chunk of memory to use, or null
      size - the size of data
      reallocFunction - a function with realloc() semantics (like g_realloc()) to be called when data needs to be grown, or null
    • MemoryOutputStream

      public MemoryOutputStream()
      Create a new MemoryOutputStream.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the MemoryOutputStream class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected MemoryOutputStream asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class OutputStream
      Returns:
      the instance as if it were its parent type
    • resizable

      public static OutputStream resizable()
      Creates a new GMemoryOutputStream, using g_realloc() and g_free() for memory allocation.
      Since:
      2.36
    • getData

      public @Nullable MemorySegment getData()

      Gets any loaded data from the ostream.

      Note that the returned pointer may become invalid on the next write or truncate operation on the stream.

      Returns:
      pointer to the stream's data, or null if the data has been stolen
    • getDataSize

      public long getDataSize()
      Returns the number of bytes from the start up to including the last byte written in the stream that has not been truncated away.
      Returns:
      the number of bytes written to the stream
      Since:
      2.18
    • getSize

      public long getSize()

      Gets the size of the currently allocated data area (available from g_memory_output_stream_get_data()).

      You probably don't want to use this function on resizable streams. See g_memory_output_stream_get_data_size() instead. For resizable streams the size returned by this function is an implementation detail and may be change at any time in response to operations on the stream.

      If the stream is fixed-sized (ie: no realloc was passed to g_memory_output_stream_new()) then this is the maximum size of the stream and further writes will return IOErrorEnum.NO_SPACE.

      In any case, if you want the number of bytes currently written to the stream, use g_memory_output_stream_get_data_size().

      Returns:
      the number of bytes allocated for the data buffer
    • stealAsBytes

      public byte[] stealAsBytes()
      Returns data from the this MemoryOutputStream as a GBytes. this MemoryOutputStream must be closed before calling this function.
      Returns:
      the stream's data
      Since:
      2.34
    • stealData

      public @Nullable MemorySegment stealData()

      Gets any loaded data from the ostream. Ownership of the data is transferred to the caller; when no longer needed it must be freed using the free function set in ostream's GMemoryOutputStream:destroy-function property.

      this MemoryOutputStream must be closed before calling this function.

      Returns:
      the stream's data, or null if it has previously been stolen
      Since:
      2.26
    • builder

      public static MemoryOutputStream.Builder<? extends MemoryOutputStream.Builder> builder()
      A MemoryOutputStream.Builder object constructs a MemoryOutputStream with the specified properties. Use the various set...() methods to set properties, and finish construction with MemoryOutputStream.Builder.build().
      Returns:
      the builder object