Class ModeFlags

java.lang.Object
org.jruby.util.io.ModeFlags
All Implemented Interfaces:
Cloneable

public class ModeFlags extends Object implements Cloneable
This file represents the POSIX-like mode flags an open channel (as in a ChannelDescriptor) can have. It provides the basic flags for read/write as well as flags for create, truncate, and others. In addition, it provides methods for querying specific flag settings and converting to two other formats: a Java mode string and an OpenFile mode int. Note: In MRI these are called oflags.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    accmode flag, used to mask the read/write mode
    static final int
    append flag, to seek to the end of the file
    static final int
    binary flag, to ensure no encoding changes are made while writing (Windows only)
    static final int
    create flag, to specify non-existing file should be created
    static final int
    exclusive access flag, to require locking the target file
    static final int
    nonblock flag, to perform all operations non-blocking.
    static final OpenOption
    try to access special mode flag, failover to default SHARE_DELETE otherwise
    static final int
    read-only flag (default value if no other flags set)
    static final int
    read/write flag
    static final int
    delete shared file flag for windows, otherwise zero
    static final int
    textmode flag, MRI has no equivalent but we use ModeFlags currently to also capture what are oflags.
    static final int
    tmpfile flag (Linux only)
    static final int
    truncate flag, to truncate the target file to zero length
    static final int
    write-only flag
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new ModeFlags object with the default read-only flag.
    ModeFlags(long flags)
    Construct a new ModeFlags object with the specified flags
    ModeFlags(String flagString)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static ModeFlags
    createModeFlags(int oflags)
     
    int
    Convert the flags in this object to a set of flags appropriate for the fcntl.
    int
    Return a value that, when passed to our constructor, would create a copy of this instance.
    static ModeFlags
    Deprecated.
    static int
     
    static int
    getOFlagsFromString(Ruby runtime, String modesString)
     
    int
    Convert the flags in this object to a set of flags appropriate for the OpenFile structure and logic therein.
    static int
    With the provided open flags parameter what fmode values should be set (fmode for us is represented by OpenFile).
    boolean
    Whether the flags specify to append to existing files.
    boolean
    Whether the flags specify "binary" mode for reads and writes.
    boolean
    Whether the flags specify to create nonexisting files.
    boolean
    Whether the flags specify exclusive access.
    boolean
    Whether the flags specify "readable", either read/write or read-only.
    final boolean
    Whether the flags specify"read only".
    boolean
    Whether the flags specify "read/write".
    boolean
    Whether the flags specify to allow deleting shared files (Windows FILE_SHARE_DELETE).
    boolean
    Check whether the target set of flags is a superset of this one; used to ensure that a file is not re-opened with more privileges than it already had.
    boolean
    Whether the flags specify "unnamed temporary".
    boolean
    Whether the flags specify "text" mode for reads and writes.
    boolean
    Whether the flags specify to truncate the target file.
    boolean
    Whether the flags specify "writable", either read/write or write-only
    boolean
    Whether the flags specify "write only".
    static int
    oflagsFrom(jnr.posix.POSIX posix, Channel channel)
    Build a set of mode flags using the specified channel's actual capabilities.
    Produce a Java IO mode string from the flags in this object.
    toOpenOptions(boolean includeAppend, boolean includeTruncate)
    Produce an array of OpenOption equivalent to these mode flags, including TRUNCATE_EXISTING only if includeTruncate is set to true.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • RDONLY

      public static final int RDONLY
      read-only flag (default value if no other flags set)
    • WRONLY

      public static final int WRONLY
      write-only flag
    • RDWR

      public static final int RDWR
      read/write flag
    • CREAT

      public static final int CREAT
      create flag, to specify non-existing file should be created
    • EXCL

      public static final int EXCL
      exclusive access flag, to require locking the target file
    • TRUNC

      public static final int TRUNC
      truncate flag, to truncate the target file to zero length
    • APPEND

      public static final int APPEND
      append flag, to seek to the end of the file
    • NONBLOCK

      public static final int NONBLOCK
      nonblock flag, to perform all operations non-blocking. Unused currently
    • BINARY

      public static final int BINARY
      binary flag, to ensure no encoding changes are made while writing (Windows only)
    • TMPFILE

      public static final int TMPFILE
      tmpfile flag (Linux only)
    • TEXT

      public static final int TEXT
      textmode flag, MRI has no equivalent but we use ModeFlags currently to also capture what are oflags.
      See Also:
    • SHARE_DELETE

      public static final int SHARE_DELETE
      delete shared file flag for windows, otherwise zero
    • NOSHARE_DELETE

      public static final OpenOption NOSHARE_DELETE
      try to access special mode flag, failover to default SHARE_DELETE otherwise
    • ACCMODE

      public static final int ACCMODE
      accmode flag, used to mask the read/write mode
  • Constructor Details

  • Method Details

    • getOFlagsFromString

      public static int getOFlagsFromString(String modesString) throws InvalidValueException
      Throws:
      InvalidValueException
    • getOFlagsFromString

      public static int getOFlagsFromString(Ruby runtime, String modesString)
    • oflagsFrom

      public static int oflagsFrom(jnr.posix.POSIX posix, Channel channel)
      Build a set of mode flags using the specified channel's actual capabilities.
      Parameters:
      channel - the channel to examine for capabilities
      Returns:
      the mode flags
    • getModesFromChannel

      @Deprecated public static ModeFlags getModesFromChannel(Channel channel)
      Deprecated.
      Build a set of mode flags using the specified channel's actual capabilities.
      Parameters:
      channel - the channel to examine for capabilities
      Returns:
      the mode flags
    • toJavaModeString

      public String toJavaModeString()
      Produce a Java IO mode string from the flags in this object.
      Returns:
      A Java string suitable for opening files with RandomAccessFile
    • toOpenOptions

      public OpenOption[] toOpenOptions(boolean includeAppend, boolean includeTruncate)
      Produce an array of OpenOption equivalent to these mode flags, including TRUNCATE_EXISTING only if includeTruncate is set to true. Set includeTruncate to false if you intend to truncate the file separately. The resulting array may be shared and should not be modified.
      Parameters:
      includeTruncate - whether to include truncation in the options
      Returns:
      an array of OpenOption that matches these mode flags.
    • isReadOnly

      public final boolean isReadOnly()
      Whether the flags specify"read only".
      Returns:
      true if read-only, false otherwise
    • isReadable

      public boolean isReadable()
      Whether the flags specify "readable", either read/write or read-only.
      Returns:
      true if readable, false otherwise
    • isWriteOnly

      public boolean isWriteOnly()
      Whether the flags specify "write only".
      Returns:
      true if the write only flag is set, false otherwise
    • isReadWrite

      public boolean isReadWrite()
      Whether the flags specify "read/write".
      Returns:
      true if read/write flag is set, false otherwise
    • isBinary

      public boolean isBinary()
      Whether the flags specify "binary" mode for reads and writes.
      Returns:
      true if binary mode, false otherwise
    • isText

      public boolean isText()
      Whether the flags specify "text" mode for reads and writes.
      Returns:
      true if text mode, false otherwise
    • isTemporary

      public boolean isTemporary()
      Whether the flags specify "unnamed temporary".
      Returns:
      true if unnamed temporary mode, false otherwise
    • isCreate

      public boolean isCreate()
      Whether the flags specify to create nonexisting files.
      Returns:
      true if nonexisting files should be created, false otherwise
    • isWritable

      public boolean isWritable()
      Whether the flags specify "writable", either read/write or write-only
      Returns:
      true if writable, false otherwise
    • isExclusive

      public boolean isExclusive()
      Whether the flags specify exclusive access.
      Returns:
      true if exclusive, false otherwise
    • isAppendable

      public boolean isAppendable()
      Whether the flags specify to append to existing files.
      Returns:
      true if append, false otherwise
    • isTruncate

      public boolean isTruncate()
      Whether the flags specify to truncate the target file.
      Returns:
      true if truncate, false otherwise
    • isShareDelete

      public boolean isShareDelete()
      Whether the flags specify to allow deleting shared files (Windows FILE_SHARE_DELETE).
      Returns:
      true if shared delete is allowed, false otherwise
    • isSubsetOf

      public boolean isSubsetOf(ModeFlags superset)
      Check whether the target set of flags is a superset of this one; used to ensure that a file is not re-opened with more privileges than it already had.
      Parameters:
      superset - The ModeFlags object which should be a superset of this one
      Returns:
      true if the object is a superset, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getFlags

      public int getFlags()
      Return a value that, when passed to our constructor, would create a copy of this instance.
      Returns:
      an int of the private flags variable.
    • getOpenFileFlagsFor

      public static int getOpenFileFlagsFor(int flags)
      With the provided open flags parameter what fmode values should be set (fmode for us is represented by OpenFile).
    • getOpenFileFlags

      public int getOpenFileFlags()
      Convert the flags in this object to a set of flags appropriate for the OpenFile structure and logic therein.
      Returns:
      an int of flags appropriate for OpenFile
    • getFcntlFileFlags

      public int getFcntlFileFlags()
      Convert the flags in this object to a set of flags appropriate for the fcntl.
      Returns:
      an int of flags appropriate for fcntl
    • createModeFlags

      public static ModeFlags createModeFlags(int oflags)