public class OpenMode extends Object
Modifier and Type | Field and Description |
---|---|
boolean |
appending
Whether this file is opened in appending mode ('a')
|
boolean |
binary
Whether this file is opened in binary mode ('b')
|
boolean |
invalid
Set true when any invalid symbol or combination is discovered
|
String |
message
Error message describing the way in which the mode is invalid, or null if no problem has been
found.
|
String |
originalModeString
Original string supplied as the mode
|
boolean |
other
Whether the mode contained some other symbol from the allowed ones
|
boolean |
reading
Whether this file is opened for reading ('r')
|
boolean |
text
Whether this file is opened in text mode ('t')
|
boolean |
universal
Whether this file is opened in universal newlines mode ('U')
|
boolean |
updating
Whether this file is opened for updating ('+')
|
boolean |
writing
Whether this file is opened for writing ('w')
|
Constructor and Description |
---|
OpenMode(String mode)
Decode the given string to an OpenMode object, checking for duplicate or unrecognised mode
letters.
|
Modifier and Type | Method and Description |
---|---|
void |
checkValid()
Call
validate() and raise an exception if the mode string is not valid, as signalled
by either invalid or other being true after that call. |
String |
forFileIO()
The mode string we need when constructing a
FileIO initialised with the present
mode. |
String |
text()
The mode string that a text file should claim to have, when initialised with the present
mode.
|
String |
toString() |
void |
validate()
Adjust and validate the flags decoded from the mode string.
|
void |
validate(String encoding,
String errors,
String newline)
Perform additional validation of the flags relevant to text files.
|
public final String originalModeString
public boolean reading
public boolean writing
public boolean appending
public boolean updating
public boolean binary
public boolean text
public boolean universal
public boolean other
public boolean invalid
public String message
validate()
method, or by client code.public OpenMode(String mode)
message
. After
construction, a client should always call validate()
to complete validity checks.mode
- public void validate()
invalid
flag is not
already true
, it checks the validity of the flags against combinations allowed
by the Python io.open()
function. In the case of a violation, it sets the
invalid
flag, and sets message
to a descriptive message. The point of
the qualification "if the invalid
flag is not already true
" is that
the message should always describe the first problem discovered. If left blank, as in fact
the constructor does, it will be filled by the generic message when checkValid()
is
finally called. Clients may override this method (by sub-classing) to express the validation
correct in their context.
The invalid combinations enforced here are those for the "raw" (ie non-text) file types:
validate(String, String, String)
for additional checks relevant to text
files.public void validate(String encoding, String errors, String newline)
invalid
is not
already true
, and the mode includes binary
, then all the arguments to
this call must be null
. If the criterion is not met, then on return from the
method, invalid==true
and message
is set to a standard error message.
This is the standard additional validation applicable to text files. (By "standard" we mean
the test and messages that CPython io.open
uses.)encoding
- argument to open()
errors
- argument to open()
newline
- argument to open()
public void checkValid() throws PyException
validate()
and raise an exception if the mode string is not valid, as signalled
by either invalid
or other
being true
after that call. If no
more specific message has been assigned in message
, report the original mode string.PyException
- (ValueError) if the mode string was invalid.public String forFileIO()
FileIO
initialised with the present
mode. Note that this is not the same as the full open mode because it omits the text-based
attributes.public String text()