Class InputStream
- All Implemented Interfaces:
Proxy
,AutoCloseable
,AutoCloseable
- Direct Known Subclasses:
FileInputStream
,FilterInputStream
,InputStream.InputStreamImpl
,MemoryInputStream
,PollableInputStream.PollableInputStreamImpl
,UnixInputStream
,Win32InputStream
GInputStream
is a base class for implementing streaming input.
It has functions to read from a stream (read(io.github.jwharm.javagi.base.Out<byte[]>, org.gnome.gio.Cancellable)
),
to close a stream (close(org.gnome.gio.Cancellable)
) and to skip some content
(skip(long, org.gnome.gio.Cancellable)
).
To copy the content of an input stream to an output stream without
manually handling the reads and writes, use OutputStream.splice(org.gnome.gio.InputStream, java.util.Set<org.gnome.gio.OutputStreamSpliceFlags>, org.gnome.gio.Cancellable)
.
See the documentation for IOStream
for details of thread safety
of streaming APIs.
All of these functions have async variants too.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
InputStream.Builder<B extends InputStream.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
static class
The InputStreamImpl type represents a native instance of the abstract InputStream class.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionInputStream
(MemorySegment address) Create a InputStream proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected InputStream
asParent()
Returns this instance as if it were its parent type.static InputStream.Builder
<? extends InputStream.Builder> builder()
AInputStream.Builder
object constructs aInputStream
with the specified properties.void
Clears the pending flag on this InputStream.boolean
close
(@Nullable Cancellable cancellable) Closes the stream, releasing resources related to it.void
closeAsync
(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Requests an asynchronous closes of the stream, releasing resources related to it.boolean
closeFinish
(AsyncResult result) Finishes closing a stream asynchronously, started from g_input_stream_close_async().protected boolean
closeFn
(@Nullable Cancellable cancellable) static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the InputStream classboolean
Checks if an input stream has pending actions.boolean
isClosed()
Checks if an input stream is closed.long
read
(Out<byte[]> buffer, @Nullable Cancellable cancellable) Tries to readcount
bytes from the stream into the buffer starting atbuffer
.boolean
readAll
(Out<byte[]> buffer, Out<Long> bytesRead, @Nullable Cancellable cancellable) Tries to readcount
bytes from the stream into the buffer starting atbuffer
.void
readAllAsync
(Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into the buffer starting atbuffer
.boolean
readAllFinish
(AsyncResult result, Out<Long> bytesRead) Finishes an asynchronous stream read operation started with g_input_stream_read_all_async().void
readAsync
(Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into the buffer starting atbuffer
.readBytes
(long count, @Nullable Cancellable cancellable) Like g_input_stream_read(), this tries to readcount
bytes from the stream in a blocking fashion.void
readBytesAsync
(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into a newGBytes
.readBytesFinish
(AsyncResult result) Finishes an asynchronous stream read-into-GBytes
operation.long
readFinish
(AsyncResult result) Finishes an asynchronous stream read operation.protected long
readFn
(@Nullable MemorySegment buffer, long count, @Nullable Cancellable cancellable) boolean
Sets this InputStream to have actions pending.long
skip
(long count, @Nullable Cancellable cancellable) Tries to skipcount
bytes from the stream.void
skipAsync
(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous skip ofcount
bytes from the stream.long
skipFinish
(AsyncResult result) Finishes a stream skip operation.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
-
Constructor Details
-
InputStream
Create a InputStream proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
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. -
clearPending
public void clearPending()Clears the pending flag on this InputStream. -
close
Closes the stream, releasing resources related to it.Once the stream is closed, all other operations will return
IOErrorEnum.CLOSED
. Closing a stream multiple times will not return an error.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.CLOSED
for all operations. Still, it is important to check and report the error to the user.If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will 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.- Specified by:
close
in interfaceAutoCloseable
- Parameters:
cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
true
on success,false
on failure- Throws:
GErrorException
- seeGError
-
closeAsync
public void closeAsync(int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Requests an asynchronous closes of the stream, releasing resources related to it. When the operation is finishedcallback
will be called. You can then call g_input_stream_close_finish() to get the result of the operation.For behaviour details see g_input_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 I/O priority of the requestcancellable
- optional cancellable objectcallback
- aGAsyncReadyCallback
to call when the request is satisfied
-
closeFinish
Finishes closing a stream asynchronously, started from g_input_stream_close_async().- Parameters:
result
- aGAsyncResult
.- Returns:
true
if the stream was closed successfully.- Throws:
GErrorException
- seeGError
-
hasPending
public boolean hasPending()Checks if an input stream has pending actions.- Returns:
true
if this InputStream has pending actions.
-
isClosed
public boolean isClosed()Checks if an input stream is closed.- Returns:
true
if the stream is closed.
-
read
public long read(Out<byte[]> buffer, @Nullable @Nullable Cancellable cancellable) throws GErrorException Tries to readcount
bytes from the stream into the buffer starting atbuffer
. Will block during this read.If count is zero returns zero and does nothing. A value of
count
larger thanG_MAXSSIZE
will cause aIOErrorEnum.INVALID_ARGUMENT
error.On success, the number of bytes read into the buffer is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. near the end of a file. Zero is returned on end of file (or if
count
is zero), but never otherwise.The returned
buffer
is not a nul-terminated string, it can contain nul bytes at any position, and this function doesn't nul-terminate thebuffer
.If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.On error -1 is returned and
error
is set accordingly.- Parameters:
buffer
- a buffer to read data into (which should be at least count bytes long).cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- Number of bytes read, or -1 on error, or 0 on end of file.
- Throws:
GErrorException
- seeGError
-
readAll
public boolean readAll(Out<byte[]> buffer, Out<Long> bytesRead, @Nullable @Nullable Cancellable cancellable) throws GErrorException Tries to readcount
bytes from the stream into the buffer starting atbuffer
. Will block during this read.This function is similar to g_input_stream_read(), except it tries to read as many bytes as requested, only stopping on an error or end of stream.
On a successful read of
count
bytes, or if we reached the end of the stream,true
is returned, andbytesRead
is set to the number of bytes read intobuffer
.If there is an error during the operation
false
is returned anderror
is set to indicate the error status.As a special exception to the normal conventions for functions that use
GError
, if this function returnsfalse
(and setserror
) thenbytesRead
will be set to the number of bytes that were successfully read before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_input_stream_read().- Parameters:
buffer
- a buffer to read data into (which should be at least count bytes long).bytesRead
- location to store the number of bytes that was read from the streamcancellable
- optionalGCancellable
object,null
to ignore.- Returns:
true
on success,false
if there was an error- Throws:
GErrorException
- seeGError
-
readAllAsync
public void readAllAsync(Out<byte[]> buffer, int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into the buffer starting atbuffer
.This is the asynchronous equivalent of g_input_stream_read_all().
Call g_input_stream_read_all_finish() to collect the result.
Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is
G_PRIORITY_DEFAULT
.- Parameters:
buffer
- a buffer to read data into (which should be at least count bytes long)ioPriority
- the I/O priority of the requestcancellable
- optionalGCancellable
object,null
to ignorecallback
- aGAsyncReadyCallback
to call when the request is satisfied
-
readAllFinish
Finishes an asynchronous stream read operation started with g_input_stream_read_all_async().As a special exception to the normal conventions for functions that use
GError
, if this function returnsfalse
(and setserror
) thenbytesRead
will be set to the number of bytes that were successfully read before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_input_stream_read_async().- Parameters:
result
- aGAsyncResult
bytesRead
- location to store the number of bytes that was read from the stream- Returns:
true
on success,false
if there was an error- Throws:
GErrorException
- seeGError
-
readAsync
public void readAsync(Out<byte[]> buffer, int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into the buffer starting atbuffer
. When the operation is finishedcallback
will be called. You can then call g_input_stream_read_finish() to get the result of the operation.During an async request no other sync and async calls are allowed on this InputStream, and will result in
IOErrorEnum.PENDING
errors.A value of
count
larger thanG_MAXSSIZE
will cause aIOErrorEnum.INVALID_ARGUMENT
error.On success, the number of bytes read into the buffer will be passed to the callback. It is not an error if this is not the same as the requested size, as it can happen e.g. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or if
count
is zero), but never otherwise.Any outstanding i/o request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is
G_PRIORITY_DEFAULT
.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:
buffer
- a buffer to read data into (which should be at least count bytes long).ioPriority
- the I/O priority of the request.cancellable
- optionalGCancellable
object,null
to ignore.callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
readBytes
public Bytes readBytes(long count, @Nullable @Nullable Cancellable cancellable) throws GErrorException Like g_input_stream_read(), this tries to readcount
bytes from the stream in a blocking fashion. However, rather than reading into a user-supplied buffer, this will create a newGBytes
containing the data that was read. This may be easier to use from language bindings.If count is zero, returns a zero-length
GBytes
and does nothing. A value ofcount
larger thanG_MAXSSIZE
will cause aIOErrorEnum.INVALID_ARGUMENT
error.On success, a new
GBytes
is returned. It is not an error if the size of this object is not the same as the requested size, as it can happen e.g. near the end of a file. A zero-lengthGBytes
is returned on end of file (or ifcount
is zero), but never otherwise.If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.On error
null
is returned anderror
is set accordingly.- Parameters:
count
- maximum number of bytes that will be read from the stream. Common values include 4096 and 8192.cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- a new
GBytes
, ornull
on error - Throws:
GErrorException
- seeGError
-
readBytesAsync
public void readBytesAsync(long count, int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcount
bytes from the stream into a newGBytes
. When the operation is finishedcallback
will be called. You can then call g_input_stream_read_bytes_finish() to get the result of the operation.During an async request no other sync and async calls are allowed on this InputStream, and will result in
IOErrorEnum.PENDING
errors.A value of
count
larger thanG_MAXSSIZE
will cause aIOErrorEnum.INVALID_ARGUMENT
error.On success, the new
GBytes
will be passed to the callback. It is not an error if this is smaller than the requested size, as it can happen e.g. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or ifcount
is zero), but never otherwise.Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is
G_PRIORITY_DEFAULT
.- Parameters:
count
- the number of bytes that will be read from the streamioPriority
- the I/O priority of the requestcancellable
- optionalGCancellable
object,null
to ignore.callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
readBytesFinish
Finishes an asynchronous stream read-into-GBytes
operation.- Parameters:
result
- aGAsyncResult
.- Returns:
- the newly-allocated
GBytes
, ornull
on error - Throws:
GErrorException
- seeGError
-
readFinish
Finishes an asynchronous stream read operation.- Parameters:
result
- aGAsyncResult
.- Returns:
- number of bytes read in, or -1 on error, or 0 on end of file.
- Throws:
GErrorException
- seeGError
-
setPending
Sets this InputStream to have actions pending. If the pending flag is already set or this InputStream is closed, it will returnfalse
and seterror
.- Returns:
true
if pending was previously unset and is now set.- Throws:
GErrorException
- seeGError
-
skip
Tries to skipcount
bytes from the stream. Will block during the operation.This is identical to g_input_stream_read(), from a behaviour standpoint, but the bytes that are skipped are not returned to the user. Some streams have an implementation that is more efficient than reading the data.
This function is optional for inherited classes, as the default implementation emulates it using read.
If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.- Parameters:
count
- the number of bytes that will be skipped from the streamcancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- Number of bytes skipped, or -1 on error
- Throws:
GErrorException
- seeGError
-
skipAsync
public void skipAsync(long count, int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Request an asynchronous skip ofcount
bytes from the stream. When the operation is finishedcallback
will be called. You can then call g_input_stream_skip_finish() to get the result of the operation.During an async request no other sync and async calls are allowed, and will result in
IOErrorEnum.PENDING
errors.A value of
count
larger thanG_MAXSSIZE
will cause aIOErrorEnum.INVALID_ARGUMENT
error.On success, the number of bytes skipped will be passed to the callback. It is not an error if this is not the same as the requested size, as it can happen e.g. near the end of a file, but generally we try to skip as many bytes as requested. Zero is returned on end of file (or if
count
is zero), but never otherwise.Any outstanding i/o request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is
G_PRIORITY_DEFAULT
.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:
count
- the number of bytes that will be skipped from the streamioPriority
- the I/O priority of the requestcancellable
- optionalGCancellable
object,null
to ignore.callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
skipFinish
Finishes a stream skip operation.- Parameters:
result
- aGAsyncResult
.- Returns:
- the size of the bytes skipped, or
-1
on error. - Throws:
GErrorException
- seeGError
-
closeFn
- Throws:
GErrorException
-
readFn
protected long readFn(@Nullable @Nullable MemorySegment buffer, long count, @Nullable @Nullable Cancellable cancellable) throws GErrorException - Throws:
GErrorException
-
builder
AInputStream.Builder
object constructs aInputStream
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withInputStream.Builder.build()
.
-