Package org.jruby.util.io
Class ModeFlags
java.lang.Object
org.jruby.util.io.ModeFlags
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionstatic final int
accmode flag, used to mask the read/write modestatic final int
append flag, to seek to the end of the filestatic 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 createdstatic final int
exclusive access flag, to require locking the target filestatic final int
nonblock flag, to perform all operations non-blocking.static final OpenOption
try to access special mode flag, failover to default SHARE_DELETE otherwisestatic final int
read-only flag (default value if no other flags set)static final int
read/write flagstatic final int
delete shared file flag for windows, otherwise zerostatic 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 lengthstatic final int
write-only flag -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ModeFlags
createModeFlags
(int oflags) int
Convert the flags in this object to a set of flags appropriate for the fcntl.int
getFlags()
Return a value that, when passed to our constructor, would create a copy of this instance.static ModeFlags
getModesFromChannel
(Channel channel) Deprecated.static int
getOFlagsFromString
(String modesString) 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
getOpenFileFlagsFor
(int flags) 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
isBinary()
Whether the flags specify "binary" mode for reads and writes.boolean
isCreate()
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
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.boolean
Whether the flags specify "unnamed temporary".boolean
isText()
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-onlyboolean
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 ofOpenOption
equivalent to these mode flags, including TRUNCATE_EXISTING only if includeTruncate is set to true.toString()
-
Field Details
-
RDONLY
public static final int RDONLYread-only flag (default value if no other flags set) -
WRONLY
public static final int WRONLYwrite-only flag -
RDWR
public static final int RDWRread/write flag -
CREAT
public static final int CREATcreate flag, to specify non-existing file should be created -
EXCL
public static final int EXCLexclusive access flag, to require locking the target file -
TRUNC
public static final int TRUNCtruncate flag, to truncate the target file to zero length -
APPEND
public static final int APPENDappend flag, to seek to the end of the file -
NONBLOCK
public static final int NONBLOCKnonblock flag, to perform all operations non-blocking. Unused currently -
BINARY
public static final int BINARYbinary flag, to ensure no encoding changes are made while writing (Windows only) -
TMPFILE
public static final int TMPFILEtmpfile flag (Linux only) -
TEXT
public static final int TEXTtextmode 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_DELETEdelete shared file flag for windows, otherwise zero -
NOSHARE_DELETE
try to access special mode flag, failover to default SHARE_DELETE otherwise -
ACCMODE
public static final int ACCMODEaccmode flag, used to mask the read/write mode
-
-
Constructor Details
-
ModeFlags
public ModeFlags()Construct a new ModeFlags object with the default read-only flag. -
ModeFlags
Construct a new ModeFlags object with the specified flags- Parameters:
flags
- The flags to use for this object- Throws:
InvalidValueException
- If the modes are invalid
-
ModeFlags
- Throws:
InvalidValueException
-
-
Method Details
-
getOFlagsFromString
- Throws:
InvalidValueException
-
getOFlagsFromString
-
oflagsFrom
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.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
Produce a Java IO mode string from the flags in this object.- Returns:
- A Java string suitable for opening files with RandomAccessFile
-
toOpenOptions
Produce an array ofOpenOption
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
-
isSubsetOf
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
-
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
-