Class IOStream
- All Implemented Interfaces:
AutoCloseable,Proxy,AutoCloseable
- Direct Known Subclasses:
FileIOStream,IOStream.IOStream$Impl,SimpleIOStream,SocketConnection,TlsConnection
GIOStream represents an object that has both read and write streams.
Generally the two streams act as separate input and output streams,
but they share some common resources and state. For instance, for
seekable streams, both streams may use the same position.
Examples of GIOStream objects are SocketConnection, which represents
a two-way network connection; and FileIOStream, which represents a
file handle opened in read-write mode.
To do the actual reading and writing you need to get the substreams
with getInputStream() and
getOutputStream().
The GIOStream object owns the input and the output streams, not the other
way around, so keeping the substreams alive will not keep the GIOStream
object alive. If the GIOStream object is freed it will be closed, thus
closing the substreams, so even if the substreams stay alive they will
always return G_IO_ERROR_CLOSED for all operations.
To close a stream use close(org.gnome.gio.Cancellable) which will close the common
stream object and also the individual substreams. You can also close
the substreams themselves. In most cases this only marks the
substream as closed, so further I/O on it fails but common state in the
GIOStream may still be open. However, some streams may support
‘half-closed’ states where one direction of the stream is actually shut down.
Operations on GIOStreams cannot be started while another operation on the
GIOStream or its substreams is in progress. Specifically, an application can
read from the InputStream and write to the
OutputStream simultaneously (either in separate threads, or as
asynchronous operations in the same thread), but an application cannot start
any GIOStream operation while there is a GIOStream, GInputStream or
GOutputStream operation in progress, and an application can’t start any
GInputStream or GOutputStream operation while there is a GIOStream
operation in progress.
This is a product of individual stream operations being associated with a
given GLib.MainContext (the thread-default context at the time the
operation was started), rather than entire streams being associated with a
single GMainContext.
GIO may run operations on GIOStreams from other (worker) threads, and this
may be exposed to application code in the behaviour of wrapper streams, such
as BufferedInputStream or TlsConnection. With such
wrapper APIs, application code may only run operations on the base (wrapped)
stream when the wrapper stream is idle. Note that the semantics of such
operations may not be well-defined due to the state the wrapper stream leaves
the base stream in (though they are guaranteed not to crash).
- Since:
- 2.22
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classIOStream.Builder<B extends IOStream.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classThe IOStream$Impl type represents a native instance of the abstract IOStream class.static classNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionIOStream()Creates a new IOStream.IOStream(MemorySegment address) Create a IOStream proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected IOStreamasParent()Returns this instance as if it were its parent type.voidClears the pending flag onstream.booleanclose(@Nullable Cancellable cancellable) Closes the stream, releasing resources related to it.voidcloseAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Requests an asynchronous close of the stream, releasing resources related to it.booleancloseFinish(AsyncResult result) Closes a stream.protected booleancloseFn(@Nullable Cancellable cancellable) Gets the input stream for this object.static MemoryLayoutThe memory layout of the native struct.Gets the output stream for this object.static @Nullable TypegetType()Get the GType of the IOStream classbooleanChecks if a stream has pending actions.booleanisClosed()Checks if a stream is closed.booleanSets this IOStream to have actions pending.voidspliceAsync(IOStream stream2, Set<IOStreamSpliceFlags> flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously splice the output stream of this IOStream to the input stream ofstream2,and splice the output stream ofstream2to the input stream ofstream1.voidspliceAsync(IOStream stream2, IOStreamSpliceFlags flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously splice the output stream of this IOStream to the input stream ofstream2,and splice the output stream ofstream2to the input stream ofstream1.static booleanspliceFinish(AsyncResult result) Finishes an asynchronous io stream splice operation.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, builder, 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 org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCodeMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.javagi.gio.AutoCloseable
close
-
Constructor Details
-
IOStream
Create a IOStream proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
IOStream
public IOStream()Creates a new IOStream.
-
-
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 Javasuperkeyword, 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. -
spliceFinish
Finishes an asynchronous io stream splice operation.- Parameters:
result- aGAsyncResult.- Returns:
trueon success,falseotherwise.- Throws:
GErrorException- seeGError- Since:
- 2.28
-
clearPending
public void clearPending()Clears the pending flag onstream.- Since:
- 2.22
-
close
Closes the stream, releasing resources related to it. This will also close the individual input and output streams, if they are not already closed.Once the stream is closed, all other operations will return
IOErrorEnum.CLOSED. Closing a stream multiple times will not return an error.Closing a stream will automatically flush any outstanding buffers in the stream.
Streams will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.
Some streams might keep the backing store of the stream (e.g. a file descriptor) open after the stream is closed. See the documentation for the individual stream for details.
On failure the first error that happened will be reported, but the close operation will finish as much as possible. A stream that failed to close will still return
IOErrorEnum.CLOSEDfor all operations. Still, it is important to check and report the error to the user, otherwise there might be a loss of data as all data might not be written.If
cancellableis not NULL, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill be returned. Cancelling a close will still leave the stream closed, but some streams can use a faster close that doesn't block to e.g. check errors.The default implementation of this method just calls close on the individual input/output streams.
- Specified by:
closein interfaceAutoCloseable- Parameters:
cancellable- optionalGCancellableobject,nullto ignore- Returns:
trueon success,falseon failure- Throws:
GErrorException- seeGError- Since:
- 2.22
-
closeAsync
public void closeAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Requests an asynchronous close of the stream, releasing resources related to it. When the operation is finishedcallbackwill be called. You can then call g_io_stream_close_finish() to get the result of the operation.For behaviour details see g_io_stream_close().
The asynchronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.
- Parameters:
ioPriority- the io priority of the requestcancellable- optional cancellable objectcallback- aGAsyncReadyCallbackto call when the request is satisfied- Since:
- 2.22
-
closeFinish
Closes a stream.- Parameters:
result- aGAsyncResult- Returns:
trueif stream was successfully closed,falseotherwise.- Throws:
GErrorException- seeGError- Since:
- 2.22
-
getInputStream
Gets the input stream for this object. This is used for reading.- Returns:
- a
GInputStream, owned by theGIOStream. Do not free. - Since:
- 2.22
-
getOutputStream
Gets the output stream for this object. This is used for writing.- Returns:
- a
GOutputStream, owned by theGIOStream. Do not free. - Since:
- 2.22
-
hasPending
public boolean hasPending()Checks if a stream has pending actions.- Returns:
trueif this IOStream has pending actions.- Since:
- 2.22
-
isClosed
public boolean isClosed()Checks if a stream is closed.- Returns:
trueif the stream is closed.- Since:
- 2.22
-
setPending
Sets this IOStream to have actions pending. If the pending flag is already set or this IOStream is closed, it will returnfalseand seterror.- Returns:
trueif pending was previously unset and is now set.- Throws:
GErrorException- seeGError- Since:
- 2.22
-
spliceAsync
public void spliceAsync(IOStream stream2, Set<IOStreamSpliceFlags> flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously splice the output stream of this IOStream to the input stream ofstream2,and splice the output stream ofstream2to the input stream ofstream1.When the operation is finished
callbackwill be called. You can then call g_io_stream_splice_finish() to get the result of the operation.- Parameters:
stream2- aGIOStream.flags- a set ofGIOStreamSpliceFlags.ioPriority- the io priority of the request.cancellable- optionalGCancellableobject,nullto ignore.callback- aGAsyncReadyCallbackto call when the request is satisfied- Since:
- 2.28
-
spliceAsync
public void spliceAsync(IOStream stream2, IOStreamSpliceFlags flags, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously splice the output stream of this IOStream to the input stream ofstream2,and splice the output stream ofstream2to the input stream ofstream1.When the operation is finished
callbackwill be called. You can then call g_io_stream_splice_finish() to get the result of the operation.- Parameters:
stream2- aGIOStream.flags- a set ofGIOStreamSpliceFlags.ioPriority- the io priority of the request.cancellable- optionalGCancellableobject,nullto ignore.callback- aGAsyncReadyCallbackto call when the request is satisfied- Since:
- 2.28
-
closeFn
- Throws:
GErrorException
-