Class FileEnumerator
- All Implemented Interfaces:
Proxy
GFileEnumerator
allows you to operate on a set of File
objects,
returning a FileInfo
structure for each file enumerated (e.g.
File.enumerateChildren(java.lang.String, java.util.Set<org.gnome.gio.FileQueryInfoFlags>, org.gnome.gio.Cancellable)
will return a GFileEnumerator
for each
of the children within a directory).
To get the next file's information from a GFileEnumerator
, use
nextFile(org.gnome.gio.Cancellable)
or its asynchronous version,
nextFilesAsync(int, int, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback)
. Note that the asynchronous
version will return a list of FileInfo
objects, whereas the
synchronous will only return the next file in the enumerator.
The ordering of returned files is unspecified for non-Unix
platforms; for more information, see Dir.readName()
. On Unix,
when operating on local files, returned files will be sorted by
inode number. Effectively you can assume that the ordering of
returned files will be stable between successive calls (and
applications) assuming the directory is unchanged.
If your application needs a specific ordering, such as by name or modification time, you will have to implement that in your application code.
To close a GFileEnumerator
, use close(org.gnome.gio.Cancellable)
, or
its asynchronous version, closeAsync(int, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback)
. Once
a GFileEnumerator
is closed, no further actions may be performed
on it, and it should be freed with GObject.unref()
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
FileEnumerator.Builder<B extends FileEnumerator.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionFileEnumerator
(MemorySegment address) Create a FileEnumerator proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected FileEnumerator
asParent()
Returns this instance as if it were its parent type.static FileEnumerator.Builder
<? extends FileEnumerator.Builder> builder()
AFileEnumerator.Builder
object constructs aFileEnumerator
with the specified properties.boolean
close
(@Nullable Cancellable cancellable) Releases all resources used by this enumerator, making the enumerator returnIOErrorEnum.CLOSED
on all calls.void
closeAsync
(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously closes the file enumerator.boolean
closeFinish
(AsyncResult result) Finishes closing a file enumerator, started from g_file_enumerator_close_async().protected boolean
closeFn
(@Nullable Cancellable cancellable) Return a newGFile
which refers to the file named byinfo
in the source directory of this FileEnumerator.Get theGFile
container which is being enumerated.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the FileEnumerator classboolean
Checks if the file enumerator has pending operations.boolean
isClosed()
Checks if the file enumerator has been closed.boolean
iterate
(@Nullable Out<FileInfo> outInfo, @Nullable Out<File> outChild, @Nullable Cancellable cancellable) This is a version of g_file_enumerator_next_file() that's easier to use correctly from C programs.nextFile
(@Nullable Cancellable cancellable) Returns information for the next file in the enumerated object.void
nextFilesAsync
(int numFiles, int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Request information for a number of files from the enumerator asynchronously.nextFilesFinish
(AsyncResult result) Finishes the asynchronous operation started with g_file_enumerator_next_files_async().void
setPending
(boolean pending) Sets the file enumerator as having pending operations.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
-
Constructor Details
-
FileEnumerator
Create a FileEnumerator 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. -
close
Releases all resources used by this enumerator, making the enumerator returnIOErrorEnum.CLOSED
on all calls.This will be automatically called when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.
- Parameters:
cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
TRUE
on success orFALSE
on error.- Throws:
GErrorException
- seeGError
-
closeAsync
public void closeAsync(int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Asynchronously closes the file enumerator.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 in g_file_enumerator_close_finish().- Parameters:
ioPriority
- the I/O priority of the requestcancellable
- optionalGCancellable
object,null
to ignore.callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
closeFinish
Finishes closing a file enumerator, started from g_file_enumerator_close_async().If the file enumerator was already closed when g_file_enumerator_close_async() was called, then this function will report
IOErrorEnum.CLOSED
inerror
, and returnfalse
. If the file enumerator had pending operation when the close operation was started, then this function will reportIOErrorEnum.PENDING
, and returnfalse
. Ifcancellable
was notnull
, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be set, andfalse
will be returned.- Parameters:
result
- aGAsyncResult
.- Returns:
true
if the close operation has finished successfully.- Throws:
GErrorException
- seeGError
-
getChild
Return a newGFile
which refers to the file named byinfo
in the source directory of this FileEnumerator. This function is primarily intended to be used inside loops with g_file_enumerator_next_file().To use this,
G_FILE_ATTRIBUTE_STANDARD_NAME
must have been listed in the attributes list used when creating theGFileEnumerator
.This is a convenience method that's equivalent to:
gchar *name = g_file_info_get_name (info); GFile *child = g_file_get_child (g_file_enumerator_get_container (enumr), name);
- Parameters:
info
- aGFileInfo
gotten from g_file_enumerator_next_file() or the async equivalents.- Returns:
- a
GFile
for theGFileInfo
passed it.
-
getContainer
Get theGFile
container which is being enumerated.- Returns:
- the
GFile
which is being enumerated.
-
hasPending
public boolean hasPending()Checks if the file enumerator has pending operations.- Returns:
true
if the this FileEnumerator has pending operations.
-
isClosed
public boolean isClosed()Checks if the file enumerator has been closed.- Returns:
true
if the this FileEnumerator is closed.
-
iterate
public boolean iterate(@Nullable @Nullable Out<FileInfo> outInfo, @Nullable @Nullable Out<File> outChild, @Nullable @Nullable Cancellable cancellable) throws GErrorException This is a version of g_file_enumerator_next_file() that's easier to use correctly from C programs. With g_file_enumerator_next_file(), the gboolean return value signifies "end of iteration or error", which requires allocation of a temporaryGError
.In contrast, with this function, a
false
return from g_file_enumerator_iterate() always means "error". End of iteration is signaled byoutInfo
oroutChild
beingnull
.Another crucial difference is that the references for
outInfo
andoutChild
are owned by this FileEnumerator (they are cached as hidden properties). You must not unref them in your own code. This makes memory management significantly easier for C code in combination with loops.Finally, this function optionally allows retrieving a
GFile
as well.You must specify at least one of
outInfo
oroutChild
.The code pattern for correctly using g_file_enumerator_iterate() from C is:
direnum = g_file_enumerate_children (file, ...); while (TRUE) { GFileInfo *info; if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error)) goto out; if (!info) break; ... do stuff with "info"; do not unref it! ... } out: g_object_unref (direnum); // Note: frees the last @info
- Parameters:
outInfo
- Output location for the nextGFileInfo
, ornull
outChild
- Output location for the nextGFile
, ornull
cancellable
- aGCancellable
- Throws:
GErrorException
- seeGError
-
nextFile
Returns information for the next file in the enumerated object. Will block until the information is available. TheGFileInfo
returned from this function will contain attributes that match the attribute string that was passed when theGFileEnumerator
was created.See the documentation of
GFileEnumerator
for information about the order of returned files.On error, returns
null
and setserror
to the error. If the enumerator is at the end,null
will be returned anderror
will be unset.- Parameters:
cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- A
GFileInfo
ornull
on error or end of enumerator. Free the returned object with g_object_unref() when no longer needed. - Throws:
GErrorException
- seeGError
-
nextFilesAsync
public void nextFilesAsync(int numFiles, int ioPriority, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Request information for a number of files from the enumerator asynchronously. When all I/O for the operation is finished thecallback
will be called with the requested information.See the documentation of
GFileEnumerator
for information about the order of returned files.Once the end of the enumerator is reached, or if an error occurs, the
callback
will be called with an empty list. In this case, the previous call to g_file_enumerator_next_files_async() will typically have returned fewer thannumFiles
items.If a request is cancelled the callback will be called with
IOErrorEnum.CANCELLED
.This leads to the following pseudo-code usage:
g_autoptr(GFile) dir = get_directory (); g_autoptr(GFileEnumerator) enumerator = NULL; g_autolist(GFileInfo) files = NULL; g_autoptr(GError) local_error = NULL; enumerator = yield g_file_enumerate_children_async (dir, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, cancellable, …, &local_error); if (enumerator == NULL) g_error ("Error enumerating: %s", local_error->message); // Loop until no files are returned, either because the end of the enumerator // has been reached, or an error was returned. do { files = yield g_file_enumerator_next_files_async (enumerator, 5, // number of files to request G_PRIORITY_DEFAULT, cancellable, …, &local_error); // Process the returned files, but don’t assume that exactly 5 were returned. for (GList *l = files; l != NULL; l = l->next) { GFileInfo *info = l->data; handle_file_info (info); } } while (files != NULL); if (local_error != NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_error ("Error while enumerating: %s", local_error->message);
During an async request no other sync and async calls are allowed, and will result in
IOErrorEnum.PENDING
errors.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:
numFiles
- the number of file info objects to requestioPriority
- the I/O priority of the requestcancellable
- optionalGCancellable
object,null
to ignore.callback
- aGAsyncReadyCallback
to call when the request is satisfied
-
nextFilesFinish
Finishes the asynchronous operation started with g_file_enumerator_next_files_async().- Parameters:
result
- aGAsyncResult
.- Returns:
- a
GList
ofGFileInfos
. You must free the list with g_list_free() and unref the infos with g_object_unref() when you're done with them. - Throws:
GErrorException
- seeGError
-
setPending
public void setPending(boolean pending) Sets the file enumerator as having pending operations.- Parameters:
pending
- a boolean value.
-
closeFn
- Throws:
GErrorException
-
builder
AFileEnumerator.Builder
object constructs aFileEnumerator
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withFileEnumerator.Builder.build()
.
-