Class InputStream
- All Implemented Interfaces:
AutoCloseable,Proxy,AutoCloseable
- Direct Known Subclasses:
FileInputStream,FilterInputStream,InputStream.InputStream$Impl,MemoryInputStream,PollableInputStream.PollableInputStream$Impl
GInputStream is a base class for implementing streaming input.
It has functions to read from a stream (read(org.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
Nested ClassesModifier and TypeClassDescriptionstatic classInputStream.Builder<B extends InputStream.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classThe InputStream$Impl type represents a native instance of the abstract InputStream class.static classNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new InputStream.InputStream(MemorySegment address) Create a InputStream proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected InputStreamasParent()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 closes of the stream, releasing resources related to it.booleancloseFinish(AsyncResult result) Finishes closing a stream asynchronously, started from g_input_stream_close_async().protected booleancloseFn(@Nullable Cancellable cancellable) static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the InputStream classbooleanChecks if an input stream has pending actions.booleanisClosed()Checks if an input stream is closed.longread(@Nullable Out<byte[]> buffer, @Nullable Cancellable cancellable) Tries to readcountbytes from the stream into the buffer starting atbuffer.Will block during this read.booleanreadAll(@Nullable Out<byte[]> buffer, Out<Long> bytesRead, @Nullable Cancellable cancellable) Tries to readcountbytes from the stream into the buffer starting atbuffer.Will block during this read.voidreadAllAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into the buffer starting atbuffer.booleanreadAllFinish(AsyncResult result, Out<Long> bytesRead) Finishes an asynchronous stream read operation started withreadAllAsync(org.javagi.base.Out<byte[]>, int, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).voidreadAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into the buffer starting atbuffer.When the operation is finishedcallbackwill be called.byte[]readBytes(long count, @Nullable Cancellable cancellable) Like g_input_stream_read(), this tries to readcountbytes from the stream in a blocking fashion.voidreadBytesAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into a newGBytes.byte[]readBytesFinish(AsyncResult result) Finishes an asynchronous stream read-into-GBytesoperation.longreadFinish(AsyncResult result) Finishes an asynchronous stream read operation.protected longreadFn(@Nullable MemorySegment buffer, long count, @Nullable Cancellable cancellable) booleanSets this InputStream to have actions pending.longskip(long count, @Nullable Cancellable cancellable) Tries to skipcountbytes from the stream.voidskipAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous skip ofcountbytes from the stream.longskipFinish(AsyncResult result) Finishes a stream skip 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
-
InputStream
Create a InputStream proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
InputStream
public InputStream()Creates a new InputStream.
-
-
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. -
clearPending
public void clearPending()Clears the pending flag onstream. -
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.CLOSEDfor all operations. Still, it is important to check and report the error to the user.If
cancellableis notnull, 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.- Specified by:
closein interfaceAutoCloseable- Parameters:
cancellable- optionalGCancellableobject,nullto ignore.- Returns:
trueon success,falseon failure- Throws:
GErrorException- seeGError
-
closeAsync
public void closeAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Requests an asynchronous closes of the stream, releasing resources related to it. When the operation is finishedcallbackwill 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- aGAsyncReadyCallbackto call when the request is satisfied
-
closeFinish
Finishes closing a stream asynchronously, started from g_input_stream_close_async().- Parameters:
result- aGAsyncResult.- Returns:
trueif the stream was closed successfully.- Throws:
GErrorException- seeGError
-
hasPending
public boolean hasPending()Checks if an input stream has pending actions.- Returns:
trueif this InputStream has pending actions.
-
isClosed
public boolean isClosed()Checks if an input stream is closed.- Returns:
trueif the stream is closed.
-
read
public long read(@Nullable Out<byte[]> buffer, @Nullable Cancellable cancellable) throws GErrorException Tries to readcountbytes 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
countlarger thanG_MAXSSIZEwill cause aIOErrorEnum.INVALID_ARGUMENTerror.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
countis zero), but never otherwise.The returned
bufferis not a nul-terminated string, it can contain nul bytes at any position, and this function doesn't nul-terminate thebuffer.If
cancellableis notnull, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill 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
erroris set accordingly.- Parameters:
buffer- a buffer to read data into (which should be at least count bytes long).cancellable- optionalGCancellableobject,nullto ignore.- Returns:
- Number of bytes read, or -1 on error, or 0 on end of file.
- Throws:
GErrorException- seeGError
-
readAll
public boolean readAll(@Nullable Out<byte[]> buffer, Out<Long> bytesRead, @Nullable Cancellable cancellable) throws GErrorException Tries to readcountbytes 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
countbytes, or if we reached the end of the stream,trueis returned, andbytesReadis set to the number of bytes read intobuffer.If there is an error during the operation
falseis returned anderroris 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)thenbytesReadwill 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- optionalGCancellableobject,nullto ignore.- Returns:
trueon success,falseif there was an error- Throws:
GErrorException- seeGError
-
readAllAsync
public void readAllAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into the buffer starting atbuffer.This is the asynchronous equivalent of
readAll(org.javagi.base.Out<byte[]>, org.javagi.base.Out<java.lang.Long>, org.gnome.gio.Cancellable).Call
readAllFinish(org.gnome.gio.AsyncResult, org.javagi.base.Out<java.lang.Long>)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- optionalGCancellableobject,nullto ignorecallback- aGAsyncReadyCallbackto call when the request is satisfied- Since:
- 2.44
-
readAllFinish
Finishes an asynchronous stream read operation started withreadAllAsync(org.javagi.base.Out<byte[]>, int, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback).As a special exception to the normal conventions for functions that use
GError, if this function returnsfalse(and setserror)thenbytesReadwill 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- aGAsyncResultbytesRead- location to store the number of bytes that was read from the stream- Returns:
trueon success,falseif there was an error- Throws:
GErrorException- seeGError- Since:
- 2.44
-
readAsync
public void readAsync(@Nullable Out<byte[]> buffer, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into the buffer starting atbuffer.When the operation is finishedcallbackwill 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
stream,and will result inIOErrorEnum.PENDINGerrors.A value of
countlarger thanG_MAXSSIZEwill cause aIOErrorEnum.INVALID_ARGUMENTerror.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
countis 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- optionalGCancellableobject,nullto ignore.callback- aGAsyncReadyCallbackto call when the request is satisfied
-
readBytes
Like g_input_stream_read(), this tries to readcountbytes from the stream in a blocking fashion. However, rather than reading into a user-supplied buffer, this will create a newGBytescontaining the data that was read. This may be easier to use from language bindings.If count is zero, returns a zero-length
GBytesand does nothing. A value ofcountlarger thanG_MAXSSIZEwill cause aIOErrorEnum.INVALID_ARGUMENTerror.On success, a new
GBytesis 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-lengthGBytesis returned on end of file (or ifcountis zero), but never otherwise.If
cancellableis notnull, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.On error
nullis returned anderroris set accordingly.- Parameters:
count- maximum number of bytes that will be read from the stream. Common values include 4096 and 8192.cancellable- optionalGCancellableobject,nullto ignore.- Returns:
- a new
GBytes, ornullon error - Throws:
GErrorException- seeGError- Since:
- 2.34
-
readBytesAsync
public void readBytesAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous read ofcountbytes from the stream into a newGBytes. When the operation is finishedcallbackwill 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
stream,and will result inIOErrorEnum.PENDINGerrors.A value of
countlarger thanG_MAXSSIZEwill cause aIOErrorEnum.INVALID_ARGUMENTerror.On success, the new
GByteswill 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 ifcountis 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- optionalGCancellableobject,nullto ignore.callback- aGAsyncReadyCallbackto call when the request is satisfied- Since:
- 2.34
-
readBytesFinish
Finishes an asynchronous stream read-into-GBytesoperation.- Parameters:
result- aGAsyncResult.- Returns:
- the newly-allocated
GBytes, ornullon error - Throws:
GErrorException- seeGError- Since:
- 2.34
-
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 returnfalseand seterror.- Returns:
trueif pending was previously unset and is now set.- Throws:
GErrorException- seeGError
-
skip
Tries to skipcountbytes 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
cancellableis notnull, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill 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- optionalGCancellableobject,nullto ignore.- Returns:
- Number of bytes skipped, or -1 on error
- Throws:
GErrorException- seeGError
-
skipAsync
public void skipAsync(long count, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request an asynchronous skip ofcountbytes from the stream. When the operation is finishedcallbackwill 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.PENDINGerrors.A value of
countlarger thanG_MAXSSIZEwill cause aIOErrorEnum.INVALID_ARGUMENTerror.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
countis 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- optionalGCancellableobject,nullto ignore.callback- aGAsyncReadyCallbackto call when the request is satisfied
-
skipFinish
Finishes a stream skip operation.- Parameters:
result- aGAsyncResult.- Returns:
- the size of the bytes skipped, or
-1on error. - Throws:
GErrorException- seeGError
-
closeFn
- Throws:
GErrorException
-
readFn
protected long readFn(@Nullable MemorySegment buffer, long count, @Nullable Cancellable cancellable) throws GErrorException - Throws:
GErrorException
-