MuntashirAkon/AppManager · error · IllegalArgumentException

Bad mode: + mode

Error message

Bad mode: + mode

What it means

VirtualFileSystem.openChannel(path, mode) throws IllegalArgumentException("Bad mode: " + mode) when the mode bitmask contains none of MODE_READ_WRITE, MODE_READ_ONLY, or MODE_WRITE_ONLY. The library only accepts these documented open-mode flags.

Source

Thrown at app/src/main/java/io/github/muntashirakon/io/fs/VirtualFileSystem.java:1003

        if (!targetNode.isFile()) {
            throw new IOException(path + " is not a file.");
        }
        return new FileOutputStream(getCachedFile(targetNode, true), append);
    }

    @NonNull
    public FileChannel openChannel(String path, int mode) throws IOException {
        boolean read = false;
        boolean write = false;
        if ((mode & MODE_READ_WRITE) != 0) {
            read = true;
            write = true;
        } else if ((mode & MODE_READ_ONLY) != 0) {
            read = true;
        } else if ((mode & MODE_WRITE_ONLY) != 0) {
            write = true;
        } else {
            throw new IllegalArgumentException("Bad mode: " + mode);
        }

        if (read && write && !checkAccess(path, OsConstants.R_OK | OsConstants.W_OK)) {
            throw new IOException(path + " cannot be opened for both reading and writing.");
        } else if (read && !checkAccess(path, OsConstants.R_OK)) {
            throw new IOException(path + " cannot be opened for both reading.");
        } else if (write && !checkAccess(path, OsConstants.W_OK)) {
            throw new IOException(path + " cannot be opened for both writing.");
        }
        Node<?> targetNode = getNode(path);
        if (targetNode == null) {
            throw new FileNotFoundException(path + " does not exist.");
        }
        if (!targetNode.isFile()) {
            throw new IOException(path + " is not a file.");
        }
        return FileSystemManager.getLocal().openChannel(getCachedFile(targetNode, write), mode);
    }

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Pass one of the class's MODE_READ_WRITE / MODE_READ_ONLY / MODE_WRITE_ONLY constants
  2. If translating from StandardOpenOption, map READ+WRITE->MODE_READ_WRITE, READ->MODE_READ_ONLY, WRITE/APPEND->MODE_WRITE_ONLY
  3. Log/validate the mode value at the call site to catch computed masks that end up 0

Example fix

// before
FileChannel ch = vfs.openChannel(path, 0);
// after
FileChannel ch = vfs.openChannel(path, VirtualFileSystem.MODE_READ_WRITE);
Defensive patterns

Strategy: validation

Validate before calling

int mode = VirtualFileSystem.MODE_READ_ONLY; // one of MODE_READ_WRITE|MODE_READ_ONLY|MODE_WRITE_ONLY
if (mode != VirtualFileSystem.MODE_READ_WRITE && mode != VirtualFileSystem.MODE_READ_ONLY && mode != VirtualFileSystem.MODE_WRITE_ONLY)
    throw new IllegalArgumentException("Unsupported mode: " + mode);

Type guard

boolean isValidMode(int mode) {
    return mode == VirtualFileSystem.MODE_READ_WRITE
        || mode == VirtualFileSystem.MODE_READ_ONLY
        || mode == VirtualFileSystem.MODE_WRITE_ONLY;
}

Try / catch

try {
    FileChannel ch = vfs.openChannel(path, mode);
} catch (IllegalArgumentException e) {
    // fix mode constant at call site; log the bad value
}

Prevention

When it happens

Trigger: Passing 0 as mode; passing an undefined/custom bit (e.g. java.nio FileChannel mode constants like READ/WRITE instead of the VFS's MODE_* constants); accidentally clearing all bits with a bad mask computation.

Common situations: Porting code from java.nio.channels.FileChannel (which uses StandardOpenOption) and passing wrong constants; typo in flag composition; mode built from a config value of 0 or a string parsed incorrectly.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/7f8d61dd549f9109. Report an issue: GitHub.