Class MappedFile

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class MappedFile extends ProxyInstance
The GMappedFile represents a file mapping created with g_mapped_file_new(). It has only private members and should not be accessed directly.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a MappedFile proxy instance for the provided memory address.
    MappedFile(String filename, boolean writable)
    Maps a file into memory.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Deprecated.
    Use g_mapped_file_unref() instead.
    static MappedFile
    fromFd(int fd, boolean writable)
    Maps a file into memory.
    byte[]
    Creates a new GBytes which references the data mapped from file. The mapped contents of the file must not be modified after creating this bytes object, because a GBytes should be immutable.
    @Nullable String
    Returns the contents of a GMappedFile.
    long
    Returns the length of the contents of a GMappedFile.
    The memory layout of the native struct.
    static @Nullable Type
    Get the GType of the MappedFile class.
    ref()
    Increments the reference count of this MappedFile by one.
    void
    Decrements the reference count of this MappedFile by one.

    Methods inherited from class ProxyInstance

    equals, handle, hashCode

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MappedFile

      public MappedFile(MemorySegment address)
      Create a MappedFile proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • MappedFile

      public MappedFile(String filename, boolean writable) throws GErrorException

      Maps a file into memory. On UNIX, this is using the mmap() function.

      If writable is true, the mapped buffer may be modified, otherwise it is an error to modify the mapped buffer. Modifications to the buffer are not visible to other processes mapping the same file, and are not written back to the file.

      Note that modifications of the underlying file might affect the contents of the GMappedFile. Therefore, mapping should only be used if the file will not be modified, or if all modifications of the file are done atomically (e.g. using g_file_set_contents()).

      If filename is the name of an empty, regular file, the function will successfully return an empty GMappedFile. In other cases of size 0 (e.g. device files such as /dev/null), error will be set to the GFileError value FileError.INVAL.

      Parameters:
      filename - The path of the file to load, in the GLib filename encoding
      writable - whether the mapping should be writable
      Throws:
      GErrorException - see GError
      Since:
      2.8
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the MappedFile class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • fromFd

      public static MappedFile fromFd(int fd, boolean writable) throws GErrorException

      Maps a file into memory. On UNIX, this is using the mmap() function.

      If writable is true, the mapped buffer may be modified, otherwise it is an error to modify the mapped buffer. Modifications to the buffer are not visible to other processes mapping the same file, and are not written back to the file.

      Note that modifications of the underlying file might affect the contents of the GMappedFile. Therefore, mapping should only be used if the file will not be modified, or if all modifications of the file are done atomically (e.g. using g_file_set_contents()).

      Parameters:
      fd - The file descriptor of the file to load
      writable - whether the mapping should be writable
      Returns:
      a newly allocated GMappedFile which must be unref'd with g_mapped_file_unref(), or null if the mapping failed.
      Throws:
      GErrorException - see GError
      Since:
      2.32
    • free

      @Deprecated public void free()
      Deprecated.
      Use g_mapped_file_unref() instead.
      This call existed before GMappedFile had refcounting and is currently exactly the same as g_mapped_file_unref().
      Since:
      2.8
    • getBytes

      public byte[] getBytes()
      Creates a new GBytes which references the data mapped from file. The mapped contents of the file must not be modified after creating this bytes object, because a GBytes should be immutable.
      Returns:
      A newly allocated GBytes referencing data from this MappedFile
      Since:
      2.34
    • getContents

      public @Nullable String getContents()

      Returns the contents of a GMappedFile.

      Note that the contents may not be zero-terminated, even if the GMappedFile is backed by a text file.

      If the file is empty then null is returned.

      Returns:
      the contents of file, or null.
      Since:
      2.8
    • getLength

      public long getLength()
      Returns the length of the contents of a GMappedFile.
      Returns:
      the length of the contents of file.
      Since:
      2.8
    • ref

      public MappedFile ref()
      Increments the reference count of this MappedFile by one. It is safe to call this function from any thread.
      Returns:
      the passed in GMappedFile.
      Since:
      2.22
    • unref

      public void unref()

      Decrements the reference count of this MappedFile by one. If the reference count drops to 0, unmaps the buffer of this MappedFile and frees it.

      It is safe to call this function from any thread.

      Since 2.22