java-native-access/jna · error · IllegalArgumentException

Size must greater than {size()}, requested {size}

Error message

Size must greater than {size()}, requested {size}

What it means

FILE_NOTIFY_INFORMATION(int size) allocates native memory for a ReadDirectoryChangesW buffer and validates that the requested byte size is at least the minimum structure size(). Requesting anything smaller cannot hold even one notification record, so the constructor throws this IllegalArgumentException before allocating.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/WinNT.java:906

     * This structure is non-trivial since it is a pattern stamped into a large
     * block of result memory rather than something that stands alone or is used
     * for input.
     */
    @FieldOrder({"NextEntryOffset", "Action", "FileNameLength", "FileName"})
    public static class FILE_NOTIFY_INFORMATION extends Structure {
        public int NextEntryOffset;
        public int Action;
        public int FileNameLength;
        // filename is not nul-terminated, so we can't use a String/WString
        public char[] FileName = new char[1];

        private FILE_NOTIFY_INFORMATION() {
            super();
        }

        public FILE_NOTIFY_INFORMATION(int size) {
            if (size < size()) {
                throw new IllegalArgumentException("Size must greater than "
                        + size() + ", requested " + size);
            }
            allocateMemory(size);
        }

        /**
         * WARNING: this filename may be either the short or long form of the
         * filename.
         * @return filename
         */
        public String getFilename() {
            return new String(FileName, 0, FileNameLength / 2);
        }

        @Override
        public void read() {
            // avoid reading filename until we know how long it is
            FileName = new char[0];

View on GitHub (pinned to d036ad9781)

Solutions

  1. Pass a size >= FILE_NOTIFY_INFORMATION.size(), typically a multiple of it.
  2. Use a realistic buffer such as 64 KB (commonly used with ReadDirectoryChangesW).
  3. Compute size as records * size() if a record count is what you actually have.
  4. Add a guard: size = Math.max(size, FILE_NOTIFY_INFORMATION.size()) before constructing.

Example fix

// before
new FILE_NOTIFY_INFORMATION(8);
// after
int size = Math.max(64 * 1024, FILE_NOTIFY_INFORMATION.size());
new FILE_NOTIFY_INFORMATION(size);
Defensive patterns

Strategy: validation

Validate before calling

int requested = 64 * 1024;
if (requested < com.sun.jna.platform.win32.WinNT.FILE_NOTIFY_INFORMATION.size())
    throw new IllegalArgumentException("buffer too small: " + requested);

Prevention

When it happens

Trigger: Calling new FILE_NOTIFY_INFORMATION(n) where n < size() — e.g. passing 0, a guess like 4 bytes, or computing buffer size from an entry count without multiplying by the record size.

Common situations: Config-driven buffer sizes with a too-small default; users assuming size is per-entry rather than total bytes; refactoring that replaced a computed size with a hard-coded minimum.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/d9fa7b7817be9b73. Report an issue: GitHub.