Class MemoryOutputStream
- All Implemented Interfaces:
Proxy
,AutoCloseable
,AutoCloseable
,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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gio.OutputStream
OutputStream.OutputStreamClass, OutputStream.OutputStreamImpl
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gio.PollableOutputStream
PollableOutputStream.PollableOutputStreamImpl, PollableOutputStream.PollableOutputStreamInterface
Nested classes/interfaces inherited from interface org.gnome.gio.Seekable
Seekable.SeekableIface, Seekable.SeekableImpl
-
Constructor Summary
ConstructorDescriptionMemoryOutputStream
(@Nullable MemorySegment data, long size, @Nullable ReallocFunc reallocFunction) Creates a newGMemoryOutputStream
.MemoryOutputStream
(MemorySegment address) Create a MemoryOutputStream proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected MemoryOutputStream
asParent()
Returns this instance as if it were its parent type.static MemoryOutputStream.Builder
<? extends MemoryOutputStream.Builder> builder()
AMemoryOutputStream.Builder
object constructs aMemoryOutputStream
with the specified properties.getData()
Gets any loaded data from the this MemoryOutputStream.long
Returns the number of bytes from the start up to including the last byte written in the stream that has not been truncated away.static MemoryLayout
The memory layout of the native struct.long
getSize()
Gets the size of the currently allocated data area (available from g_memory_output_stream_get_data()).static Type
getType()
Get the GType of the MemoryOutputStream classstatic MemoryOutputStream
Creates a newGMemoryOutputStream
, using g_realloc() and g_free() for memory allocation.Returns data from the this MemoryOutputStream as aGBytes
.Gets any loaded data from the this MemoryOutputStream.Methods inherited from class org.gnome.gio.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, writevFinish
Methods inherited from class org.gnome.gobject.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, 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, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.jwharm.javagi.gio.AutoCloseable
close
Methods inherited from interface org.gnome.gio.PollableOutputStream
canPoll, createSource, isWritable, writeNonblocking, writevNonblocking
-
Constructor Details
-
MemoryOutputStream
Create a MemoryOutputStream proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
MemoryOutputStream
public MemoryOutputStream(@Nullable @Nullable MemorySegment data, long size, @Nullable @Nullable ReallocFunc reallocFunction) Creates a newGMemoryOutputStream
.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 bigdata
is. 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
reallocFn
isnull
then the stream is fixed-sized. Seeking to the end will seek tosize
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 asdata
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 thatdata
is properly initialised.It is probably only meaningful to provide
data
andsize
in the case that you want a fixed-sized stream. Put another way: ifreallocFn
is non-null
then it makes most sense to givedata
asnull
andsize
as 0 (allowingGMemoryOutputStream
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, ornull
size
- the size ofdata
reallocFunction
- a function with realloc() semantics (like g_realloc()) to be called whendata
needs to be grown, ornull
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParent
in classOutputStream
-
resizable
Creates a newGMemoryOutputStream
, using g_realloc() and g_free() for memory allocation. -
getData
Gets any loaded data from the this MemoryOutputStream.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
-
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
Returns data from the this MemoryOutputStream as aGBytes
. this MemoryOutputStream must be closed before calling this function.- Returns:
- the stream's data
-
stealData
Gets any loaded data from the this MemoryOutputStream. Ownership of the data is transferred to the caller; when no longer needed it must be freed using the free function set in this MemoryOutputStream'sGMemoryOutputStream
: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
-
builder
AMemoryOutputStream.Builder
object constructs aMemoryOutputStream
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withMemoryOutputStream.Builder.build()
.
-