Class MemoryOutputStream
- All Implemented Interfaces:
AutoCloseable, PollableOutputStream, Seekable, Proxy, AutoCloseable
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInner class implementing a builder pattern to construct a GObject with properties.static classNested classes/interfaces inherited from class OutputStream
OutputStream.OutputStream$Impl, OutputStream.OutputStreamClassNested classes/interfaces inherited from class GObject
GObject.NotifyCallback, GObject.ObjectClassNested classes/interfaces inherited from interface PollableOutputStream
PollableOutputStream.PollableOutputStream$Impl, PollableOutputStream.PollableOutputStreamInterfaceNested classes/interfaces inherited from interface Seekable
Seekable.Seekable$Impl, Seekable.SeekableIface -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new MemoryOutputStream.MemoryOutputStream(@Nullable MemorySegment data, long size, @Nullable ReallocFunc reallocFunction) Creates a newGMemoryOutputStream.MemoryOutputStream(MemorySegment address) Create a MemoryOutputStream instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected MemoryOutputStreamasParent()Return this instance as if it were its parent type.static MemoryOutputStream.Builder<? extends MemoryOutputStream.Builder> builder()AMemoryOutputStream.Builderobject constructs aMemoryOutputStreamwith the specified properties.@Nullable MemorySegmentgetData()Gets any loaded data from theostream.longReturns the number of bytes from the start up to including the last byte written in the stream that has not been truncated away.static MemoryLayoutThe memory layout of the native struct.longgetSize()Gets the size of the currently allocated data area (available from g_memory_output_stream_get_data()).static @Nullable TypegetType()Get the GType of the MemoryOutputStream class.static OutputStreamCreates a newGMemoryOutputStream, using g_realloc() and g_free() for memory allocation.byte[]Returns data from the this MemoryOutputStream as aGBytes.@Nullable MemorySegmentGets any loaded data from theostream.Ownership of the data is transferred to the caller; when no longer needed it must be freed using the free function set inostream'sGMemoryOutputStream:destroy-function property.Methods inherited from class OutputStream
clearPending, close, closeAsync, closeFinish, closeFn, flush, flushAsync, flushFinish, hasPending, isClosed, isClosing, printf, setPending, splice, splice, spliceAsync, spliceAsync, spliceFinish, write, writeAll, writeAllAsync, writeAllFinish, writeAsync, writeBytes, writeBytesAsync, writeBytesFinish, writeFinish, writev, writevAll, writevAllAsync, writevAllFinish, writevAsync, writevFinishMethods inherited from class GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class ProxyInstance
equals, handle, hashCodeMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AutoCloseable
closeMethods inherited from interface PollableOutputStream
canPoll, createSource, isWritable, writeNonblocking, writevNonblocking
-
Constructor Details
-
MemoryOutputStream
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
datais non-null, the stream will use that for its internal storage.If
reallocFnis 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.sizeis used only as a hint for how bigdatais. Specifically, seeking to the end of a newly-created stream will seek to zero, notsize.Seeking past the end of the stream and then writing will introduce a zero-filled gap.If
reallocFnisnullthen the stream is fixed-sized. Seeking to the end will seek tosizeexactly. 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 asdatabefore 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 thatdatais properly initialised.It is probably only meaningful to provide
dataandsizein the case that you want a fixed-sized stream. Put another way: ifreallocFnis non-nullthen it makes most sense to givedataasnullandsizeas 0 (allowingGMemoryOutputStreamto 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, ornullsize- the size ofdatareallocFunction- a function with realloc() semantics (like g_realloc()) to be called whendataneeds to be grown, ornull
-
MemoryOutputStream
public MemoryOutputStream()Create a new MemoryOutputStream.
-
-
Method Details
-
getType
Get the GType of the MemoryOutputStream class.- Returns:
- the GType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Return this instance as if it were its parent type. Comparable to the Javasuperkeyword, but ensures the parent typeclass is also used in native code.- Overrides:
asParentin classOutputStream- Returns:
- the instance as if it were its parent type
-
resizable
Creates a newGMemoryOutputStream, using g_realloc() and g_free() for memory allocation.- Since:
- 2.36
-
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
nullif 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 aGBytes. this MemoryOutputStream must be closed before calling this function.- Returns:
- the stream's data
- Since:
- 2.34
-
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 inostream'sGMemoryOutputStream:destroy-function property.this MemoryOutputStream must be closed before calling this function.
- Returns:
- the stream's data, or
nullif it has previously been stolen - Since:
- 2.26
-
builder
AMemoryOutputStream.Builderobject constructs aMemoryOutputStreamwith the specified properties. Use the variousset...()methods to set properties, and finish construction withMemoryOutputStream.Builder.build().- Returns:
- the builder object
-